查看linux系统重启的日志
文章目录
- 方法 1:使用 last 命令
- 方法 2:使用 journalctl 命令(适用于 systemd 系统)
- 方法 3:查看 /var/log/wtmp 文件
- 方法 4:查看 /var/log/messages 或 /var/log/syslog
- 方法 5:查看 /var/log/boot.log
- 方法 6:使用 uptime 命令
在 Linux 系统中,系统重启的日志通常记录在系统日志文件中。你可以通过以下几种方式查看系统重启的日志,以确定系统何时重启。
● last reboot:查看系统重启记录。
● journalctl:适用于 systemd 系统,查看详细的启动日志。
● /var/log/wtmp:记录系统登录、注销和重启事件。
● /var/log/messages 或 /var/log/syslog:查找系统日志中的重启记录。
● /var/log/boot.log:查看系统启动时的日志。
● uptime:查看系统上次启动的时间。
方法 1:使用 last 命令
last 命令可以显示系统的重启和关机记录。
- 运行以下命令:
last reboot - 输出示例:
reboot system boot 5.4.0-42-generic Tue Oct 10 14:30:01 2023 - Tue Oct 10 15:00:01 2023 (00:30)
reboot system boot 5.4.0-42-generic Mon Oct 9 09:15:01 2023 - Mon Oct 9 10:00:01 2023 (00:45)
○ 每一行表示一次重启事件。
○ 时间格式为 月 日 时:分:秒 年。
方法 2:使用 journalctl 命令(适用于 systemd 系统)
journalctl 是 systemd 系统的日志管理工具,可以查看系统日志,包括重启记录。
- 运行以下命令查看重启日志:
sudo journalctl -b
○ -b 选项表示查看当前启动的日志。
○ 如果要查看上一次启动的日志,可以使用:
sudo journalctl -b -1
○ 如果要查看所有启动的日志,可以使用:
sudo journalctl --list-boots
输出示例:
0 abc1234 Tue 2023-10-10 14:30:01 - Tue 2023-10-10 15:00:01 (30min)
-1 xyz5678 Mon 2023-10-09 09:15:01 - Mon 2023-10-09 10:00:01 (45min) - 如果要查看特定时间段的日志,可以使用时间过滤器。例如:
sudo journalctl --since “2023-10-10 00:00:00” --until “2023-10-10 23:59:59”
方法 3:查看 /var/log/wtmp 文件
/var/log/wtmp 文件记录了系统的登录、注销和重启事件。可以使用 last 命令解析该文件。
- 运行以下命令:
last -f /var/log/wtmp - 输出示例:
reboot system boot 5.4.0-42-generic Tue Oct 10 14:30:01 2023 - Tue Oct 10 15:00:01 2023 (00:30)
reboot system boot 5.4.0-42-generic Mon Oct 9 09:15:01 2023 - Mon Oct 9 10:00:01 2023 (00:45)
方法 4:查看 /var/log/messages 或 /var/log/syslog
在某些 Linux 发行版中,系统日志会记录在 /var/log/messages 或 /var/log/syslog 文件中。
- 使用 grep 命令查找重启相关的日志:
grep “systemd: Started” /var/log/messages
或:
grep “systemd: Started” /var/log/syslog - 输出示例:
Oct 10 14:30:01 hostname systemd: Started User Manager for UID 1000.
Oct 10 14:30:01 hostname systemd: Started Session 1 of user root.
方法 5:查看 /var/log/boot.log
/var/log/boot.log 文件记录了系统启动时的日志。
- 使用以下命令查看:
cat /var/log/boot.log - 输出示例:
[ OK ] Started User Manager for UID 1000.
[ OK ] Started Session 1 of user root.
方法 6:使用 uptime 命令
uptime 命令可以显示系统已经运行了多长时间,从而推断出上次重启的时间。
- 运行以下命令:
uptime -s - 输出示例:
2023-10-10 14:30:01
○ 这表示系统上次启动的时间是 2023-10-10 14:30:01。