- 工信部備案號 滇ICP備05000110號-1
- 滇公安備案 滇53010302000111
- 增值電信業務經營許可證 B1.B2-20181647、滇B1.B2-20190004
- 云南互聯網協會理事單位
- 安全聯盟認證網站身份V標記
- 域名注冊服務機構許可:滇D3-20230001
- 代理域名注冊服務機構:新網數碼
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而已。
售前咨詢
售后咨詢
備案咨詢
二維碼
TOP