我们在前面的课程中多次提到Linux系统的运行级别,runleve 那么 runlevel 是什么呢?Linux默认有7个运行级,从运行级0到运行级6,每一个运行级对应的含义如下:
运行级0:关机
运行级1:单用户模式,系统出现问题时,可以通过这种模式进行维护,典型的应用场景就是忘记 root 密码时可以进入此模式修改 root 密码。
运行级2:多用户模式,但是没有网络连接。
运行级3:完全多用户模式,这也是 linux系统最常见的运行级。
运行级4:保留未使用。
运行级5:窗口模式,支持多用户,支持网络。
运行级6:重启。
任何时候,Linux 只能在一种 runlevel 下运行。
系统在启动的过程中,会根据在 /etc/inittab 中的设定读取 runlevel 的数值X,并相应地读取和运行 /etc/rcX.d/ ( X 代表0~6 ) 下所有脚本。
在我们的实验环境下,运行 ll /etc/rc3.d 可以看到,我们的系统在3 这个运行级下面的启动脚本。
root@ubuntu:~# ll /etc/rc3.d/
total 20
drwxr-xr-x 2 root root 4096 Sep 25 00:02 ./
drwxr-xr-x 130 root root 12288 Oct 4 23:08 ../
-rw-r--r-- 1 root root 677 Feb 16 2016 README
lrwxrwxrwx 1 root root 20 Sep 9 01:25 S20kerneloops -> ../init.d/kerneloops*
lrwxrwxrwx 1 root root 15 Sep 9 01:25 S20rsync -> ../init.d/rsync*
lrwxrwxrwx 1 root root 24 Sep 25 00:02 S20screen-cleanup -> ../init.d/screen-cleanup*
lrwxrwxrwx 1 root root 27 Sep 9 01:25 S20speech-dispatcher -> ../init.d/speech-dispatcher*
lrwxrwxrwx 1 root root 15 Sep 9 01:25 S50saned -> ../init.d/saned*
lrwxrwxrwx 1 root root 19 Sep 9 01:25 S70dns-clean -> ../init.d/dns-clean*
lrwxrwxrwx 1 root root 18 Sep 9 01:25 S70pppd-dns -> ../init.d/pppd-dns*
lrwxrwxrwx 1 root root 21 Sep 9 01:25 S99grub-common -> ../init.d/grub-common*
lrwxrwxrwx 1 root root 18 Sep 9 01:25 S99ondemand -> ../init.d/ondemand*
lrwxrwxrwx 1 root root 18 Sep 9 01:25 S99rc.local -> ../init.d/rc.local*
可以通过查看 README 文件来查看关于文件夹下面脚本的说明:
root@ubuntu:~# cat /etc/rc3.d/README
The scripts in this directory are executed each time the system enters
this runlevel.
The scripts are all symbolic links whose targets are located in
/etc/init.d/ .
To disable a service in this runlevel, rename its script in this
directory so that the new name begins with a 'K' and a two-digit
number, and run 'update-rc.d script defaults' to reorder the scripts
according to dependencies. A warning about the current runlevels
being enabled not matching the LSB header in the init.d script will be
printed. To re-enable the service, rename the script back to its
original name beginning with 'S' and run update-rc.d again.
For a more information see /etc/init.d/README.
首先,所有可以执行的程序都存储在 /etc/init.d/,当前文件夹下面的都是到init.d 下面可执行文件的连接。
其次,系统首先运行 K 开头,并跟着两个数字再跟着服务名称的连接脚本。其次是S开头的脚本,K和S 的意思分别是 kill 和 start。
再次,在同一个字母开头的脚本中,会按照数字小的先运行,数字大的后运行的顺序进行。
eg : 运行级1 和运行级3 的对比:
root@ubuntu:~# ll /etc/rc1.d/
total 20
drwxr-xr-x 2 root root 4096 Sep 25 00:02 ./
drwxr-xr-x 130 root root 12288 Oct 4 23:45 ../
lrwxrwxrwx 1 root root 20 Sep 9 01:25 K20kerneloops -> ../init.d/kerneloops*
lrwxrwxrwx 1 root root 15 Sep 9 01:25 K20rsync -> ../init.d/rsync*
lrwxrwxrwx 1 root root 15 Sep 9 01:25 K20saned -> ../init.d/saned*
lrwxrwxrwx 1 root root 24 Sep 25 00:02 K20screen-cleanup -> ../init.d/screen-cleanup*
lrwxrwxrwx 1 root root 27 Sep 9 01:25 K20speech-dispatcher -> ../init.d/speech-dispatcher*
-rw-r--r-- 1 root root 369 Mar 12 2014 README
lrwxrwxrwx 1 root root 19 Sep 9 01:25 S30killprocs -> ../init.d/killprocs*
lrwxrwxrwx 1 root root 19 Sep 9 01:25 S70dns-clean -> ../init.d/dns-clean*
lrwxrwxrwx 1 root root 18 Sep 9 01:25 S70pppd-dns -> ../init.d/pppd-dns*
lrwxrwxrwx 1 root root 16 Sep 9 01:25 S90single -> ../init.d/single*
root@ubuntu:~# ll /etc/rc3.d/
total 20
drwxr-xr-x 2 root root 4096 Sep 25 00:02 ./
drwxr-xr-x 130 root root 12288 Oct 4 23:45 ../
-rw-r--r-- 1 root root 677 Feb 16 2016 README
lrwxrwxrwx 1 root root 20 Sep 9 01:25 S20kerneloops -> ../init.d/kerneloops*
lrwxrwxrwx 1 root root 15 Sep 9 01:25 S20rsync -> ../init.d/rsync*
lrwxrwxrwx 1 root root 24 Sep 25 00:02 S20screen-cleanup -> ../init.d/screen-cleanup*
lrwxrwxrwx 1 root root 27 Sep 9 01:25 S20speech-dispatcher -> ../init.d/speech-dispatcher*
lrwxrwxrwx 1 root root 15 Sep 9 01:25 S50saned -> ../init.d/saned*
lrwxrwxrwx 1 root root 19 Sep 9 01:25 S70dns-clean -> ../init.d/dns-clean*
lrwxrwxrwx 1 root root 18 Sep 9 01:25 S70pppd-dns -> ../init.d/pppd-dns*
lrwxrwxrwx 1 root root 21 Sep 9 01:25 S99grub-common -> ../init.d/grub-common*
lrwxrwxrwx 1 root root 18 Sep 9 01:25 S99ondemand -> ../init.d/ondemand*
lrwxrwxrwx 1 root root 18 Sep 9 01:25 S99rc.local -> ../init.d/rc.local*