如何在 Linux 系統查詢機器最近重啟時間

Vivek Gite發表於2018-02-06

如何在 Linux 系統查詢機器最近重啟時間

在你的 Linux 或類 UNIX 系統中,你是如何查詢系統上次重新啟動的日期和時間?怎樣顯示系統關機的日期和時間? last 命令不僅可以按照時間從近到遠的順序列出該會話的特定使用者、終端和主機名,而且還可以列出指定日期和時間登入的使用者。輸出到終端的每一行都包括使用者名稱、會話終端、主機名、會話開始和結束的時間、會話持續的時間。要檢視 Linux 或類 UNIX 系統重啟和關機的時間和日期,可以使用下面的命令。

  • last 命令
  • who 命令

使用 who 命令來檢視系統重新啟動的時間/日期

你需要在終端使用 who 命令來列印有哪些人登入了系統,who 命令同時也會顯示上次系統啟動的時間。使用 last 命令來檢視系統重啟和關機的日期和時間,執行:

$ who -b

示例輸出:

system boot 2017-06-20 17:41

使用 last 命令來查詢最近登入到系統的使用者和系統重啟的時間和日期。輸入:

$ last reboot | less

示例輸出:

Fig.01: last command in action

或者,嘗試輸入:

$ last reboot | head -1

示例輸出:

reboot system boot 4.9.0-3-amd64 Sat Jul 15 19:19 still running

last 命令透過檢視檔案 /var/log/wtmp 來顯示自 wtmp 檔案被建立時的所有登入(和登出)的使用者。每當系統重新啟動時,這個偽使用者 reboot 就會登入。因此,last reboot 命令將會顯示自該日誌檔案被建立以來的所有重啟資訊。

檢視系統上次關機的時間和日期

可以使用下面的命令來顯示上次關機的日期和時間:

$ last -x|grep shutdown | head -1

示例輸出:

shutdown system down 2.6.15.4 Sun Apr 30 13:31 - 15:08 (01:37)

命令中,

  • -x:顯示系統關機和執行等級改變資訊

這裡是 last 命令的其它的一些選項:

$ last
$ last -x
$ last -x reboot
$ last -x shutdown

示例輸出:

Fig.01: How to view last Linux System Reboot Date/Time

檢視系統正常的執行時間

評論區的讀者建議的另一個命令如下:

$ uptime -s

示例輸出:

2017-06-20 17:41:51

OS X/Unix/FreeBSD 檢視最近重啟和關機時間的命令示例

在終端輸入下面的命令:

$ last reboot

在 OS X 示例輸出結果如下:

reboot ~ Fri Dec 18 23:58
reboot ~ Mon Dec 14 09:54
reboot ~ Wed Dec 9 23:21
reboot ~ Tue Nov 17 21:52
reboot ~ Tue Nov 17 06:01
reboot ~ Wed Nov 11 12:14
reboot ~ Sat Oct 31 13:40
reboot ~ Wed Oct 28 15:56
reboot ~ Wed Oct 28 11:35
reboot ~ Tue Oct 27 00:00
reboot ~ Sun Oct 18 17:28
reboot ~ Sun Oct 18 17:11
reboot ~ Mon Oct 5 09:35
reboot ~ Sat Oct 3 18:57


wtmp begins Sat Oct 3 18:57

檢視關機日期和時間,輸入:

$ last shutdown

示例輸出:

shutdown ~ Fri Dec 18 23:57
shutdown ~ Mon Dec 14 09:53
shutdown ~ Wed Dec 9 23:20
shutdown ~ Tue Nov 17 14:24
shutdown ~ Mon Nov 16 21:15
shutdown ~ Tue Nov 10 13:15
shutdown ~ Sat Oct 31 13:40
shutdown ~ Wed Oct 28 03:10
shutdown ~ Sun Oct 18 17:27
shutdown ~ Mon Oct 5 09:23


wtmp begins Sat Oct 3 18:57

如何檢視是誰重啟和關閉機器?

你需要啟用 psacct 服務然後執行下面的命令來檢視執行過的命令(包括使用者名稱),在終端輸入 lastcomm 命令檢視資訊

# lastcomm userNameHere
# lastcomm commandNameHere
# lastcomm | more
# lastcomm reboot
# lastcomm shutdown
### 或者檢視重啟和關機時間
# lastcomm | egrep 'reboot|shutdown'

示例輸出:

reboot S X root pts/0 0.00 secs Sun Dec 27 23:49
shutdown S root pts/1 0.00 secs Sun Dec 27 23:45

我們可以看到 root 使用者在當地時間 12 月 27 日星期二 23:49 在 pts/0 重新啟動了機器。

參見

關於作者

作者是 nixCraft 的創立者,同時也是一名經驗豐富的系統管理員,也是 Linux,類 Unix 作業系統 shell 指令碼的培訓師。他曾與全球各行各業的客戶工作過,包括 IT,教育,國防和空間研究以及非營利部門等等。你可以在 TwitterFacebookGoogle+ 關注他。


via: https://www.cyberciti.biz/tips/linux-last-reboot-time-and-date-find-out.html

作者:Vivek Gite 譯者:amwps290 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

相關文章