last 命令

本文详细介绍了Linux系统中last命令的功能及用法,last命令用于显示系统登录日志,包括用户登录和登出的时间、主机名、IP地址等信息。文章通过实际操作演示了如何使用last命令的各种选项参数来查看特定条件下的登录记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

last 命令:

功能说明:列出目前与过去登入系统的用户相关信息。

  语  法:last [-adRx][-f <记录文件>][-n <显示行数>][帐号名称...][终端机编号...]

  补充说明:单独执行last指令,它会读取位于/var/log目录下,名称为wtmp的文件,并把该给文件的内容记录的登入系统的用户名单全部显示出来。

  参  数:
  -a  把从何处登入系统的主机名称或IP地址,显示在最后一行。
  -d  将IP地址转换成主机名称。
  -f <记录文件>  指定记录文件。
  -n <显示行数>或-<显示行数>  设置列出名单的显示行数。
  -R  不显示登入系统的主机名称或IP地址。
  -x  显示系统关机,重新开机,以及执行等级的改变等信息。

 

 

例子:

 

[root@ora2 ~]# date
Tue May 10 21:14:52 CST 2011
[root@ora2 ~]# last -a
root     pts/3        Tue May 10 21:14   still logged in    192.168.36.71
root     pts/2        Tue May 10 21:09   still logged in    :0.0
root     pts/1        Tue May 10 21:05   still logged in    :0.0
root     :0           Tue May 10 21:04   still logged in   
root     :0           Tue May 10 21:04 - 21:04  (00:00)    
reboot   system boot  Tue May 10 20:57          (00:17)     2.6.18-92.el5
root     pts/2        Wed Jan 26 05:36 - crash (104+15:21)  192.168.36.71
root     pts/1        Wed Jan 26 05:35 - crash (104+15:22)  :0.0
root     :0           Wed Jan 26 05:34 - crash (104+15:23) 
root     :0           Wed Jan 26 05:34 - 05:34  (00:00)    
reboot   system boot  Wed Jan 26 05:07         (104+16:07)  2.6.18-92.el5
reboot   system boot  Wed Jan 26 04:47          (00:16)     2.6.18-92.el5

wtmp begins Wed Jan 26 04:47:12 2011
[root@ora2 ~]# last
root     pts/3        192.168.36.71    Tue May 10 21:14   still logged in  
root     pts/2        :0.0             Tue May 10 21:09   still logged in  
root     pts/1        :0.0             Tue May 10 21:05   still logged in  
root     :0                            Tue May 10 21:04   still logged in  
root     :0                            Tue May 10 21:04 - 21:04  (00:00)   
reboot   system boot  2.6.18-92.el5    Tue May 10 20:57          (00:17)   
root     pts/2        192.168.36.71    Wed Jan 26 05:36 - crash (104+15:21)
root     pts/1        :0.0             Wed Jan 26 05:35 - crash (104+15:22)
root     :0                            Wed Jan 26 05:34 - crash (104+15:23)
root     :0                            Wed Jan 26 05:34 - 05:34  (00:00)   
reboot   system boot  2.6.18-92.el5    Wed Jan 26 05:07         (104+16:07)
reboot   system boot  2.6.18-92.el5    Wed Jan 26 04:47          (00:16)   

wtmp begins Wed Jan 26 04:47:12 2011
[root@ora2 ~]# last -d
root     pts/3        192.168.36.71    Tue May 10 21:14   still logged in  
root     pts/2        :0.0             Tue May 10 21:09   still logged in  
root     pts/1        :0.0             Tue May 10 21:05   still logged in  
root     :0                            Tue May 10 21:04   still logged in  
root     :0                            Tue May 10 21:04 - 21:04  (00:00)   
reboot   system boot  2.6.18-92.el5    Tue May 10 20:57          (00:17)   
root     pts/2        192.168.36.71    Wed Jan 26 05:36 - crash (104+15:21)
root     pts/1        :0.0             Wed Jan 26 05:35 - crash (104+15:22)
root     :0                            Wed Jan 26 05:34 - crash (104+15:23)
root     :0                            Wed Jan 26 05:34 - 05:34  (00:00)   
reboot   system boot  2.6.18-92.el5    Wed Jan 26 05:07         (104+16:08)
reboot   system boot  2.6.18-92.el5    Wed Jan 26 04:47          (00:16)   

wtmp begins Wed Jan 26 04:47:12 2011
[root@ora2 ~]# man last
Formatting page, please wait...
LAST,LASTB(1)         Linux System Administrator鈥檚 Manual        LAST,LASTB(1)

NAME
       last, lastb - show listing of last logged in users

SYNOPSIS
       last  [-R] [-num] [ -n num ] [-adiox] [ -f file ] [ -t YYYYMMDDHHMMSS ]
       [name...]  [tty...]
       lastb [-R] [-num] [ -n num ] [ -f file ] [-adiox] [name...]  [tty...]

DESCRIPTION
       Last searches back through the file /var/log/wtmp (or the  file  desig-
       nated  by  the -f flag) and displays a list of all users logged in (and
       out) since that file was created.  Names of  users  and  tty鈥檚  can  be
       given,  in  which  case  last will show only those entries matching the
       arguments.  Names of ttys can be abbreviated, thus last 0 is  the  same
       as last tty0.

       When last catches a SIGINT signal (generated by the interrupt key, usu-
       ally control-C) or a SIGQUIT signal (generated by the quit key, usually
       control-/), last will show how far it has searched through the file; in
       the case of the SIGINT signal last will then terminate.

       The pseudo user reboot logs in each time the system is rebooted.   Thus
       last  reboot will show a log of all reboots since the log file was cre-
       ated.

       Lastb is the same as last, except that by default it shows a log of the
       file /var/log/btmp, which contains all the bad login attempts.

OPTIONS
       -num   This is a count telling last how many lines to show.

       -n num The same.

       -t YYYYMMDDHHMMSS
              Display  the  state of logins as of the specified time.  This is
              useful, e.g., to determine easily who was logged in at a partic-
              ular  time  --  specify  that  time  with -t and look for "still
              logged in".

       -f file
              Specifies a file to search other than /var/log/wtmp.

       -R     Suppresses the display of the hostname field.

       -a     Display the hostname in the last column. Useful  in  combination
              with the next flag.

       -d     For non-local logins, Linux stores not only the host name of the
              remote host but its IP number as well.  This  option  translates
              the IP number back into a hostname.

       -i     This  option is like -d in that it displays the IP number of the
              remote host, but it displays the IP number  in  numbers-and-dots
              notation.

       -o     Read  an  old-type  wtmp  file  (written by linux-libc5 applica-
              tions).

       -x     Display the system shutdown entries and run level changes.

NOTES
       The files wtmp and btmp might not be found. The system only logs infor-
       mation  in  these files if they are present. This is a local configura-
       tion issue. If you want the files to be used, they can be created  with
       a simple touch(1) command (for example, touch /var/log/wtmp).

FILES
       /var/log/wtmp
       /var/log/btmp

AUTHOR
       Miquel van Smoorenburg, miquels@cistron.nl

SEE ALSO
       shutdown(8), login(1), init(8)

                                 Jul 31, 2004                    LAST,LASTB(1)
[root@ora2 ~]# last
root     pts/3        192.168.36.71    Tue May 10 21:14   still logged in  
root     pts/2        :0.0             Tue May 10 21:09   still logged in  
root     pts/1        :0.0             Tue May 10 21:05   still logged in  
root     :0                            Tue May 10 21:04   still logged in  
root     :0                            Tue May 10 21:04 - 21:04  (00:00)   
reboot   system boot  2.6.18-92.el5    Tue May 10 20:57          (00:18)   
root     pts/2        192.168.36.71    Wed Jan 26 05:36 - crash (104+15:21)
root     pts/1        :0.0             Wed Jan 26 05:35 - crash (104+15:22)
root     :0                            Wed Jan 26 05:34 - crash (104+15:23)
root     :0                            Wed Jan 26 05:34 - 05:34  (00:00)   
reboot   system boot  2.6.18-92.el5    Wed Jan 26 05:07         (104+16:08)
reboot   system boot  2.6.18-92.el5    Wed Jan 26 04:47          (00:16)   

wtmp begins Wed Jan 26 04:47:12 2011
[root@ora2 ~]# last -R
root     pts/3        Tue May 10 21:14   still logged in  
root     pts/2        Tue May 10 21:09   still logged in  
root     pts/1        Tue May 10 21:05   still logged in  
root     :0           Tue May 10 21:04   still logged in  
root     :0           Tue May 10 21:04 - 21:04  (00:00)   
reboot   system boot  Tue May 10 20:57          (00:18)   
root     pts/2        Wed Jan 26 05:36 - crash (104+15:21)
root     pts/1        Wed Jan 26 05:35 - crash (104+15:22)
root     :0           Wed Jan 26 05:34 - crash (104+15:23)
root     :0           Wed Jan 26 05:34 - 05:34  (00:00)   
reboot   system boot  Wed Jan 26 05:07         (104+16:08)
reboot   system boot  Wed Jan 26 04:47          (00:16)   

wtmp begins Wed Jan 26 04:47:12 2011
[root@ora2 ~]# last - x

wtmp begins Wed Jan 26 04:47:12 2011
[root@ora2 ~]# last -x
root     pts/3        192.168.36.71    Tue May 10 21:14   still logged in  
root     pts/2        :0.0             Tue May 10 21:09   still logged in  
root     pts/1        :0.0             Tue May 10 21:05   still logged in  
root     :0                            Tue May 10 21:04   still logged in  
root     :0                            Tue May 10 21:04 - 21:04  (00:00)   
runlevel (to lvl 5)   2.6.18-92.el5    Tue May 10 20:57 - 21:16  (00:18)   
reboot   system boot  2.6.18-92.el5    Tue May 10 20:57          (00:18)   
root     pts/2        192.168.36.71    Wed Jan 26 05:36 - crash (104+15:21)
root     pts/1        :0.0             Wed Jan 26 05:35 - crash (104+15:22)
root     :0                            Wed Jan 26 05:34 - crash (104+15:23)
root     :0                            Wed Jan 26 05:34 - 05:34  (00:00)   
runlevel (to lvl 5)   2.6.18-92.el5    Wed Jan 26 05:07 - 20:57 (104+15:50)
reboot   system boot  2.6.18-92.el5    Wed Jan 26 05:07         (104+16:09)
shutdown system down  2.6.18-92.el5    Wed Jan 26 05:04 - 21:16 (104+16:11)
runlevel (to lvl 6)   2.6.18-92.el5    Wed Jan 26 05:03 - 05:04  (00:00)   
runlevel (to lvl 5)   2.6.18-92.el5    Wed Jan 26 04:47 - 05:03  (00:16)   
reboot   system boot  2.6.18-92.el5    Wed Jan 26 04:47          (00:16)   

wtmp begins Wed Jan 26 04:47:12 2011
[root@ora2 ~]# last -a
root     pts/3        Tue May 10 21:14   still logged in    192.168.36.71
root     pts/2        Tue May 10 21:09   still logged in    :0.0
root     pts/1        Tue May 10 21:05   still logged in    :0.0
root     :0           Tue May 10 21:04   still logged in   
root     :0           Tue May 10 21:04 - 21:04  (00:00)    
reboot   system boot  Tue May 10 20:57          (00:19)     2.6.18-92.el5
root     pts/2        Wed Jan 26 05:36 - crash (104+15:21)  192.168.36.71
root     pts/1        Wed Jan 26 05:35 - crash (104+15:22)  :0.0
root     :0           Wed Jan 26 05:34 - crash (104+15:23) 
root     :0           Wed Jan 26 05:34 - 05:34  (00:00)    
reboot   system boot  Wed Jan 26 05:07         (104+16:09)  2.6.18-92.el5
reboot   system boot  Wed Jan 26 04:47          (00:16)     2.6.18-92.el5

wtmp begins Wed Jan 26 04:47:12 2011
[root@ora2 ~]# exit
logout

<think>好的,我现在需要帮助用户了解Linux中的last命令的详细用法。用户已经提供了几个相关的引用资料,我需要先仔细阅读这些资料,整理出last命令的功能、语法、参数选项以及使用案例,然后按照用户的要求生成相关问题。 首先,看看引用内容。引用[1]提到last是用来显示最近登录会话的信息,帮助跟踪用户活动或安全漏洞。引用[2]说明last读取/var/log/wtmp文件,记录登录、注销、系统启动关闭等信息。基本语法和功能部分需要整理。引用[4]和[5]详细介绍了命令的语法和参数选项,比如-a、-d、-R、-x等。引用[3]虽然主要讲其他命令,但可能对理解命令格式有帮助。 接下来,需要将信息结构化。用户需要详细的使用说明,所以应该包括功能概述、命令语法、参数选项解释、使用案例以及注意事项。同时,用户要求生成相关问题,这需要根据内容提出常见的问题,比如如何显示特定用户登录、如何查看关机记录、如何限制显示行数等。 在整理过程中,要注意引用对应的来源,比如参数选项的解释来自引用[5],文件路径来自引用[2]和[4]。案例部分需要结合参数选项,比如使用-n限制行数,使用-x查看系统事件,这些在引用[5]中有提到。 另外,用户提到要使用中文回答,并且行内公式用$...$,但last命令可能不涉及数学公式,所以这部分可能不需要处理。但需要确保LaTeX语法正确,不过这里主要是命令和参数,可能用代码块更合适。 最后,生成相关问题时要考虑用户可能进一步想知道的内容,比如查看实时日志、与其他命令对比、日志文件位置、安全审计应用等。这些问题应基于已提供的信息,并引导用户深入学习。 现在需要检查所有参数是否覆盖完整,比如-a、-d、-f、-n、-R、-x等,确保每个参数都有解释和案例。同时注意语法格式,如代码块使用反引号,参数用短横线,引用标识正确添加。 可能需要注意的地方:引用[4]提到wtmp文件的路径和示例输出,但用户问题主要是用法,所以案例中需要展示命令和输出示例,比如last -x | head -n 5这样的组合命令是否合适?需要确保正确性,可能参考引用中的例子。 总结步骤: 1. 功能概述:基于引用[1][2][5]。 2. 语法结构:引用[5]的语法部分。 3. 参数选项:详细列出每个参数及其作用,引用[5]和[2]。 4. 使用案例:每个案例对应一个参数或常见用法。 5. 注意事项:如需要权限,文件位置等。 6. 相关问题:围绕功能、参数、应用场景生成问题。 现在开始组织内容,确保结构清晰,引用正确,语言简洁。</think>### Linux last 命令使用说明 #### 一、功能概述 `last` 命令用于查看系统用户的登录历史记录,包括登录/注销时间、来源IP地址、终端类型等信息。它通过读取 `/var/log/wtmp` 文件实现[^2][^4][^5],还可显示系统重启、关机等事件(需配合 `-x` 参数)[^2]。 #### 二、命令语法 ```bash last [-adRx] [-f <记录文件>] [-n <显示行数>] [用户名] [终端号] ``` #### 三、参数详解 | 参数 | 说明 | |------|------| | `-a` | 将登录来源的主机名或IP显示在最后一列 | | `-d` | 将IP地址解析为主机名 | | `-R` | **隐藏**登录来源的主机名/IP(默认行为) | | `-x` | 显示系统关机、重启及运行级别变更事件[^5] | | `-f <文件>` | 指定自定义日志文件(默认读取 `/var/log/wtmp`)[^5] | | `-n <行数>` | 限制显示的行数(如 `-n 10` 显示最近10条记录) | #### 四、使用案例 1. **基础用法** ```bash last # 显示全部登录记录(按时间倒序排列) ``` 输出示例: ``` user1 pts/0 192.168.1.100 Mon Jun 10 09:30 - 11:45 (02:15) reboot system boot 5.4.0-91-generic Mon Jun 10 09:28 - 11:50 (02:22) ``` 2. **查看系统关机/重启记录** ```bash last -x | grep shutdown # 筛选关机事件 ``` 3. **显示特定用户的登录历史** ```bash last -a -n 5 user1 # 显示用户user1最近5次登录,包含来源IP ``` 4. **解析IP为主机名** ```bash last -d # 自动转换IP地址为主机名 ``` 5. **指定自定义日志文件** ```bash last -f /var/log/wtmp.1 # 分析历史日志文件 ``` #### 五、注意事项 1. `/var/log/wtmp` 文件需要 root 权限才能直接读取[^2]。 2. 输出结果按时间倒序排列,最新记录显示在最上方。 3. 若需实时监控登录行为,可结合 `watch` 命令(如 `watch -n 1 last`)[^3]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值