查看当前的运行级别,Ubuntu 桌面默认是2。
$ runlevel
Ubuntu 的系统运行级别:
0 系统停机状态 1 单用户或系统维护状态 2~5 多用户状态 6 重新启动 S
切换运行级别,执行命令:
# init [0123456Ss]
即在 init 命令后跟一个参数,此参数是要切换到的运行级的运行级代号,如:用 init 0 命令关机;用 init 6 命令重新启动。
在Debian Policy Manual中对此有如下讲解:
init.d
scriptsThe /etc/init.d
directory contains the scripts executed by init
at boot time and when the init state (or "runlevel") is changed (see init(8)
).
There are at least two different, yet functionally equivalent, ways of handling these scripts. For the sake of simplicity, this document describes only the symbolic link method. However, it must not be assumed by maintainer scripts that this method is being used, and any automated manipulation of the various runlevel behaviors by maintainer scripts must be performed using update-rc.d
as described below and not by manually installing or removing symlinks. For information on the implementation details of the other method, implemented in the file-rc package, please refer to the documentation of that package.
These scripts are referenced by symbolic links in the /etc/rcn.d
directories. When changing runlevels, init
looks in the directory /etc/rcn.d
for the scripts it should execute, where n is the runlevel that is being changed to, or S for the boot-up scripts.
The names of the links all have the form Smmscript
or Kmmscript
where mm is a two-digit number and script is the name of the script (this should be the same as the name of the actual script in/etc/init.d
).
When init
changes runlevel first the targets of the links whose names start with a K are executed, each with the single argument stop, followed by the scripts prefixed with an S, each with the single argument start. (The links are those in the /etc/rcn.d
directory corresponding to the new runlevel.) The K links are responsible for killing services and the S link for starting services upon entering the runlevel.
For example, if we are changing from runlevel 2 to runlevel 3, init will first execute all of the K prefixed scripts it finds in /etc/rc3.d
, and then all of the S prefixed scripts in that directory. The links starting with K will cause the referred-to file to be executed with an argument of stop, and the S links with an argument of start.
The two-digit number mm is used to determine the order in which to run the scripts: low-numbered links have their scripts run first. For example, the K20 scripts will be executed before the K30scripts. This is used when a certain service must be started before another. For example, the name server bind
might need to be started before the news server inn
so that inn
can set up its access lists. In this case, the script that starts bind
would have a lower number than the script that starts inn
so that it runs first:
/etc/rc2.d/S17bind /etc/rc2.d/S70inn
The two runlevels 0 (halt) and 6 (reboot) are slightly different. In these runlevels, the links with an S prefix are still called after those with a K prefix, but they too are called with the single argumentstop.
A number of other init systems are available now in Debian that can be used in place of sysvinit
. Alternative init implementations must support running SysV init scripts for compatibility.
Packages may integrate with these replacement init systems by providing implementation-specific configuration information about how and when to start a service or in what order to run certain tasks at boot time. However, any package integrating with other init systems must also be backwards-compatible with sysvinit
by providing a SysV-style init script with the same name as and equivalent functionality to any init-specific job, as this is the only start-up configuration method guaranteed to be supported by all init implementations. An exception to this rule is scripts or jobs provided by the init implementation itself; such jobs may be required for an implementation-specific equivalent of the /etc/rcS.d/
scripts and may not have a one-to-one correspondence with the init scripts.
Packages may integrate with the upstart
event-based boot system by installing job files in the /etc/init
directory. SysV init scripts for which an equivalent upstart job is available must query the output of the command initctl version
for the string upstart and avoid running in favor of the native upstart job, using a test such as this:
if [ "$1" = start ] && which initctl >/dev/null && initctl version | grep -q upstart then exit 1 fi
Because packages shipping upstart jobs may be installed on systems that are not using upstart, maintainer scripts must still use the common update-rc.d
and invoke-rc.d
interfaces for configuring runlevels and for starting and stopping services. These maintainer scripts must not call the upstart start
, restart
, reload
, or stop
interfaces directly. Instead, implementations of invoke-rc.d
must detect when upstart is running and when an upstart job with the same name as an init script is present, and perform the requested action using the upstart job instead of the init script.
更多内容请参考:
$ man 7 runlevel
$ man 5 init
$ man 8 init