1. 程式人生 > 其它 >|NO.Z.00001|——————————|ManageMent|——|Linux&系統管理.V01|

|NO.Z.00001|——————————|ManageMent|——|Linux&系統管理.V01|

一、系統執行級別
### --- 執行級別

~~~     Linux 預設有 7 個執行級別
執行級別 含義
0 關機
1 單使用者模式,可以想象為windows的安全模式,主要用於系統修復
2 不完全的命令列模式,不含NFS服務
3 完全的命令列模式,就是標準字元介面
4 系統保留
5 圖形模式
6 重啟系統
### --- 在 Linux 系統中可以使用 runlevel 命令來檢視系統的執行級別,命令如下:
~~~     N 代表進入這個級別前,上一個是哪個級別。3 代表當前級別
~~~     在這個命令的結果中,“N 3”中的 N 代表進入這個級別前,
~~~     上一個級別是什麼,3 代表當前級別。
~~~     “N”就是 None 的意思,也就是說系統是開機直接進入的 3 執行級別,沒有上一個執行級別。
~~~     那如果是從圖形介面切換到字元介面的話,再檢視執行級別,就應該是這樣的:

[root@server11 ~]# runlevel
N 3                                                             // N表示空的,表示在進入當前級別之前,上一個級別是什麼;3表示當前級別
 
### --- 代表是由 5 級別進入的 3 級別
~~~     那麼可以手工改變當前的執行級別嗎?當然可以了,只要使用 init 命令(注意著不是 init 程序)即可,命令如下:

[root@server11 ~]# init 5
[root@server11 ~]# runlevel
3 5
[root@server11 ~]# init 5
 
~~~     關機
~~~     進入圖形介面,當然要已經安裝了圖形介面才可以

[root@server11 ~]# init 0
 
### --- 重啟動
~~~     不過要注意使用 init 命令關機和重啟動,並不是太安全,容易造成資料丟失。
~~~     所以推薦大家還是使用 shutdown 命令進行關機和重啟吧!

[root@server11 ~]# init 6

二、系統預設執行級別

### --- 系統預設執行級別
~~~     知道了執行級別的作用,我們回到系統啟動過程中來。
~~~     /etc/init/rcS.conf 配置檔案呼叫/etc/inittab 配置檔案的目的就是為了確定系統的預設執行級別,
~~~     也就是系統一開機後會進入那個執行級別。這個檔案的內容如下:

[root@server11 ~]# vim /etc/inittab
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
#   #不允許把執行級別設定為0或者6
id:3:initdefault:
~~~     系統會先呼叫/etc/init/rcS.conf
Individual runlevels are started by /etc/init/rc.conf
~~~     再呼叫/etc/init/rc.conf,在不同的執行級別啟動不同的服務
Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
~~~     通過這個配置檔案判斷 Ctrl+Alt+Delete 熱啟動鍵是否可用
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)
1 - Single user mode
2 - Multiuser, without NFS (The same as 3, if you do not have networking)
3 - Full multiuser mode
4 - unused
5 - X11
6 - reboot (Do NOT set initdefault to this)
~~~     很眼熟吧,就是剛剛的 0-6 的執行級別的說明
id:3:initdefault:
~~~     這就是系統的預設執行級別,也就是系統開機後直接進入哪個執行級別
~~~     注意這裡的預設執行級別只能寫 3 或 5,其他的級別要不就是關機重啟,
~~~     要不就是保留或單使用者,都不能作為系統預設執行級別的。

三、 /etc/rc.d/rc.local 檔案

### --- /etc/rc.d/rc.local 檔案
~~~     這個配置檔案會在使用者登陸之前讀取,這個檔案中寫入什麼命令,
~~~     在每次系統啟動時都會執行一次。
~~~     也就是說,我如果有任何需要在系統啟動就執行的工作,
~~~     只需要寫入/etc/rc.d/rc.local 這個配置檔案即可。這個檔案內容如下:

[root@server21 ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Feb  4 07:44 /etc/rc.local -> rc.d/rc.local
~~~     有個連結檔案,兩個檔案修改哪一個都可以
[root@server21 ~]# vi /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local                                // 每次開機都touch一下這個檔案,
 
~~~     預設會 touch 這個檔案,每次系統啟動時 touch 這個檔案,
~~~     這個檔案的修改時間就是系統的啟動時間了。
~~~     如果寫入 RPM 包安裝的 apache 的啟動命令,apache 服務就會開機時自動啟動了。

[root@server21 ~]# /etc/rc.d/init.d/httpd start

 

                 
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor