关机

命令 | 说明
—|—
shutdown -h now | 立刻关机(生产常用),shutdown命令是发送命令至系统,切换init进程调整到运行级别0
shutdown -h +1 | 1分钟后关机
shutdown -h 11:00 | 11点关机
init 0 | 切换到运行级别0,0表示关机,具体可以查看/etc/inittab
halt | 立即停止系统,但是需要人工关闭电源,因为halt是reboot的链接文件,是reboot的一个参数使用
poweroff | 立即停止系统,并关闭电源

重启

命令 | 说明
—|—
reboot | 立即重启(生产常用)
shutdown -r now | 立即重启(生产常用)
shutdown -r +1 | 一分钟后重启
shutdown -r 11:00 | 11点重启
init 6 | 切换到运行级别6,即重启

注销

命令 | 说明
—|—
logout | 注销,退出当前用户
exit | 注销,退出当前用户
ctrl + d | 登出当前用户

FAQ

1、关于系统运行级别,可以参考命令runlevel

[root@localhost ~]# runlevel
N 3

查看/etc/inittab文件

[root@localhost tables]# cat /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target

上述翻译为:

inittab在使用systemd时不再使用。

systemd使用“目标”而不是运行级别。默认情况下,有两个主要目标:

multi-user.target:多用户目标,类似于运行级别3;

graphical.target:图形目标,类似于运行级别5。

要查看当前默认目标,运行命令:

systemctl get-default

要设置默认目标,运行:

systemctl set-default TARGET.target

2、shutdown、halt、poweroff有什么区别?

halt 是强制关机,需要手动关闭电源;

而poweroff 、shutdown 相当于windows的关机命令,先关闭系统然后关闭电源。

shutdown是一个安全关闭命令,也是生产常用命令,在系统关闭之前,会通知所有登录用户,系统即将关闭,此时所有新用户都不可以登录。

可以用ls -l 命令看下

lrwxrwxrwx. 1 root root 16 9月  30 23:55 /sbin/halt -> ../bin/systemctl
[root@localhost ~]# ls -l /sbin/poweroff
lrwxrwxrwx. 1 root root 16 9月  30 23:55 /sbin/poweroff -> ../bin/systemctl
[root@localhost ~]# ls -l /sbin/shutdown
lrwxrwxrwx. 1 root root 16 9月  30 23:55 /sbin/shutdown -> ../bin/systemctl

可以看到,在CentOS 7上,halt、shutdown、poweroff都是指向systemctl的软连接

但是在在CentOS 6上,是直接指向reboot的。

在CentOS 7上,也可以从帮助文档看到他们的关系

[root@localhost tables]# halt --help
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
[root@localhost tables]# poweroff --help
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

[root@localhost tables]# shutdown --help
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
[root@localhost tables]# reboot --help
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

一般这些参数多不常用,看最上面的常用命令就ok了。