RHEL/CentOS 7的systemd target及其中的multi-user.target

本文介绍了RHEL/CentOS 7如何从传统的Runlevels转换到systemd targets,特别是multi-user.target。这个target在系统启动后默认激活,类似于之前的Runlevel 3。通过systemctl命令可以查看和管理target,如设置默认target。理解systemd targets有助于更好地管理系统服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在RHEL/CentOS 6中,使用SysV init和Upstart,通过预定义一组Runlevels(从0到6)表示不同的执行模式。

[root@myhost app]# ll /etc/rc.d/
total 4
drwxr-xr-x. 2 root root 148 May 28 05:40 init.d
-rwxr-xr-x. 1 root root 503 May 27 11:29 rc.local
drwxr-xr-x. 2 root root  84 Jan 16 08:07 rc0.d
drwxr-xr-x. 2 root root  84 Jan 16 08:07 rc1.d
drwxr-xr-x. 2 root root  84 Jan 16 08:07 rc2.d
drwxr-xr-x. 2 root root  84 Jan 16 08:07 rc3.d
drwxr-xr-x. 2 root root  84 Jan 16 08:07 rc4.d
drwxr-xr-x. 2 root root  84 Jan 16 08:07 rc5.d
drwxr-xr-x. 2 root root  84 Jan 16 08:07 rc6.d

对于选定的一个Runlevel,执行该Runlevel关联的所有服务,对应/etc/rc.d/init.d/目录下的初始文件,从而定制Linux系统的启动过程。管理员可以通过service命令或chkconfig命令工具进行操作。

事实上,RHEL/CentOS 6中的Runlevels表示的是系统的一组状态。在RHEL/CentOS 7中,Runlevels的概念已经被systemd target替代,而/etc/rc.d/init.d/目录下的初始文件也已经被service units所替代。

systemd target是一种systemd unit,用以描述系统的一个状态或一个同步点(快照)。一个systemd target拥有一个以.target为后缀的unit配置文件,位于/usr/lib/systemd/system/路径下。一个systemd target往往只是一组相关的systemd units的集合,用以将Linux系统设置为某个状态,类似于CentOS 6的Runlevel。CentOS 6的Runlevels与CentOS 7的targets之间的对应关系如下:

Traditional runlevel      New target name     Symbolically linked to target

Runlevel 0           |    runlevel0.target -> poweroff.target
Runlevel 1           |    runlevel1.target -> rescue.target
Runlevel 2           |    runlevel2.target -> multi-user.target
Runlevel 3           |    runlevel3.target -> multi-user.target
Runlevel 4           |    runlevel4.target -> multi-user.target
Runlevel 5           |    runlevel5.target -> graphical.target
Runlevel 6           |    runlevel6.target -> reboot.target

1. multi-user.target

systemd有一个默认target,即multi-user.target,Linux系统启动后即处于该默认target的状态。

Linux系统启动时,各个target之间的依赖关系如下图所示:

查看multi-user.target的unit配置文件,执行命令systemctl cat multi-user.target:

[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes

说明:其中Requires给出的basic.target是另一个target,Requires表示multi-user.target依赖于basic.target。
           Conflicts给出的rescure.service rescue.target,表示multi-user.target与他们互斥,不能同时处于multi-user状态和rescue状态。
           After表示multi-user.target在basic.target rescue.service rescue.target之后启动,如果有他们的话。显然,如果启动了 rescue.service rescue.target则表示进入rescue状态,则必然无法启动multi-user.target,从而就不可能进入multi-user状态了。

2. 常见的target相关的systemctl 操作

    查看systemd的默认target:
        systemctl get-default
    查看systemd的所有可用targets:
        systemctl list-unit-files --type=target
    查看systemd的所有启动targets:
        systemctl list-units --type=target
    设置systemd的默认target:
        systemctl set-default graphical.target
 

参考链接:

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/system_administrators_guide/index#sect-Managing_Services_with_systemd-Targets

https://www.freedesktop.org/software/systemd/man/bootup.html#System%20Manager%20Bootup

https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值