1.3 Linux关机重启注销命令
1.3.1 重启和关机命令:shutdown
【功能说明】
shutdown是一个用来安全关闭或重启Linux系统的命令,系统在关闭之前会通知所有的登录用户,系统即将关闭,此时所有的新用户都不可以再登陆了,与shutdown功能类似的命令还有init、halt、poweroff、reboot。
【语法格式】
shutdown [OPTIONS...] [TIME] [WALL...]
[选项] [时间] [消息]
技巧说明:
1)注意shutdown命令与后面的选项之间至少要有一个空格。
2)通常情况下,我们执行的shutdown命令为shutdown -h now或shutdown -r now。
【选项参数】
参数选项及说明见表1-5。
表1-5 shutdown命令的参数选项及说明
说明:表中到*符号的为重点说明。
shutdown命令的工作过程就是当用户执行了对应的参数并附带关机时间的命令之后,其会通知所有的用户即将关机,并且在这个时间内禁止用户登录;当到了指定的关机时间时,shutdown命令会根据参数选项,发送请求给系统的init进程,请求将系统调整到对应的参数状态(例如,“-h”参数),系统的关机状态实际上对应的是Linux系统里的运行级别0。与系统关机相关的运行级别有:0(关机运行级别)-halt,6(重启运行级别) -reboot。更多信息请查看/etc/inittab文件。
【实践操作】
**范例1-2:**关机或重启系统的场景操作
一分钟后关闭Linux系统的命令如下:
[root@centos7 ~]# shutdown -h +1 #1分钟后关闭Linux系统。
Shutdown scheduled for Mon 2020-10-05 15:34:22 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from root@oldboyedu2 (Mon 2020-10-05 15:33:22 CST): #通知所有用户关机信息。
The system is going down for power-off at Mon 2020-10-05 15:34:22 CST! #关机时间提示。
^C #按Ctrl+c快捷键取消。
上述代码中,结尾的“+1”表示的是关机的时间段,即1分钟后关机,当然,你也可以改为5分钟后关机,这个时间段是以当下系统时间为准来计算的,时间段也可以改为具体时间点。
shutdown命令的工作原理如下:一旦到达关机时间,那么shutdown命令会发送请求给系统的init进程将系统调整到合适的运行级别(运行级别命令请参考runlevel命令),其中,0表示关机,6表示重启。所以执行“init 0”就表示关机,执行“init 6”就表示重启。
11点整重启Linux系统的命令如下:
[root@centos7 ~]# shutdown -r 11:00
Shutdown scheduled for Tue 2020-10-06 11:00:00 CST, use 'shutdown -c' to cancel.
其中,结尾的11:00表示的是关机的时间点,比如说下午19:00我要和一个女生约会,那么19:00就是一个时间点。本命令相当于是11:00的时候告诉进场把运行级别调整为6,及相当于执行了“init 6”命令。
立即关闭Linux系统的命令如下:
[root@centos7 ~]# shutdown -h now
在工作中,一般使用得较多的都是立即关闭系统命令。
1.3.2 关机与重启命令:halt/poweroff/reboot/systemctl
【功能说明】
halt、poweroff、reboot这三个命令对应的是同一个man帮助文档,而halt、poweroff、shutdown、reboot命令都是systemctl命令的链接文件(CentOS 7)。
[root@centos7 ~]# ll `which reboot` `which poweroff` `which halt` `which shutdown`
lrwxrwxrwx. 1 root root 16 Oct 1 16:04 /usr/sbin/halt -> ../bin/systemctl
lrwxrwxrwx. 1 root root 16 Oct 1 16:04 /usr/sbin/poweroff -> ../bin/systemctl
lrwxrwxrwx. 1 root root 16 Oct 1 16:04 /usr/sbin/reboot -> ../bin/systemctl
lrwxrwxrwx. 1 root root 16 Oct 1 16:04 /usr/sbin/shutdown -> ../bin/systemctl
【语法格式】
halt [option]...
poweroff [option]...
reboot [option]...
systemctl [command] #CentOS 7的独有命令。
对于上述这几个命令的参数,由于实在是没有什么价值,因此,这里就不为读者介绍了。
【实践操作】
**范例1-3:**关机或重启系统的常见操作
使用halt关机的命令如下:
[root@centos7 ~]# halt #在CentOS 7中,此处改为了systemctl halt。
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(centos7-10.0.0.201) at 15:51:09.
Type `help' to learn how to use Xshell prompt.
使用poweroff关机的命令如下:
[root@centos7 ~]# poweroff #在CentOS 7中,此处改为了systemctl poweroff。
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(centos7-10.0.0.201) at 16:02:33.
Type `help' to learn how to use Xshell prompt.
使用 systemctl reboot重启系统的命令如下:
[root@centos7 ~]# systemctl reboot 在CentOS 6中,此处改为了reboot。
Connection closing...Socket close.
Connection closed by foreign host.
Disconnected from remote host(centos7-10.0.0.201) at 16:05:04.
Type `help' to learn how to use Xshell prompt.
CentOS 7系统中,systemctl有关关机、重启功能的命令小结如表1-6所示。
提示:CentOS 7里新增的systemctl命令功能十分强大,我们后续再慢慢与大家详解相关内容。
1.3.3 关机、重启和注销的命令列表
本章结尾为读者总结了Linux(CentOS 6和CentOS 7都使用)下常见的关机、重启、注销等命令。并标注了企业在常用命令,具体见表1-7。
表1-7 Linux下常见的关机、重启、注销命令集合