Linux: Check the current run level of the five methods

Run level is Linux functional level operating system currently running. There are seven run levels, numbered from 0-6. The system can be directed to any given run level. Run level is identified by a number.

Each run level specified different system configurations and allows different combinations of access to the process. By default, Linux will boot to run level 3 or level 5 to run. Do only a run level at startup. It will not be executed one by one.
The default system run level is specified in the / etc / inittab file SysVinit system.
But systemd system does not read this file, it uses the following files /etc/systemd/system/default.target to get the default run level information.
We can use the following five ways to check the Linux system is currently running level.
runlevelCommand : runlevela printing system and the current operating level.
whoCommand : print information about the currently logged-on user. It will use the " -r" option to print run level information.
systemctlCommand: It controls the systemdsystem and service manager.
Use /etc/inittabfile: default run level of the system in SysVinit Systemthe /etc/inittabfile specified.
Use /etc/systemd/system/default.targetfile: default run level of the system in systemd Systemthe /etc/systemd/system/default.targetfile specified.
Detailed operational level information described in the table below.
Linux: Five ways to check the current run level of Linux: Five ways to check the current level of operation
The system will run according to the level of implementation of the program / service.

1, for SysVinit system, it performs the following positions:
Run Level 0 - /etc/rc.d/rc0.d/ 

Run Level 1 - /etc/rc.d/rc1.d/ 
run level 2 - /etc/rc.d/rc2.d/ 
run level 3 - / etc / rc.d / rc3.d / 
run level 4 - /etc/rc.d/rc4.d/ 
run level 5 - /etc/rc.d/rc5.d/ 
run level 6 - /etc/rc.d /rc6.d/
2, for systemd system, it performs the following positions:
runlevel1.target – /etc/systemd/system/rescue.target
runlevel2.target – /etc/systemd/system/multi-user.target.wants
runlevel3.target – /etc/systemd/system/multi-user.target.wants
runlevel4.target – /etc/systemd/system/multi-user.target.wants
runlevel5.target – /etc/systemd/system/graphical.target.wants
1, the use of runlevel command

runlevelAnd is currently running on a level printing system:

[Linuxidc linuxidc.com @ localhost] $ runlevel

5 N
1], N: "N" represents the system up and running since the level has not been changed.
2], 5: "5" indicates that the current run level of the system.
Linux: Five ways to check the current run level of Linux: Five ways to check the current level of operation

2, using the who command

打印有关当前登录用户的信息,它将使用-r选项打印运行级别信息:
[linuxidc@localhost linuxidc.com]$ who -r
运行级别 5

3、使用systemctl命令

systemctl用于控制systemd系统和服务管理器,systemd是Unix操作系统的系统和服务管理器。它可以作为sysvinit系统的直接替代品,systemd是内核启动并保持PID 1的第一个进程。systemd使用.service文件而不是bash脚本(SysVinit使用),systemd将所有守护进程排序到他们自己的Linux cgroup中,可以通过浏览/cgroup/systemd文件来查看系统层次结构:

[linuxidc@localhost linuxidc.com]$ systemctl get-default
graphical.target
4、使用/etc/inittab文件

系统的默认运行级别在SysVinit System/etc/inittab文件中指定,但systemd不读取文件,因此,它仅适用于SysVinit系统而不适用于systemd系统。

CentOS 6是通过修改/etc/inittab来修改默认运行级别的:
[root@localhost ~]# vim /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:5:initdefault:
在CentOS 7下运行vim /etc/inittab结果如下:
[linuxidc@localhost linuxidc.com]$ vim /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 6中设置默认运行级别的方式。
注释内容大意是说,

# Multi-user.target 3 similar runlevel; 
# graphical.target similar runlevel5

Get the current mode is the default run level

systemctl get-default

Set the default run level way

systemctl set-default TARGET.target

Set run level command format:

systemctl [command] [unit.target]

Commands need  systemctl
to set the default level is a run command: systemctl set-default xxx
the level corresponds to the system is

init target level systemctl 
0 shutdown.target 
. 1 emergency.target 
2 rescure.target 
. 3 multi-user.target 
. 4 no 
. 5 graphical.target 
. 6 None

Note: The above commands require super administrator privileges, if you need to temporarily switch directly  init + need to switch to digital.

5, using /etc/systemd/system/default.target file

The default system run level specified in systemd System of /etc/systemd/system/default.target file, it does not apply to SysVinit system:

[Linuxidc linuxidc.com @ localhost] $ cat /etc/systemd/system/default.target

Guess you like

Origin www.cnblogs.com/linuxprobe-sarah/p/10941686.html