CentOS 7/8修改系统运行级别
CentOS 5、 CentOS 6可以通过修改配置文件/etc/inittab 参数,从而修改系统运行级别,切换系统的runlevel,但是从CentOS 7开始,不能用这个命令了,只能由systemd统一管理,并且减少到了两个等级(当然这里不包含poweroff.target、reboot.target等)。/etc/inittab中也明确inittab不再使用了。
CentOS 7下的inittab文件
注意:”systemd uses 'targets' instead of runlevels. By default, there are two main targets“这条信息
[root@localhost ~]# more /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 #
CentOS 8下的inittab文件
注意:”systemd uses 'targets' instead of runlevels. By default, there are two main targets“这条信息
[root@localhost ~]# cat /etc/inittab # inittab is no longer used. # # 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
系统运行级别都定义在/lib/systemd/system下:
[root@localhost ~]# ll /usr/lib/systemd/system/runlevel* lrwxrwxrwx. 1 root root 15 6月 18 17:06 /usr/lib/systemd/system/runlevel0.target -> poweroff.target lrwxrwxrwx. 1 root root 13 6月 18 17:06 /usr/lib/systemd/system/runlevel1.target -> rescue.target lrwxrwxrwx. 1 root root 17 6月 18 17:06 /usr/lib/systemd/system/runlevel2.target -> multi-user.target lrwxrwxrwx. 1 root root 17 6月 18 17:06 /usr/lib/systemd/system/runlevel3.target -> multi-user.target lrwxrwxrwx. 1 root root 17 6月 18 17:06 /usr/lib/systemd/system/runlevel4.target -> multi-user.target lrwxrwxrwx. 1 root root 16 6月 18 17:06 /usr/lib/systemd/system/runlevel5.target -> graphical.target lrwxrwxrwx. 1 root root 13 6月 18 17:06 /usr/lib/systemd/system/runlevel6.target -> reboot.target /usr/lib/systemd/system/runlevel1.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 6月 18 17:06 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service /usr/lib/systemd/system/runlevel2.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 6月 18 17:06 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service /usr/lib/systemd/system/runlevel3.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 6月 18 17:06 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service /usr/lib/systemd/system/runlevel4.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 6月 18 17:06 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service /usr/lib/systemd/system/runlevel5.target.wants: 总用量 0 lrwxrwxrwx. 1 root root 39 6月 18 17:06 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service [root@localhost ~]#
从上图可以看到runlevel2.target、runlevel3.target、runlevel4.target 这些软连接都是指向multi-user.target
通过命令来查看运行级别:
[root@localhost ~]# systemctl get-default # 查看默认运行级别
multi-user.target
[root@localhost ~]# runlevel N 3
通过命令来设置运行级别:
systemd命令修改: #systemctl set-default TARGET.target 修改软连接方式: # ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
测试例子如下:
例1:
[root@db-server tmp]# systemctl set-default graphical.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target. [root@db-server tmp]#
例2:
[root@kerrydb ~]# systemctl get-default graphical.target [root@kerrydb ~]# runlevel N 5 [root@kerrydb ~]# systemctl set-default multi-user.target Removed /etc/systemd/system/default.target. Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/multi-user.target. [root@kerrydb ~]# systemctl get-default multi-user.target [root@kerrydb ~]#
例3:
[root@kerrydb ~]# systemctl get-default multi-user.target [root@kerrydb ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target [root@kerrydb ~]# systemctl get-default multi-user.target [root@kerrydb ~]# ln -sf /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target [root@kerrydb ~]# systemctl get-default multi-user.target
# 可设置的运行级别
runlevel0.target -> poweroff.target runlevel1.target -> rescue.target runlevel2.target -> multi-user.target runlevel3.target -> multi-user.target runlevel4.target -> multi-user.target runlevel5.target -> graphical.target runlevel6.target -> reboot.target
修改后需要重启服务器才能生效。否则只对当前连接生效。