Linux - 系统运行级别

linux系统运行级别

Centos, Ubuntu系统运行级别、开机启动脚本的说明

Linux系统运行级别

Centos 运行级别 target信息 说明
0 poweroff.target 系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
1, S rescue.target 单用户工作状态,只有root可以login,用于系统修复,重置密码信息, 没有网络, 禁止远程登陆
2 multi-user.target 状态(没有NFS), 没有网络
3 multi-user.target 完全的多用户状态(有NFS),登陆后进入控制台命令行模式, 有网络
4 multi-user.target 系统未使用,保留
5 graphical.target X11控制台,登陆后进入图形GUI模式
6 reboot.target 系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

查看运行级别:

$ runlevel            
$ systemctl get-default       

查看target以及依赖independency信息

  1. **查看可用target****

    除了以上的运行级别外, linux系统还提供了70多种额外的targets, 大多数是以上5种级别的子集

    **daniel@daniel-lib-ubuntu:~$ systemctl list-units --type=target **

daniel@daniel-lib-ubuntu:~$ systemctl list-units --type=target 
  UNIT                   LOAD   ACTIVE SUB    DESCRIPTION                  
  basic.target           loaded active active Basic System                 
  cryptsetup.target      loaded active active Local Encrypted Volumes      
  getty.target           loaded active active Login Prompts                
  graphical.target       loaded active active Graphical Interface          
  local-fs-pre.target    loaded active active Local File Systems (Pre)     
  local-fs.target        loaded active active Local File Systems           
  multi-user.target      loaded active active Multi-User System            
  network-online.target  loaded active active Network is Online            
  network.target         loaded active active Network                      
  nss-lookup.target      loaded active active Host and Network Name Lookups
  nss-user-lookup.target loaded active active User and Group Name Lookups  
  paths.target           loaded active active Paths                        
  remote-fs.target       loaded active active Remote File Systems          
  slices.target          loaded active active Slices                       
  sockets.target         loaded active active Sockets                      
  swap.target            loaded active active Swap                         
  sysinit.target         loaded active active System Initialization        
  time-set.target        loaded active active System Time Set              
  time-sync.target       loaded active active System Time Synchronized     
  timers.target          loaded active active Timers                       

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

20 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
  1. 查看target需要的依赖服务

    daniel@daniel-lib-ubuntu:~$ systemctl list-dependencies multi-user.target

    绿点: service 或 target 处于运行(running)或活跃(active)状态

    白点: service 或 target 处于非活跃状态(inactive or dead)
    红点: service 或 target 启动失败或有致命错误

list-dependencies.png
  1. 查询runlevels对应的targets
    daniel@daniel-lib-ubuntu:~$ ls -l /usr/lib/systemd/system/runlevel{0..6}.target
daniel@daniel-lib-ubuntu:~$ ls -l /usr/lib/systemd/system/runlevel*target
lrwxrwxrwx 1 root root 15 Jan  7 04:47 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx 1 root root 13 Jan  7 04:47 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx 1 root root 17 Jan  7 04:47 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx 1 root root 17 Jan  7 04:47 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx 1 root root 17 Jan  7 04:47 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx 1 root root 16 Jan  7 04:47 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx 1 root root 13 Jan  7 04:47 /usr/lib/systemd/system/runlevel6.target -> reboot.target

临时调整runlevel级别:

init 3
telinit 3
systemctl isolate multi-user.target  #isolate 动态改变对应target:  开启对应级别依赖,并立即停止新级别中不会使用的进程 参考 man systemctl

永久设置runlevel级别

centos 6
vim /etc/inittab
centos 7 , ubuntu
systemctl set-default multi-user.target 

ubuntu自启动脚本目录/文件

daniel@daniel-lib-ubuntu:~$ ll /etc |grep rc..d
drwxr-xr-x   2 root root    4096 Mar 31 20:58 rc0.d/
drwxr-xr-x   2 root root    4096 Mar 31 20:58 rc1.d/
drwxr-xr-x   2 root root    4096 Mar 31 20:58 rc2.d/
drwxr-xr-x   2 root root    4096 Mar 31 20:58 rc3.d/
drwxr-xr-x   2 root root    4096 Mar 31 20:58 rc4.d/
drwxr-xr-x   2 root root    4096 Mar 31 20:58 rc5.d/
drwxr-xr-x   2 root root    4096 Mar 31 20:58 rc6.d/
drwxr-xr-x   2 root root    4096 Jul 30  2020 rcS.d/
lrwxrwxrwx   1 root root    27 Jul 30  2020 K01speech-dispatcher -> ../init.d/speech-dispatcher*
lrwxrwxrwx   1 root root    15 Jul 30  2020 S01acpid -> ../init.d/acpid*
lrwxrwxrwx   1 root root    17 Jul 30  2020 S01anacron -> ../init.d/anacron*
lrwxrwxrwx   1 root root    16 Jul 30  2020 S01apport -> ../init.d/apport*
lrwxrwxrwx   1 root root    22 Jul 30  2020 S01avahi-daemon -> ../init.d/avahi-daemon*
lrwxrwxrwx   1 root root    19 Jul 30  2020 S01bluetooth -> ../init.d/bluetooth*
lrwxrwxrwx   1 root root    26 Jul 30  2020 S01console-setup.sh -> ../init.d/console-setup.sh*
lrwxrwxrwx   1 root root    14 Jul 30  2020 S01cron -> ../init.d/cron*

说明:

  • rc.0.d~rc.t6.d , rcS.d对应系统0-6个运行状态需要执行的脚本, 目录下的每个文件都是/etc/init.d/目录下文件的一个软链接, 命名规则为K+nn+服务名或S+nn+服务名,其中nn为两位数字

  • /etc/init.d/ 目录存放了所有开机自启动脚本

  • K, S 为init.d下的脚本传递了参数, K 传递 stop, S传递 start , 可以看到rc0.d中全部文件都以K开头.停止了所有服, 实现了关机

  • K, S后的数字 用来决定脚本执行顺序(从小到大)

  • 关于没有/etc/rc.local 文件: Ubuntu 16.04 版本开始去除了 rc.local 文件,自启动服务方面基本由 systemd 全面接管

    实际上Ubuntu20.04 存在rc-local.service文件 位置为: /lib/systemd/system/rc-local.service, 用户可以自行创建rc.local

    步骤如下:

    1.  $sudo vim /etc/rc.local
      
    2. #!/bin/sh
         
      # 这里添加要开机执行的脚本和命令等等
         
      exit 0
      
    3. 添加执行权限

      $ sudo chmod +x /etc/rc.local
      
    4. 创建软链接

      $ sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
      
    5. 重启系统使rc.local生效

参考:
https://blog.csdn.net/qq_41782149/article/details/89001226
https://www.answertopia.com/ubuntu/managing-ubuntu-systemd-units/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。