代码改变世界

CentOS 7/8修改系统运行级别

2020-07-03 13:42  潇湘隐者  阅读(864)  评论(0编辑  收藏

CentOS 7/8修改系统运行级别

 

CentOS 5 CentOS 6可以通过修改配置文件/etc/inittab 参数,从而修改系统运行级别,切换系统的runlevel,但是从CentOS 7开始,不能用这个命令了,只能由systemd统一管理,并且减少到了两个等级(当然这里不包含poweroff.targetreboot.target等)。/etc/inittab中也明确inittab不再使用了。

 

CentOS 7下的inittab文件

 

注意:systemd uses 'targets' instead of runlevels. By default, there are two main targets这条信息

 

[root@db-server ~]# 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@kerrydb ~]# 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@db-server ~]# ll /usr/lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 Aug  2 00:11 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Aug  2 00:11 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Aug  2 00:11 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Aug  2 00:11 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Aug  2 00:11 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Aug  2 00:11 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Aug  2 00:11 /usr/lib/systemd/system/runlevel6.target -> reboot.target
 
/usr/lib/systemd/system/runlevel1.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Aug  2 00:11 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
 
/usr/lib/systemd/system/runlevel2.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Aug  2 00:11 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
 
/usr/lib/systemd/system/runlevel3.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Aug  2 00:11 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
 
/usr/lib/systemd/system/runlevel4.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Aug  2 00:11 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service
 
/usr/lib/systemd/system/runlevel5.target.wants:
total 0
lrwxrwxrwx. 1 root root 39 Aug  2 00:11 systemd-update-utmp-runlevel.service -> ../systemd-update-utmp-runlevel.service

 

clip_image001[6]

 

从上图可以看到runlevel2.targetrunlevel3.targetrunlevel4.target 这些软连接都是指向multi-user.target

 

[root@kerrydb ~]# ll /usr/lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 15 Apr 24 11:53 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Apr 24 11:53 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Apr 24 11:53 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Apr 24 11:53 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Apr 24 11:53 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Apr 24 11:53 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Apr 24 11:53 /usr/lib/systemd/system/runlevel6.target -> reboot.target
 
/usr/lib/systemd/system/runlevel1.target.wants:
total 0
 
/usr/lib/systemd/system/runlevel2.target.wants:
total 0
 
/usr/lib/systemd/system/runlevel3.target.wants:
total 0
 
/usr/lib/systemd/system/runlevel4.target.wants:
total 0
 
/usr/lib/systemd/system/runlevel5.target.wants:
total 0

clip_image002[6]

 

 

通过命令来查看运行级别:

 

# systemctl get-default  # 查看默认运行级别

 

# runlevel

 

 

通过命令来设置运行级别:

 

 

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

 

修改后需要重启服务器才能生效。否则只对当前连接生效。