CentOS統的7個運行層級的含義

來源:互聯網
上載者:User

CentOS統的7個運行層級的含義

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        singleblk-availability          ip6tables     mysql         ntpd     restorecond  sshdcloud-set-guest-password  iptables      netconsole    ntpdate  rsyslog      sysstatcrond                     killall       netfs         php-fpm  sandbox      udev-postfunctions                 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總用量 0lrwxrwxrwx  1 root root 19 11月 14 09:35 K10saslauthd -> ../init.d/saslauthdlrwxrwxrwx. 1 root root 20 11月 13 16:22 K50netconsole -> ../init.d/netconsolelrwxrwxrwx  1 root root 14 3月  16 13:53 K74ntpd -> ../init.d/ntpdlrwxrwxrwx  1 root root 17 3月  16 13:53 K75ntpdate -> ../init.d/ntpdatelrwxrwxrwx. 1 root root 21 11月 13 16:22 K87restorecond -> ../init.d/restorecondlrwxrwxrwx  1 root root 15 11月 14 09:34 K89rdisc -> ../init.d/rdisclrwxrwxrwx  1 root root 18 3月  25 12:20 K92iptables -> ../init.d/iptableslrwxrwxrwx  1 root root 17 4月   1 22:36 S01sysstat -> ../init.d/sysstatlrwxrwxrwx  1 root root 22 3月  25 12:20 S02lvm2-monitor -> ../init.d/lvm2-monitorlrwxrwxrwx  1 root root 19 3月  25 12:20 S08ip6tables -> ../init.d/ip6tableslrwxrwxrwx  1 root root 17 3月  25 12:18 S10network -> ../init.d/networklrwxrwxrwx. 1 root root 16 11月 13 16:23 S11auditd -> ../init.d/auditdlrwxrwxrwx  1 root root 17 3月  25 12:19 S12rsyslog -> ../init.d/rsysloglrwxrwxrwx. 1 root root 31 11月 14 09:05 S14xe-linux-distribution -> ../init.d/xe-linux-distributionlrwxrwxrwx  1 root root 26 3月  25 12:20 S25blk-availability -> ../init.d/blk-availabilitylrwxrwxrwx  1 root root 15 3月  25 12:18 S25netfs -> ../init.d/netfslrwxrwxrwx  1 root root 19 3月  25 12:18 S26udev-post -> ../init.d/udev-postlrwxrwxrwx  1 root root 17 4月   1 22:36 S50php-fpm -> ../init.d/php-fpmlrwxrwxrwx  1 root root 15 3月  16 16:07 S55nginx -> ../init.d/nginxlrwxrwxrwx  1 root root 14 3月  25 12:20 S55sshd -> ../init.d/sshdlrwxrwxrwx  1 root root 15 3月  16 15:24 S64mysql -> ../init.d/mysqllrwxrwxrwx  1 root root 17 4月   1 22:36 S80postfix -> ../init.d/postfixlrwxrwxrwx  1 root root 15 4月   1 22:36 S90crond -> ../init.d/crondlrwxrwxrwx  1 root root 34 11月 14 09:29 S98cloud-set-guest-password -> ../init.d/cloud-set-guest-passwordlrwxrwxrwx  1 root root 11 3月  25 12:18 S99local -> ../rc.local

4、系統會根據指定的運行層級進入對應的rcN.d目錄,並按照檔案名稱順序檢索目錄下的連結檔案

  • 對於以K開頭的檔案,系統將終止對應的服務
  • 對於以S開頭的檔案,系統將啟動對應的服務

5、查看運行層級用:runlevel

[root@bin2ctyun rc3.d]# runlevelN 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而已。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.