Home  >  Article  >  Operation and Maintenance  >  In which file are linux runlevels defined?

In which file are linux runlevels defined?

青灯夜游
青灯夜游Original
2022-05-12 18:19:473309browse

The Linux run level is defined in the "/etc/inittab" configuration file. "/etc/inittab" is the configuration file used by the init initialization program when Linux initializes the file system; this configuration file can only be used to set the default run level of the system, which is the run level the system will enter after booting.

In which file are linux runlevels defined?

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What is a run level?

In fact, Linux uses run levels to determine which services are started when the system starts.

Linux has 7 run levels by default, as shown in Table 1.

Table 1 Run Level
          Run level             Meaning
            0           Shut down
          1 Single-user mode, which can be imagined as the safe mode of Windows, is mainly used for system repair
            2 Incomplete command line mode, does not include NFS service
              3           The complete command line mode is the standard character interface
            4           System reserved
5             Graphics mode
          6             Restart

In which file is the linux run level defined?

The linux run level is defined in the "/etc/inittab" configuration file middle.

/etc/inittab The configuration file can only be used to set the default run level of the system, that is, the run level the system will enter after booting up

/etc/inittab file Describes which processes start up and run normally. inittab is the configuration file used by the init initialization program when Linux initializes the file system. This file is responsible for setting where the init initialization program initialization script is; the commands to be run when each run level is initialized; the commands corresponding to boot, shutdown, and restart; and the commands to be run when each run level logs in.

The format of the entries in the inittab file is as follows:

label:runlevels:action:process

The following describes each field

1.label

label is a unique sequence that identifies an entry 1 - 4 characters, some systems only support 2 character labels. For other login program items such as getty or mingetty, the label and tty numbers are required to be the same, otherwise the getty program will not work properly.

Commonly used tags are:

id 用来定义缺省的init运行的级别
si 是系统初始化的进程
ln 其中的n从1~6,指明该进程可以使用的runlevel的级别
ud 是升级进程
ca 指明当按下Ctrl+Alt+Del时运行的进程
pf 指当UPS表明断电时运行的进程
pr 是在系统真正关闭之前,UPS发出电源恢复的信号时需要运行的进程
x 是将系统转入X终端时需要运行的进程

2.runlevels

runlevels lists the run levels and specifies the actions that should be taken . Multiple runlevel levels can be specified, or no specific value can be specified for the runlevel field.

runlevel 0是让init关闭所有进程并终止系统。
runlevel 1是用来将系统转到单用户模式,单用户模式只能有系统管理员进入,在该模式下处理那些在有登录用户的情况下不能进行更改的文件,改runlevel的编号1也可以用S代替。
runlevel 2是允许系统进入多用户的模式,但并不支持文件共享,这种模式很少应用。
runlevel 3是最常用的运行模式,主要用来提供真正的多用户模式,也是多数服务器的缺省模式。
runlevel 4一般不被系统使用,用户可以设计自己的系统状态并将其应用到runlevel 4阶段,尽管很少使用,但使用该系统可以实现一些特定的登录请求。
runlevel 5是将系统初始化为专用的X Window终端。对功能强大的Linux系统来说,这并不是好的选择,但用户如果需要这样,也可以通过在runlevel启动来实现该方案。
runlevel 6是关闭所有运行的进程并重新启动系统。

Debian6 and later do not run the /etc/inittab file by default. The default level is 2-5 and classified into one category

3.action

The action field defines the state in which the process should run and describes how subsequent processes will run. :

respawn 不管何时终止都重新启动进程
wait 运行进程等待输入运行模式   这个过程将会开始一次运行级别时指定输入和init将等待其终止
once 每一个runlevel级别运行一次  这个过程将执行一次当指定输入runlevel
boot 在系统启动时运行,忽略runlevel
bootwait 在系统启动时运行,init等待进程完成。忽略runlevel
ctrlaltdel 当Ctrl+Alt+Del三个键同时按下时运行,把SIGINT信号发送给init。忽略 runlevel
initdefault 不要执行这个进程,它用于设置默认runlevel
kbrequest 当init从键盘中收到信号时运行。这里要求键盘组合符合KeyBoardSigra
off 禁止进入,因此该进程不运行
once 每一个runlevel级别运行一次
ondemand 当系统指定特定的运行级别A、B、C时运行
powerfail 当init收到SIGPWR信号时运行
powerokwait 当收到SIGPWD信号且/etc/文件中的电源状态包含OK时运行
powerwait 当收到SIGPWD信号,并且init等待进程结束时运行
sysinit 在运行boot或bootwait进程之前运行

4.process

The process field contains the process executed by init. The process adopts the same format as running the process under the command line. The format is the same, so the process field starts with the name of the process, followed by the parameters to be passed to the process at runtime.

The code is as follows:

# /etc/inittab: init(8) configuration.
# $Id: inittab,v 1.91 2002/01/25 13:35:21 miquels Exp $

The code is as follows:

# The default runlevel.
id:2:initdefault: #表示当前缺省运行级别为2

The code is as follows:

# Boot-time system configuration/initialization script.
# This is run first except when booting in emergency (-b) mode.
si::sysinit:/etc/init.d/rcS #系统初始化的进程

The code is as follows:

# What to do in single-user mode.
~~:S:wait:/sbin/sulogin

The code is as follows:

# /etc/init.d executes the S and K scripts upon change
# of runlevel.
#
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.

The code is as follows:

l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
# Normally not reached, but fallthrough in case of emergency.
z6:6:respawn:/sbin/sulogin

The code is as follows:

# What to do when CTRL-ALT-DEL is pressed. #在启动过程中是否允许按CTRL-ALT-DELETE重启系统
#ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

The code is as follows:

# Action on special keypress (ALT-UpArrow).
#kb::kbrequest:/bin/echo "Keyboard Request--edit /etc/inittab to let this work."

The code is as follows:

# What to do when the power fails/returns.
pf::powerwait:/etc/init.d/powerfail start
pn::powerfailnow:/etc/init.d/powerfail now
po::powerokwait:/etc/init.d/powerfail stop

The code is as follows:

# /sbin/getty invocations for the runlevels.
#
# The "id" field MUST be the same as the last
# characters of the device (after "tty").
#
# Format:
# 53384f78b45ee9f1e3082cf378b9c5b4:aa8e9a9ffd796aa413e03363b25da911:26e39fa7e1b26005f564b20d47361531:22cd36f119203ea5e8c594b34aeec209
#
# Note that on most Debian systems tty7 is used by the X Window System,
# so if you want to add more getty's go ahead but skip tty7 if you run X.
# getty进程来提供虚拟终端设备的服务
1:2345:respawn:/sbin/getty 38400 tty1

The code is as follows:

#标签字段的值是1,1是设备tty1的数字后缀,tty1与相应的进程相关联,该getty进程可以启动的runlevel是2,3,4和5,当该进程终止时,init马上就重新启动它。启动进程的路径名是/sbin/getty,该进程是实现虚拟终端支持的最小版本的getty,为tty1提供启动虚拟设备的进程。
2:23:respawn:/sbin/getty 38400 tty2
#3:23:respawn:/sbin/getty 38400 tty3
#4:23:respawn:/sbin/getty 38400 tty4
#5:23:respawn:/sbin/getty 38400 tty5
#6:23:respawn:/sbin/getty 38400 tty6
# Example how to put a getty on a serial line (for a terminal)
#
#T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100
T1:23:respawn:/sbin/getty -L ttyS1 115200 vt100
# Example how to put a getty on a modem line.
#
#T3:23:respawn:/sbin/mgetty -x0 -s 57600 ttyS3

Example

# Begin /etc/inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/init.d/rc sysinit
l0:0:wait:/etc/rc.d/init.d/rc 0
l1:S1:wait:/etc/rc.d/init.d/rc 1
l2:2:wait:/etc/rc.d/init.d/rc 2
l3:3:wait:/etc/rc.d/init.d/rc 3
l4:4:wait:/etc/rc.d/init.d/rc 4
l5:5:wait:/etc/rc.d/init.d/rc 5
l6:6:wait:/etc/rc.d/init.d/rc 6
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now
su:S016:once:/sbin/sulogin
1:2345:respawn:/sbin/agetty tty1 9600
2:2345:respawn:/sbin/agetty tty2 9600
3:2345:respawn:/sbin/agetty tty3 9600
4:2345:respawn:/sbin/agetty tty4 9600
5:2345:respawn:/sbin/agetty tty5 9600
6:2345:respawn:/sbin/agetty tty6 9600
# End /etc/inittab

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of In which file are linux runlevels defined?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn