Linux 运行级别

147 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第11天,点击查看活动详情


title: Linux 运行级别 tags: [Linux] categories: [Linux]

运行级别Runlevel)指的是Unix或者Linux等类Unix操作系统下不同的运行模式,本文记录相关内容。

运行级别

  • 运行级别Runlevel)指的是Unix或者Linux等类Unix操作系统下不同的运行模式。运行级别通常分为7等,分别是从0到6。

  • 在大多数linux操作系统下一共有如下6个典型的运行级别:

    • 0 停机

    • 1 单用户,Does not configure network interfaces, start daemons, or allow non-root logins

    • 2 多用户,无网络连接 Does not configure network interfaces or start daemons

    • 3 多用户,启动网络连接 Starts the system normally.

    • 4 用户自定义

    • 5 多用户带图形界面

    • 6 重启

一般情况下我们处于的运行级在3或者5,切换至 0 就是关机, 6 就是重启

  • 用户在同一时间只能处于一种运行级别下,但是可以相互切换

查看级别

在Ubuntu下可以通过

$runlevel

查看当前运行级别

$ runlevel
N 5

切换级别

telinit num

或者

init num

切换运行级别。

例如可以通过以下命令重启:

telinit 6   
# or
init 6

可运行脚本

  • 查看/etc/init.d这个文件夹下,就是可以运行的脚本:
$ ll /etc/init.d/
总用量 232
drwxr-xr-x   2 root root 4096 11 16 13:17 ./
drwxr-xr-x 137 root root 8192 11 16 13:17 ../
-rwxr-xr-x   1 root root 2269 11 28  2019 acpid*
-rwxr-xr-x   1 root root 5574 11  5  2019 alsa-utils*
-rwxr-xr-x   1 root root 2055 7  17  2019 anacron*
-rwxr-xr-x   1 root root 3740 4   1  2020 apparmor*
-rwxr-xr-x   1 root root 2964 12  7  2019 apport*
-rwxr-xr-x   1 root root 2401 8  21  2018 avahi-daemon*
-rwxr-xr-x   1 root root 2968 2  26  2020 bluetooth*
-rwxr-xr-x   1 root root 1232 3  27  2020 console-setup.sh*
-rwxr-xr-x   1 root root 3059 2  11  2020 cron*
-rwxr-xr-x   1 root root 2804 2  17  2020 cups*
-rwxr-xr-x   1 root root 1961 4  10  2020 cups-browsed*
-rwxr-xr-x   1 root root 3152 9  30  2019 dbus*
-rwxr-xr-x   1 root root 3033 11 10  2020 gdm3*
-rwxr-xr-x   1 root root  985 8  12 17:18 grub-common*
-rwxr-xr-x   1 root root 3809 7  29  2019 hwclock.sh*
-rwxr-xr-x   1 root root 2638 12 13  2019 irqbalance*
-rwxr-xr-x   1 root root 3131 5  19  2017 kerneloops*
-rwxr-xr-x   1 root root 1479 11 27  2019 keyboard-setup.sh*
-rwxr-xr-x   1 root root 2044 2  19  2020 kmod*
-rw-r--r--   1 root root    0 11 16 13:17 .legacy-bootordering
-rwxr-xr-x   1 root root  695 1  28  2020 lvm2*
-rwxr-xr-x   1 root root  586 1  28  2020 lvm2-lvmpolld*
-rwxr-xr-x   1 root root 1942 9  17  2020 network-manager*
-rwxr-xr-x   1 root root 5658 5  13  2021 nfs-common*
-rwxr-xr-x   1 root root 4836 5  25 04:51 nfs-kernel-server*
-rwxr-xr-x   1 root root 9138 7  20 03:26 openvpn*
-rwxr-xr-x   1 root root 1366 3  23  2020 plymouth*
-rwxr-xr-x   1 root root  752 3  23  2020 plymouth-log*
-rwxr-xr-x   1 root root  612 2   9  2019 pppd-dns*
-rwxr-xr-x   1 root root  924 2  14  2020 procps*
-rwxr-xr-x   1 root root  469 10 16  2020 pulseaudio-enable-autospawn*
...
  • 在/etc/rc.d/rcN.d文件夹中,可以分别查看各个运行级别的相对应的文件:

这6种模式还有许多功能,比如说如果忘记了root密码就可以进入单用户模式,在启动后的提示符界面下输入init = /bin/sh rw就进入了模式1,把root文件挂为读写就可以跳过系统认证,直接用passwd程序来更改root用户口令了,然后再启动到正常的运行级去即可

参考资料