如何在Linux系統(tǒng)查詢機(jī)器最近重啟時(shí)間
在你的 Linux 或類 UNIX 系統(tǒng)中,你是如何查詢系統(tǒng)上次重新啟動(dòng)的日期和時(shí)間?怎樣顯示系統(tǒng)關(guān)機(jī)的日期和時(shí)間? last
命令不僅可以按照時(shí)間從近到遠(yuǎn)的順序列出該會(huì)話的特定用戶、終端和主機(jī)名,而且還可以列出指定日期和時(shí)間登錄的用戶。輸出到終端的每一行都包括用戶名、會(huì)話終端、主機(jī)名、會(huì)話開始和結(jié)束的時(shí)間、會(huì)話持續(xù)的時(shí)間。要查看 Linux 或類 UNIX 系統(tǒng)重啟和關(guān)機(jī)的時(shí)間和日期,可以使用下面的命令。
last
命令who
命令
使用 who 命令來(lái)查看系統(tǒng)重新啟動(dòng)的時(shí)間/日期
你需要在終端使用 who 命令來(lái)打印有哪些人登錄了系統(tǒng),who
命令同時(shí)也會(huì)顯示上次系統(tǒng)啟動(dòng)的時(shí)間。使用 last
命令來(lái)查看系統(tǒng)重啟和關(guān)機(jī)的日期和時(shí)間,運(yùn)行:
$ who -b
示例輸出:
system boot 2017-06-20 17:41
使用 last
命令來(lái)查詢最近登錄到系統(tǒng)的用戶和系統(tǒng)重啟的時(shí)間和日期。輸入:
$ 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
命令通過(guò)查看文件 /var/log/wtmp
來(lái)顯示自 wtmp 文件被創(chuàng)建時(shí)的所有登錄(和登出)的用戶。每當(dāng)系統(tǒng)重新啟動(dòng)時(shí),這個(gè)偽用戶 reboot
就會(huì)登錄。因此,last reboot
命令將會(huì)顯示自該日志文件被創(chuàng)建以來(lái)的所有重啟信息。
查看系統(tǒng)上次關(guān)機(jī)的時(shí)間和日期
可以使用下面的命令來(lái)顯示上次關(guān)機(jī)的日期和時(shí)間:
$ last -x|grep shutdown | head -1
示例輸出:
shutdown system down 2.6.15.4 Sun Apr 30 13:31 - 15:08 (01:37)
命令中,
-x
:顯示系統(tǒng)關(guān)機(jī)和運(yùn)行等級(jí)改變信息
這里是 last
命令的其它的一些選項(xiàng):
$ last
$ last -x
$ last -x reboot
$ last -x shutdown
示例輸出:
Fig.01: How to view last Linux System Reboot Date/Time
查看系統(tǒng)正常的運(yùn)行時(shí)間
評(píng)論區(qū)的讀者建議的另一個(gè)命令如下:
$ uptime -s
示例輸出:
2017-06-20 17:41:51
OS X/Unix/FreeBSD 查看最近重啟和關(guān)機(jī)時(shí)間的命令示例
在終端輸入下面的命令:
$ last reboot
在 OS X 示例輸出結(jié)果如下:
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
查看關(guān)機(jī)日期和時(shí)間,輸入:
$ 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
如何查看是誰(shuí)重啟和關(guān)閉機(jī)器?
你需要啟用 psacct 服務(wù)然后運(yùn)行下面的命令來(lái)查看執(zhí)行過(guò)的命令(包括用戶名),在終端輸入 lastcomm 命令查看信息
# lastcomm userNameHere
# lastcomm commandNameHere
# lastcomm | more
# lastcomm reboot
# lastcomm shutdown
### 或者查看重啟和關(guān)機(jī)時(shí)間
# 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 用戶在當(dāng)?shù)貢r(shí)間 12 月 27 日星期二 23:49 在 pts/0 重新啟動(dòng)了機(jī)器。
參見(jiàn)
- 更多信息可以查看 man 手冊(cè)(
man last
)和參考文章 如何在 Linux 服務(wù)器上使用 tuptime 命令查看歷史和統(tǒng)計(jì)的正常的運(yùn)行時(shí)間。
關(guān)于作者
作者是 nixCraft 的創(chuàng)立者,同時(shí)也是一名經(jīng)驗(yàn)豐富的系統(tǒng)管理員,也是 Linux,類 Unix 操作系統(tǒng) shell 腳本的培訓(xùn)師。他曾與全球各行各業(yè)的客戶工作過(guò),包括 IT,教育,國(guó)防和空間研究以及非營(yíng)利部門等等。你可以在 Twitter、Facebook、Google+ 關(guān)注他。