linux关闭系统是有序终止所有进程,断电前执行重要的内务整理工作,以下五个命令可以完成这项工作:
halt
reboot
poweroff
shutdow
init
reboot
reboot [OPTIONS...] [ARG]
Reboot the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
reboot -p #调用 halt 或 poweroff 命令,切断设备电源。
reboot -d #仅重启,不写/var/log/wtmp记录
poweroff
poweroff [OPTIONS...]
Power off the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
例如:
poweroff -w #不关闭主机,仅在/var/log/wtmp中写入记录
poweroff -f #强制关机
poweroff -d #仅关机,不写/var/log/wtmp记录
shutdown
shutdown [OPTIONS...] [TIME] [WALL...]
Shut down the system.
--help Show this help
-H --halt Halt the machine
-P --poweroff Power-off the machine
-r --reboot Reboot the machine
-h Equivalent to --poweroff, overridden by --halt
-k Don't halt/power-off/reboot, just send warnings
--no-wall Don't send wall message before halt/power-off/reboot
-c Cancel a pending shutdown
例如:
shutdown -r -t 10 #10s后重启系统
shutdown -c #取消之前的showdown操作
shutdown -h now #立即关机
shutdown -h 17:30 #停止系统的服务,在17:30关机
[root@09385 ~]# shutdown -k 10:00 #通知在线用户10:00将要关机
Shutdown scheduled for 日 2022-08-07 10:00:00 CST, use 'shutdown -c' to cancel.
halt
halt [OPTIONS...]
Halt the system.
--help Show this help
--halt Halt the machine
-p --poweroff Switch off the machine
--reboot Reboot the machine
-f --force Force immediate halt/power-off/reboot
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
-d --no-wtmp Don't write wtmp record
--no-wall Don't send wall message before halt/power-off/reboot
注:halt用法参照poweroff或reboot用法
init
#init 0 - 停机(千万不能把initdefault 设置为0 )
#init 1 - 单用户模式,只root用户进行维护
#init 2 - 多用户,不能使用NFS(Net File System) 不联网
#init 3 - 完全多用户模式(标准的运行级)
#init 4 - 安全模式
#init 5 - X11 (xwindow) 图形化界面模式
#init 6 - 重新启动 (千万不要把initdefault 设置为6 )
wtmp文件内容查看
在介绍前面的命令时,提到关闭系统操作会不会记录在/var/log/wtmp中(wtmp中还会记录用户登录也操作),wtmp是二进制文件,目的是防止人为恶意篡改,那么如果查看这个文件中的内容呢,使用last命令看查看其中的记录
[root@09385 ~]# last --help
last: invalid option -- '-'
Usage: last [-num | -n num] [-f file] [-t YYYYMMDDHHMMSS] [-R] [-adioxFw] [username..] [tty..]
例如:
[root@09385 ~]# last
root pts/1 172.17.84.169 Sat Aug 6 21:42 still logged in
root pts/0 172.17.84.169 Sat Aug 6 21:42 still logged in
root pts/1 172.17.84.169 Sat Aug 6 21:25 - 21:35 (00:10)
root pts/0 172.17.84.169 Sat Aug 6 21:25 - 21:35 (00:10)
reboot system boot 5.10.0-202204291 Sun Aug 7 05:05 - 21:42 (-7:-23)
root pts/1 172.17.84.169 Sat Aug 6 20:59 - crash (08:06)
root pts/0 172.17.84.169 Sat Aug 6 20:59 - crash (08:06)
reboot system boot 5.10.0-202204291 Sun Aug 7 01:15 - 21:42 (-3:-33)
root pts/1 172.17.84.169 Sat Aug 6 17:13 - down (00:00)
root pts/0 172.17.84.169 Sat Aug 6 17:13 - down (00:00)
root pts/1 172.17.84.169 Fri Aug 5 14:15 - 14:25 (00:10)
root pts/0 172.17.84.169 Fri Aug 5 14:15 - 14:25 (00:10)
root pts/1 172.17.85.72 Fri Aug 5 13:39 - 13:44 (00:04)
root pts/0 172.17.85.72 Fri Aug 5 13:39 - 13:44 (00:04)
[root@09385 ~]#last root
root pts/1 172.17.84.169 Sat Aug 6 21:42 still logged in
root pts/0 172.17.84.169 Sat Aug 6 21:42 still logged in
root pts/1 172.17.84.169 Sat Aug 6 21:25 - 21:35 (00:10)
root pts/0 172.17.84.169 Sat Aug 6 21:25 - 21:35 (00:10)
root pts/1 172.17.84.169 Sat Aug 6 20:59 - crash (08:06)
root pts/0 172.17.84.169 Sat Aug 6 20:59 - crash (08:06)
root pts/1 172.17.84.169 Sat Aug 6 17:13 - down (00:00)
root pts/0 172.17.84.169 Sat Aug 6 17:13 - down (00:00)
root pts/1 172.17.84.169 Fri Aug 5 14:15 - 14:25 (00:10)
root pts/0 172.17.84.169 Fri Aug 5 14:15 - 14:25 (00:10)
root pts/1 172.17.85.72 Fri Aug 5 13:39 - 13:44 (00:04)
root pts/0 172.17.85.72 Fri Aug 5 13:39 - 13:44 (00:04)
如果你不想这些记录被别人查到怎么整?
清空登录记录
echo 1 >/var/log/wtmp
清空操作记录
history -c