linux重启查看日志及历史记录 查询原因

次浏览

摘要:linux系统文件通常在/var/log 中下面是对下面常出现的文件进行解释...

linux系统文件通常在 /var/log 中下面是对下面常出现的文件进行解释

/var/log/message ---------------------------------------系统启动后的信息和错误日志

/var/log/secure ------------------------------------------与安全相关的日志信息

/var/log/maillog ------------------------------------------与邮件相关的日志信息

/var/log/cron ----------------------------------------------与定时任务相关的日志信息

/var/log/spooler ------------------------------------------与UUCP和news设备相关的日志信息

/var/log/boot.log -----------------------------------------守护进程启动和停止相关的日志消息

/var/log/wtmp ---------------------------------------------永久记录每个用户登录、注销及系统的启动、停机的事件

/var/run/utmp ---------------------------------------------记录当前正在登录系统的用户信息;

/var/log/btmp ----------------------------------------------记录失败的登录尝试信息。

查看历史记录

last | grep reboot -----------------------------------------查看重启的命令

root@U-NAS:~# last | grep reboot
reboot   system boot  4.9.0-11-amd64   Thu Jan  2 09:09   still running
reboot   system boot  4.9.0-11-amd64   Fri Dec 27 10:42   still running
reboot   system boot  4.9.0-11-amd64   Wed Dec 25 11:24   still running
reboot   system boot  4.9.0-11-amd64   Fri Dec 20 09:01   still running
reboot   system boot  4.9.0-11-amd64   Wed Dec 18 09:12   still running
reboot   system boot  4.9.0-11-amd64   Tue Dec 17 09:03   still running
reboot   system boot  4.9.0-11-amd64   Mon Dec 16 17:13   still running
reboot   system boot  4.9.0-11-amd64   Wed Dec 11 16:11   still running
reboot   system boot  4.9.0-11-amd64   Tue Dec 10 16:59   still running
reboot   system boot  4.9.0-11-amd64   Tue Dec 10 16:57 - 16:59  (00:02)
reboot   system boot  4.9.0-11-amd64   Tue Nov 26 10:27 - 16:59 (14+06:31)
reboot   system boot  4.9.0-11-amd64   Mon Nov 25 17:54 - 16:59 (14+23:04)
reboot   system boot  4.9.0-11-amd64   Fri Nov 22 14:52 - 17:54 (3+03:01)
reboot   system boot  4.9.0-11-amd64   Fri Nov 22 12:56 - 17:54 (3+04:57)

history -------------------------------------------------------历史操作

history 10 -------------------------------------------------------历史操作-显示最近10条;修改为 history 3,则显示最近3条

root@U-NAS:~# history 3
    2  ls
    3  last | grep reboot
    4  history 3

history -c ----------------------------------------------------立即清空当前所有的历史记录

root@U-NAS:~# history -c

Linux 系统查询机器最近重启时间命令

last 命令不仅可以按照时间从近到远的顺序列出该会话的特定用户、终端和主机名,而且还可以列出指定日期和时间登录的用户。

输出到终端的每一行都包括用户名、会话终端、主机名、会话开始和结束的时间、会话持续的时间.

1.使用 last 命令来查询最近登录到系统的用户和系统重启的时间和日期。输入:last reboot #查看所有重启日志信息

root@U-NAS:~# last reboot  #查看所有重启日志信息
reboot   system boot  4.9.0-11-amd64   Thu Jan  2 09:09   still running
reboot   system boot  4.9.0-11-amd64   Fri Dec 27 10:42   still running
reboot   system boot  4.9.0-11-amd64   Wed Dec 25 11:24   still running
reboot   system boot  4.9.0-11-amd64   Fri Dec 20 09:01   still running
reboot   system boot  4.9.0-11-amd64   Wed Dec 18 09:12   still running
reboot   system boot  4.9.0-11-amd64   Tue Dec 17 09:03   still running
reboot   system boot  4.9.0-11-amd64   Mon Dec 16 17:13   still running
reboot   system boot  4.9.0-11-amd64   Wed Dec 11 16:11   still running
reboot   system boot  4.9.0-11-amd64   Tue Dec 10 16:59   still running
reboot   system boot  4.9.0-11-amd64   Tue Dec 10 16:57 - 16:59  (00:02)
reboot   system boot  4.9.0-11-amd64   Tue Nov 26 10:27 - 16:59 (14+06:31)
reboot   system boot  4.9.0-11-amd64   Mon Nov 25 17:54 - 16:59 (14+23:04)
reboot   system boot  4.9.0-11-amd64   Fri Nov 22 14:52 - 17:54 (3+03:01)
reboot   system boot  4.9.0-11-amd64   Fri Nov 22 12:56 - 17:54 (3+04:57)
reboot   system boot  4.9.0-11-amd64   Wed Nov 20 13:42 - 13:45  (00:03)
reboot   system boot  4.9.0-11-amd64   Wed Nov 20 11:34 - 13:42  (02:07)
reboot   system boot  4.9.0-11-amd64   Wed Nov 20 11:19 - 11:34  (00:15)
reboot   system boot  4.9.0-11-amd64   Fri Nov 15 10:03 - 11:34 (5+01:30)
reboot   system boot  4.9.0-11-amd64   Wed Nov 13 11:54 - 11:34 (6+23:40)

wtmp begins Wed Nov 13 11:54:00 2019

这条命令实际上显示的是最近几天的系统运行时间。last 原本被设计来显示某个特定用户的登录历史。在Linux中,有一个特别的“伪用户”称为reboot会在系统重启的时候立即自动登录。这样通过检查reboot用户的登录历史,你就可以检查最后的重启时间。

 

或者:last reboot | head -1 #查看最近的一条

root@U-NAS:~# last reboot | head -1 #查看最近的一条
reboot   system boot  4.9.0-11-amd64   Thu Jan  2 09:09   still running

在实际开发过程中,有时可能发现有一些服务器的进程挂了,查询相关错误日志也没有头绪。此时需要考虑是否是由于服务器重启导致的错误,使用命令 last reboot 来查看是否自动重启过。

导致服务器重启的原因有很多,可能是物理机资源分配不合理导致虚拟机出问题,也可能是系统内存不足,也可能是由于CPU自身的BUG,这个需要根据 /var/log/messages 系统日志进一步判断

 

查看系统上次关机的时间和日期

可以使用下面的命令来显示上次关机的日期和时间:last -x|grep shutdown | head -1 #查看最近的一条,head -1 是指定最近 1条 的参数

root@U-NAS:~# last -x|grep shutdown | head -1
shutdown system down  4.9.0-11-amd64   Tue Dec 10 16:59 - 16:59  (00:00)

命令中,-x:显示系统关机和运行等级改变信息

这里是 last 命令的其它的一些选项:

  • last
  • last -x
  • last -x reboot
  • last -x shutdown

 

查看系统正常的运行时间

使用 uptime 命令来推断系统最后的启动时间。uptime 命令会显示当前的时间,同样也会显示系统已经运行的时间。从这些信息中,你就可以计算系统最后启动的时间了

命令如下:

uptime -p 系统启动时长

uptime -s 系统启动时间

root@U-NAS:~# uptime
 13:08:31 up  3:59,  2 users,  load average: 0.20, 0.07, 0.02
root@U-NAS:~# uptime -p
up 3 hours, 59 minutes
root@U-NAS:~# uptime -s
2020-01-02 09:09:16

 

---------------------------------- THE END ----------------------------------

随机新闻