Linux系统之last命令的基本使用

一、last命令介绍
last 命令在 Linux 系统中用于显示最近登录系统的用户列表。它读取 /var/log/wtmp 文件来获取登录信息,并且可以显示用户的登录名、终端、登录IP地址(如果通过网络登录)、登录时间以及登出时间等信息。
二、last命令的使用帮助
2.1 last命令help帮助信息
在命令行终端中,我们使用
--help查询last命令的基本帮助信息。
[root@openEuler-test ~]# last --help
Usage:
last [options] [...] [...]
Show a listing of last logged in users.
Options:
- how many lines to show
-a, --hostlast display hostnames in the last column
-d, --dns translate the IP number back into a hostname
-f, --file use a specific file instead of /var/log/wtmp
-F, --fulltimes print full login and logout times and dates
-i, --ip display IP numbers in numbers-and-dots notation
-n, --limit how many lines to show
-R, --nohostname don't display the hostname field
-s, --since display the lines since the specified time
-t, --until display the lines until the specified time
-p, --present display who were present at the specified time
-w, --fullnames display full user and domain names
-x, --system display system shutdown entries and run level changes
--time-format show timestamps in the specified :
notime|short|full|iso
-h, --help display this help
-V, --version display version
For more details see last(1).
2.2 last命令选项解释
last [选项] [<用户名>...] [<终端>...]
选项
选项 描述 - 显示多少行记录 -a, --hostlast 在最后一列中显示主机名 -d, --dns 将IP地址转换回主机名 -f, --file <文件> 使用指定的文件而不是 /var/log/wtmp -F, --fulltimes 打印完整的登录和登出时间和日期 -i, --ip 以点分十进制格式显示IP地址 -n, --limit <数量> 显示多少行记录 -R, --nohostname 不显示主机名字段 -s, --since <时间> 显示从指定时间开始的记录 -t, --until <时间> 显示到指定时间为止的记录 -p, --present <时间> 显示在指定时间时在线的用户 -w, --fullnames 显示完整的用户名和域名 -x, --system 显示系统关机条目和运行级别变更 --time-format <格式> 按照指定的 <格式> 显示时间戳:notime -h, --help 显示此帮助 -V, --version 显示版本号
三、last命令的基本使用
3.1 last直接使用
直接在命令行,使用last命令,会列出所有记录中的用户登录历史。
[root@openEuler-test ~]# last
root pts/0 192.168.3.241 Sat Sep 28 11:58 still logged in
root pts/0 192.168.3.241 Fri Sep 27 13:27 - 14:02 (00:34)
reboot system boot 6.6.0-28.0.0.34. Fri Sep 27 13:26 still running
root pts/0 192.168.3.241 Fri Sep 27 11:16 - down (02:09)
root pts/0 192.168.3.241 Fri Sep 27 08:49 - 09:19 (00:29)
root pts/0 192.168.3.241 Thu Sep 26 14:06 - 15:29 (01:23)
root pts/0 192.168.3.241 Thu Sep 26 13:32 - 14:05 (00:32)
root pts/0 192.168.3.241 Wed Sep 25 18:54 - 01:09 (06:15)
root pts/1 192.168.3.241 Tue Sep 24 23:06 - 00:14 (01:07)
root pts/0 192.168.3.241 Tue Sep 24 22:59 - 00:14 (01:15)
root pts/0 192.168.3.241 Tue Sep 24 22:47 - 22:58 (00:10)
root pts/0 192.168.3.241 Tue Sep 24 22:41 - 22:45 (00:03)
root pts/0 192.168.3.241 Tue Sep 24 22:39 - 22:39 (00:00)
root pts/0 192.168.3.241 Tue Sep 24 11:15 - 13:52 (02:36)
root pts/0 192.168.3.241 Sun Sep 22 23:52 - 01:01 (01:08)
root pts/0 192.168.3.241 Sat Sep 7 14:57 - 00:59 (10:02)
reboot system boot 6.6.0-28.0.0.34. Sun Sep 1 04:40 - 13:26 (26+08:46)
root pts/0 192.168.3.241 Sun Sep 1 04:33 - down (00:06)
root pts/0 192.168.3.241 Thu Aug 29 03:05 - 10:05 (07:00)
root pts/0 192.168.3.241 Wed Aug 21 07:19 - 20:04 (12:44)
root pts/0 192.168.3.241 Mon Aug 19 13:09 - 14:43 (01:33)
root pts/0 192.168.3.241 Wed Aug 14 20:43 - 20:43 (00:00)
root pts/0 192.168.3.241 Tue Aug 13 19:43 - 04:46 (09:03)
root pts/0 192.168.3.241 Mon Aug 12 11:12 - 03:19 (16:07)
root pts/0 192.168.3.241 Sun Aug 11 21:26 - 00:12 (02:46)
root pts/0 192.168.3.241 Sun Aug 11 10:02 - 10:19 (00:17)
root pts/0 192.168.3.241 Sat Aug 10 12:02 - 21:42 (09:40)
root pts/1 192.168.3.241 Thu Aug 8 16:46 - 19:00 (02:14)
root pts/0 192.168.3.168 Thu Aug 8 16:41 - 18:55 (02:14)
root tty1 Thu Aug 8 16:40 - 04:39 (23+11:59)
reboot system boot 6.6.0-28.0.0.34. Thu Aug 8 16:38 - 04:39 (23+12:01)
reboot system boot 6.6.0-28.0.0.34. Thu Jun 27 15:54 - 04:39 (65+12:45)
wtmp begins Thu Jun 27 15:54:01 2024
3.2 显示特定用户的登录记录
如果我们只想查看某个用户的登录记录,可以在命令后加上用户名。
[root@openEuler-test ~]# last admin
admin pts/1 192.168.3.89 Sat Sep 28 15:55 - 15:55 (00:00)
wtmp begins Thu Jun 27 15:54:01 2024
3.3 显示自系统启动以来的所有活动
如果想查看自上一次系统重启以来的所有登录和注销活动,可以使用 -s 和 -t 选项来定义一个时间段,或者简单地使用 reboot 关键字来代替用户名:
[root@openEuler-test ~]# last reboot
reboot system boot 6.6.0-28.0.0.34. Fri Sep 27 13:26 still running
reboot system boot 6.6.0-28.0.0.34. Sun Sep 1 04:40 - 13:26 (26+08:46)
reboot system boot 6.6.0-28.0.0.34. Thu Aug 8 16:38 - 04:39 (23+12:01)
reboot system boot 6.6.0-28.0.0.34. Thu Jun 27 15:54 - 04:39 (65+12:45)
wtmp begins Thu Jun 27 15:54:01 2024
3.4 显示指定数量的最新记录
通过 -n 选项来限制输出的行数,例如只显示最新的5条记录:
[root@openEuler-test ~]# last -n 5
admin pts/1 192.168.3.89 Sat Sep 28 15:55 - 15:55 (00:00)
root pts/0 192.168.3.241 Sat Sep 28 11:58 still logged in
root pts/0 192.168.3.241 Fri Sep 27 13:27 - 14:02 (00:34)
reboot system boot 6.6.0-28.0.0.34. Fri Sep 27 13:26 still running
root pts/0 192.168.3.241 Fri Sep 27 11:16 - down (02:09)
wtmp begins Thu Jun 27 15:54:01 2024
3.5 查询1小时前还在线用户
可以使用 -p 或 --present 选项。这个选项允许你指定一个时间点,并且 last 会显示在那个时间点仍然登录系统的用户。
[root@openEuler-test ~]# last -p "1 hour ago"
root pts/0 192.168.3.241 Sat Sep 28 11:58 still logged in
reboot system boot 6.6.0-28.0.0.34. Fri Sep 27 13:26 still running
wtmp begins Thu Jun 27 15:54:01 2024
四、总结
确保
/var/log/wtmp文件存在且可读,因为last命令依赖于该文件来显示登录信息。需要有适当的权限(通常是root用户或具有sudo权限的用户)才能查看完整的登录记录。
如果系统非常繁忙,
/var/log/wtmp文件可能会变得很大,可能会影响last命令的性能。使用
-p或--present选项指定时间时,确保提供的时间格式正确无误。使用
-f选项指定的日志文件必须存在并且格式正确,否则last可能无法正常工作。使用
-s和-t选项限制时间范围时,确保时间格式与系统日志中的时间戳匹配。如果系统未启用 wtmp 记录功能,
last命令将不会返回任何结果。定期检查和维护
/var/log/wtmp文件,以防止其占用过多磁盘空间。查询特定用户时,确保用户名拼写正确,否则
last将不会显示任何结果。对于远程登录,确保网络配置允许记录IP地址,以便
last能够显示正确的来源信息。
作者声明本文无利益相关,欢迎值友理性交流,和谐讨论~
