zoukankan      html  css  js  c++  java
  • CentOS统的7个运行级别的含义

    原文: http://blog.csdn.net/liansehai/article/details/45370965

    CentOS系统有7个运行级别(runlevel)

    运行级别就是操作系统当前正在运行的功能级别。级别是从0到6,具有不同的功能。这些级别定义在/ect/inittab文件中。这个文件是init程序寻找的主要文件,最先运行的服务是那些放在/etc/rc.d目录下的文件。

    #查看系统运行级别
    [root@bin2ctyun ~]# cat /etc/inittab
    # inittab is only used by upstart for the default runlevel.
    #
    # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
    #
    # System initialization is started by /etc/init/rcS.conf
    #
    # Individual runlevels are started by /etc/init/rc.conf
    #
    # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
    #
    # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
    # with configuration in /etc/sysconfig/init.
    #
    # For information on how to write upstart event handlers, or how
    # upstart works, see init(5), init(8), and initctl(8).
    #
    # Default runlevel. The runlevels used are:
    #   0 - halt (Do NOT set initdefault to this)
    #   运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
    #   1 - Single user mode
    #   运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
    #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
    #   运行级别2:多用户状态(没有NFS)
    #   3 - Full multiuser mode
    #   运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式
    #   4 - unused
    #   运行级别4:系统未使用,保留
    #   5 - X11
    #   运行级别5:X11控制台,登陆后进入图形GUI模式
    #   6 - reboot (Do NOT set initdefault to this)
    #   运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动
    # 
    id:3:initdefault:
    # 表示运行级别3,是默认的。

    运行级别的原理: 1、在目录/etc/rc.d/init.d下有许多服务器脚本程序,一般称为服务(service)

    [root@bin2ctyun init.d]# ls /etc/rc.d/init.d/
    auditd                    halt          lvm2-monitor  nginx    rdisc        single
    blk-availability          ip6tables     mysql         ntpd     restorecond  sshd
    cloud-set-guest-password  iptables      netconsole    ntpdate  rsyslog      sysstat
    crond                     killall       netfs         php-fpm  sandbox      udev-post
    functions                 lvm2-lvmetad  network       postfix  saslauthd    xe-linux-distribution

    2、在/etc/rc.d下有7个名为rcN.d的目录,对应系统的7个运行级别

    [root@bin2ctyun init.d]# ls /etc/rc.d/
    init.d  rc  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local  rc.sysinit

    3、rcN.d目录下都是一些符号链接文件,这些链接文件都指向init.d目录下的service脚本文件,命名规则为K+nn+服务名或S+nn+服务名,其中nn为两位数字。

    [root@bin2ctyun rc3.d]# ll /etc/rc.d/rc3.d
    总用量 0
    lrwxrwxrwx  1 root root 19 11月 14 09:35 K10saslauthd -> ../init.d/saslauthd
    lrwxrwxrwx. 1 root root 20 11月 13 16:22 K50netconsole -> ../init.d/netconsole
    lrwxrwxrwx  1 root root 14 3月  16 13:53 K74ntpd -> ../init.d/ntpd
    lrwxrwxrwx  1 root root 17 3月  16 13:53 K75ntpdate -> ../init.d/ntpdate
    lrwxrwxrwx. 1 root root 21 11月 13 16:22 K87restorecond -> ../init.d/restorecond
    lrwxrwxrwx  1 root root 15 11月 14 09:34 K89rdisc -> ../init.d/rdisc
    lrwxrwxrwx  1 root root 18 3月  25 12:20 K92iptables -> ../init.d/iptables
    lrwxrwxrwx  1 root root 17 4月   1 22:36 S01sysstat -> ../init.d/sysstat
    lrwxrwxrwx  1 root root 22 3月  25 12:20 S02lvm2-monitor -> ../init.d/lvm2-monitor
    lrwxrwxrwx  1 root root 19 3月  25 12:20 S08ip6tables -> ../init.d/ip6tables
    lrwxrwxrwx  1 root root 17 3月  25 12:18 S10network -> ../init.d/network
    lrwxrwxrwx. 1 root root 16 11月 13 16:23 S11auditd -> ../init.d/auditd
    lrwxrwxrwx  1 root root 17 3月  25 12:19 S12rsyslog -> ../init.d/rsyslog
    lrwxrwxrwx. 1 root root 31 11月 14 09:05 S14xe-linux-distribution -> ../init.d/xe-linux-distribution
    lrwxrwxrwx  1 root root 26 3月  25 12:20 S25blk-availability -> ../init.d/blk-availability
    lrwxrwxrwx  1 root root 15 3月  25 12:18 S25netfs -> ../init.d/netfs
    lrwxrwxrwx  1 root root 19 3月  25 12:18 S26udev-post -> ../init.d/udev-post
    lrwxrwxrwx  1 root root 17 4月   1 22:36 S50php-fpm -> ../init.d/php-fpm
    lrwxrwxrwx  1 root root 15 3月  16 16:07 S55nginx -> ../init.d/nginx
    lrwxrwxrwx  1 root root 14 3月  25 12:20 S55sshd -> ../init.d/sshd
    lrwxrwxrwx  1 root root 15 3月  16 15:24 S64mysql -> ../init.d/mysql
    lrwxrwxrwx  1 root root 17 4月   1 22:36 S80postfix -> ../init.d/postfix
    lrwxrwxrwx  1 root root 15 4月   1 22:36 S90crond -> ../init.d/crond
    lrwxrwxrwx  1 root root 34 11月 14 09:29 S98cloud-set-guest-password -> ../init.d/cloud-set-guest-password
    lrwxrwxrwx  1 root root 11 3月  25 12:18 S99local -> ../rc.local

    4、系统会根据指定的运行级别进入对应的rcN.d目录,并按照文件名顺序检索目录下的链接文件

    • 对于以K开头的文件,系统将终止对应的服务

    • 对于以S开头的文件,系统将启动对应的服务

    5、查看运行级别用:runlevel

    [root@bin2ctyun rc3.d]# runlevel
    N 3

    6、进入其它运行级别用:init N

    7、注意如果输入 init 0 则为关机,init 6为重启系统

    Linux运行级别的配置

    cat /etc/inittab

    可以看到最后一行 id:3:initdefault: 各字段解释如下: id:runlevels:action:process

    id:是一个任意指定的四个字符以内的序列标号,在本文件内必须唯一;使用老版本的libc5(低于5.2.18)或a.out库编译出来的sysvinit限制为2字符。注意:像getty之类的登陆进程必须使id字段与tty编号一致,如tty1需要id=1,许多老版本的登陆进程都遵循这种规则。

    runlevels:表示这一行适用于运行那个/些级别(这里是3,可以有多个,表示在相应的运行级均需要运行);另外sysinit、boot、bootwait这三个进程会忽略这个设置值。

    action:表示进入对应的runlevels时,init应该运行process字段的命令的方式,常用的字段值及解释在附录内。例子中的wait表示需要运行这个进程一次并等待其结束。

    process:具体应该执行的命令。例子中的/etc/init.d/rc命令启动运行级别3中应该运行的进程/命令,并负责在退出运行级时将其终止(当然在进入的runlevel中仍要运行的程序除外。)当运行级别改变,并且正在运行的程序并没有在新的运行级别中指定需要运行,那么init会先发送一个SIGTERM信号终止,然后是SIGKILL。

    有效的action值如下:

    respawn:表示init应该监视这个进程,即使其结束后也应该被重新启动。
    wait:init应该运行这个进程一次,并等待其结束后再进行下一步操作。 once:init需要运行这个进程一次。 boot:随系统启动运行,所以runlevel值对其无效。 bootwait:随系统启动运行,并且init应该等待其结束。 off: 没有任何意义。
    initdefault:系统启动后的默认运行级别;由于进入相应的运行级别会激活对应级别的进程,所以对其指定process字段没有任何意义。如果inittab文件内不存在这一条记录,系统启动时在控制台上询问进入的运行级。 sysinit:系统启动时准备运行的命令。比如说,这个命令将清除/tmp。可以查看/etc/rc.d/rc.sysinit脚本了解其运行了那些操作。 powerwait:允许init在电源被切断时,关闭系统。当然前提是有U P S和监视U P S并通知init电源已被切断的软件。RH linux默认没有列出该选项。 powerfail:同powerwait,但init不会等待正在运行的进程结束。RH linux默认没有列出该选项。 powerokwait:当电源监视软件报告“电源恢复”时,init要执行的操作。
    powerfailnow:检测到ups电源即将耗尽时,init要执行的操作,和powerwait/powerfail不同的哟。
    ctrlaltdel:允许init在用户于控制台键盘上按下C t r l + A l t + Del组合键时,重新启动系统。注意,如果该系统放在一个公共场所,系统管理员可将C t r l + A l t + Del组合键配置为别的行为,比如忽略等。我是设置成打印一句骂人的话了^o^。 kbrequest:监视到特定的键盘组合键被按下时采取的动作,现在还不完善。 ondemand:A process marked with an ondemand runlevel will be executed whenever the specifiedondemand runlevel is called. However, no runlevel change will occur(ondemand runlevels are ‘a’, ‘b’,and ‘c’

    补救措施:

    如果您的机器进入了这样一种状态,系统由于损坏的/etc/inittab而不能boot,由于受损的/etc/passwd而不能登入,或者干脆就是忘记了口令。怎么办?

    法1: 在LILO boot 提示符下键入 linux 1 而boot进入单用户模式,这是一个几乎没有装载任何服务的状态,您可以在提供的shell下执行修复命令。

    法2: 在启动后的 lilo 提示符下输入: init=/bin/sh rw 就可以使机器进入运行级1 , 并把 root 文件系统挂为读写。他会跳过所有系统认证, 让你使用passwd 程序 来改变root口令,然后启动到一个新的运行级。

    2法的区别:

    单用户模式和传入init=/bin/sh 是不同的 单用户模式会做rc.sysinit的初始化,就是系统的最基本初始化,比如检查文件系统,开启swap,加载模块,等等而传入init=/bin/sh 则不做任何初始化,只是给你个shell而已。

  • 相关阅读:
    windows7管理员权限
    Linux编程 第一个Hello World的C程序
    初步接触XCode和IPhone Simulator
    Android调用WCF
    mysql常用命令
    怎样利用Python制作守护进程
    Android模拟器如何设置DNS访问局域网内网站
    ubuntu下mysql攻略
    IIS7快速启动
    内存出错有时候是free后没有给指针赋NULL
  • 原文地址:https://www.cnblogs.com/jiftle/p/7494756.html
Copyright © 2011-2022 走看看