在Linux系统中,可以通过以下几种方式查看过去登录的用户信息:
last
命令last
命令是最常用的工具,它会读取 /var/log/wtmp
文件(记录登录历史)并显示用户的登录/登出记录。
last
username pts/0 192.168.1.100 Tue Oct 10 09:30 - 11:45 (02:15)
username tty1 Mon Oct 9 18:20 - 20:50 (02:30)
reboot system boot 5.4.0-91-generic Mon Oct 9 18:18 - now (1 day)
last -n 10
:仅显示最近10条记录。last -x
:显示关机/重启记录。last username
:查看特定用户(如 root
)的登录记录。/var/log/auth.log
或 /var/log/secure
bash
grep "session opened" /var/log/auth.log
bash
grep "session opened" /var/log/secure
这些日志会记录详细的认证信息(如SSH登录、sudo操作等)。who
或 w
命令(当前登录用户)who
:显示当前登录的用户。
bash
who
w
:显示更详细的当前用户活动(包括运行的命令)。
bash
w
/var/log/wtmp
和 /var/log/btmp
/var/log/wtmp
:记录成功的登录信息(需用 last
命令解析)。/var/log/btmp
:记录失败的登录尝试(需用 lastb
命令解析)。
bash
lastb # 查看失败的登录尝试
utmpdump
工具(原始日志分析)如果需要解析二进制日志文件(如 wtmp
、btmp
),可以使用:
utmpdump /var/log/wtmp # 显示原始内容
如果用户曾执行过命令,可以通过其 ~/.bash_history
文件查看:
sudo cat /home/username/.bash_history
/var/log/secure
、/var/log/btmp
)。auth.log.1
、wtmp.1
),需检查归档文件。auditd
)或第三方监控服务。根据需求选择合适的方法,last
通常是首选。