21 管理服务-改变运行级别
21.1 Target和运行级别
systemd用目标(target)替代了运行级别的概念,提供了更大的灵活性,如您可以继承一个已有的目标,并添加其他服务,来创建自己的目标。表7列举了systemd下的目标和常见runlevel的对应关系。
表 7 运行级别和systemd目标
| 运行级别 | systemd目标(target) | 描述 | 
|---|---|---|
| runlevel0.target,poweroff.target | 关闭系统。 | |
| 1, s, single | runlevel1.target,rescue.target | 单用户模式。 | 
| 2, 4 | runlevel2.target,runlevel4.target,multi-user.target | 用户定义/域特定运行级别。默认等同于3。 | 
| 3 | runlevel3.target,multi-user.target | 多用户,非图形化。用户可以通过多个控制台或网络登录。 | 
| 5 | runlevel5.target,graphical.target | 多用户,图形化。通常为所有运行级别3的服务外加图形化登录。 | 
| 6 | runlevel6.target,reboot.target | 重启系统。 | 
| emergency | emergency.target | 紧急Shell | 
21.2 查看系统默认启动目标
查看当前系统默认的启动目标,命令如下:
# systemctl get-default
例如示例命令如下:
[root@superman-21 ~]# systemctl get-default
multi-user.target
[root@superman-21 ~]# 
21.3 查看当前系统所有的启动目标
查看当前系统所有的启动目标,命令如下:
# systemctl list-units --type=target
例如示例命令如下:
[root@superman-21 ~]# systemctl list-units --type=target
  UNIT                  LOAD   ACTIVE SUB    DESCRIPTION                        
  basic.target          loaded active active Basic System
  bluetooth.target      loaded active active Bluetooth Support
  getty.target          loaded active active Login Prompts
  local-fs-pre.target   loaded active active Preparation for Local File Systems
  local-fs.target       loaded active active Local File Systems
  multi-user.target     loaded active active Multi-User System
  network-online.target loaded active active Network is Online
  network-pre.target    loaded active active Preparation for Network
  network.target        loaded active active Network
  nfs-client.target     loaded active active NFS client services
  paths.target          loaded active active Path Units
  remote-fs-pre.target  loaded active active Preparation for Remote File Systems
  remote-fs.target      loaded active active Remote File Systems
  rpc_pipefs.target     loaded active active rpc_pipefs.target
  rpcbind.target        loaded active active RPC Port Mapper
  slices.target         loaded active active Slice Units
  sockets.target        loaded active active Socket Units
  sound.target          loaded active active Sound Card
  sshd-keygen.target    loaded active active sshd-keygen.target
  swap.target           loaded active active Swaps
  sysinit.target        loaded active active System Initialization
  timers.target         loaded active active Timer Units
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
22 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
[root@superman-21 ~]# 
21.4 改变默认目标
改变系统默认的目标,在root权限下执行如下命令:
# systemctl set-default name.target
例如示例命令如下:
[root@superman-21 ~]# systemctl set-default multi-user.target
[root@superman-21 ~]# 
21.5 改变当前目标
改变当前系统的目标,在root权限下执行如下命令:
# systemctl isolate name.target
例如示例命令如下:
[root@superman-21 ~]# systemctl isolate rescue.target
[root@superman-21 ~]#
21.6 切换到救援模式
改变当前系统为救援模式,在root权限下执行如下命令:
# systemctl rescue
例如示例命令如下:
[root@superman-21 ~]# systemctl rescue
[root@superman-21 ~]# 
这条命令和“systemctl isolate rescue.target”类似。命令执行后会在串口有如下打印信息:
You are in rescue mode. After logging in, type "journalctl -xb" to viewsystem logs, systemctl reboot" to reboot, "systemctl default" or "exit"to boot into default mode.
Give root password for maintenance
(or press Control-D to continue):
说明:
用户需要重启系统,从救援模式进入正常模式。
21.7 切换到紧急模式
改变当前系统为紧急模式,在root权限下执行如下命令:
# systemctl emergency
这条命令和“systemctl isolate emergency.target”类似。命令执行后会在串口有如下打印信息:
You are in emergency mode. After logging in, type "journalctl -xb" to viewsystem logs, systemctl reboot" to reboot, "systemctl default" or "exit"to boot into default mode.
Give root password for maintenance
(or press Control-D to continue):
例如示例命令如下:
[root@superman-21 ~]# systemctl emergency
[root@superman-21 ~]# 
 说明:
用户需要重启系统,从紧急模式进入正常模式。
👍 点赞,你的认可是我创作的动力!
⭐️ 收藏,你的青睐是我努力的方向!
✏️ 评论,你的意见是我进步的财富!











