首页 > 代码库 > CentOS 7 运行级别切换

CentOS 7 运行级别切换

CentOS 7 之前的版本是通过 /etc/inittab 文件来定义系统运行级别:

[sywu@wusuyuan ~]$ cat /etc/centos-release 
CentOS release 6.4 (Final)
[sywu@wusuyuan ~]$ cat /etc/inittab 
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
# 
id:3:initdefault:
[sywu@wusuyuan ~]$

 

CentOS 7 版本不再使用该文件定义系统运行级别,相关运行级别设置无效:

[sywu@c1-11r2-rac1 etc]$ cat /etc/centos-release 
CentOS Linux release 7.0.1406 (Core) 
[sywu@c1-11r2-rac1 etc]$ 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 /etc/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 set a default target, run:
#
# ln -sf /lib/systemd/system/<target name>.target /etc/systemd/system/default.target
#
id:3:initdefault:  #无效
[sywu@c1-11r2-rac1 etc]$

新版本的运行级别都定义在 /lib/systemd/system下:

[sywu@c1-11r2-rac1 system]$ ls -ltr /lib/systemd/system/runlevel*
lrwxrwxrwx. 1 root root 13 7月  30 14:22 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 15 7月  30 14:22 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 17 7月  30 14:22 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 7月  30 14:22 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 7月  30 14:22 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 7月  30 14:22 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 7月  30 14:22 /lib/systemd/system/runlevel6.target -> reboot.target

可以针对不同需要设置不同的运行级别:

如设置命令行级别(init 3)方法:

ln -sf /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target
或
ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
或
systemctl set-default multi-user.target

设置窗口级别(init 5)方法:

ln -sf /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target
或
ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
或
systemctl set-default graphical.target

 

-----------------------------分割线-----------------------------

修改系统运行级别:
1、systemd使用比sysvinit的运行级更为自由的target替代。第3运行级用multi-user.target替代。第5运行级用graphical.target替代。runlevel3.target和runlevel5.target分别是指向 multi-user.target和graphical.target的符号链接。
可以使用下面的命令切换到“运行级别3 ”:
systemctl isolate multi-user.target或systemctl isolate runlevel3.target

可以使用下面的命令切换到“运行级别5 ”:
systemctl isolate graphical.target或systemctl isolate runlevel5.target
2、如何改变默认运行级别?
systemd使用链接来指向默认的运行级别。在创建新的链接前,可以通过下面命令删除存在的链接: rm /etc/systemd/system/default.target
默认启动运行级别3 :
ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

默认启动运行级别5 :
ln -sf /lib/systemd/system/graphical.target/etc/systemd/system/default.target

systemd不使用/etc/inittab文件。

-----------------------------分割线-----------------------------

如何查看当前运行级别?
旧的runlevel命令在systemd下仍然可以使用。可以继续使用它,尽管systemd使用 ‘target’ 概念(多个的 ‘target’ 可以同时激活)替换了之前系统的runlevel。
等价的systemd命令是systemctl list-units –type=target

CentOS 7 运行级别切换