6個Linux chkconfig命令執行個體 – 增加,刪除,查看和修改services的自動啟動選項

來源:互聯網
上載者:User

翻譯自:http://www.thegeekstuff.com/2011/06/chkconfig-examples/

 

注意:service的安裝目錄在/etc/rc.d/init.d下,/etc/init.d 是/etc/rc.d/init.d的連結。

 

chkconfig命令用來安裝,查看或修改 services隨系統啟動的啟動選項的設定。本文章包含了7個執行個體來解釋如何使用chkconfig命令。

 

1 在shell指令碼中檢查service的啟動選項的設定

當你執行chkconfig加service名字,如果service被配置為自動啟動,則它將返回true。下列的程式碼片段顯示了如何在指令碼中檢查一個service是否被配置為自動啟動。

# vi check.sh
chkconfig network && echo "Network service is configured"
chkconfig junk && echo "Junk service is configured"

# ./check.sh
Network service is configured

你也可以特別地查看它是否配置為在某個run level自動啟動。

# vi check1.sh
chkconfig network --level 3 && echo "Network service is configured for level 3"
chkconfig network --level 1 && echo "Network service is configured for level 1"

# ./check1.sh
Network service is configured for level 3

 

2 查看所有的services的啟動選項的設定

--list選項顯示所有的services的啟動選項的配置狀態。

# chkconfig --list
abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off
acpid 0:off 1:off 2:off 3:off 4:off 5:off 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
...

只查看在某個runlevel啟動的services,例如查看在runlevel3下自動啟動的services。

chkconfig --list | grep 3:on

查看某個service的啟動選項的設定,也可以對chkconfig --list 的output使用grep,例如:chkconfig --list | grep network

 

3 增加一個新的service為自動啟動

使用-add選項來增加某個service,使其能夠在系統啟動的時候自動地啟動。

下列的執行個體顯示了如何增加一個新的service iptables,使其能夠在系統啟動的時候自動地啟動。使用‘chkconfig --add’命令將自動地設定在runlevel 2,3,4,5上自動地啟動。

# chkconfig --list | grep iptables
# chkconfig --add iptables
# chkconfig --list | grep iptables
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off

注意:”chkconfig --add“ 只是設定一個存在的service為自動啟動,此service必須已經安裝,即已經存在於/etc/rc.d/init.d下。如果service不存在,你需要首先安裝service到/etc/rc.d/init.d下,然後再為其設定自動啟動選項。這是很多的初學者都容易犯的錯誤,所以值得在這裡強調。

 

4 刪除某個service的自動啟動選項的設定

下列的執行個體顯示了ip6tables service被配置為自動啟動。
# chkconfig --list | grep ip6tables
ip6tables 0:off 1:off 2:off 3:on 4:off 5:off 6:off

要刪除某個service的自動啟動的設定,使用--del選項,如下:
# chkconfig --del ip6tables
# chkconfig --list | grep ip6tables

 

5 設定或取消某個service在某個runlevel的自動啟動設定
有時你可能不想取消整個service的自動啟動的設定,你只是需要設定或取消service在某個runlevel的自動啟動的設定,

下列的執行個體將取消nfserver service在runlevel5的自動啟動設定
# chkconfig --level 5 nfsserver off
你也可以組合多個runlevel,例如你可以取消nfserver在runlevel3和5的自動啟動設定
# chkconfig --level 35 nfsserver off

 

6 chkconfig 與 rc*.d目錄下的指令碼 (*表示runlevel1-6)
當你使用chkconfig來增加或刪除某個service的自動啟動選項的設定的時候,其實是在rc*.d目錄下做下列的事情:
當chkconfig --add命令執行的時候,其實是在對應的rc*.d目錄下建立相應用來啟動和停止service的service的symbollink檔案;
當chkconfig --del命令執行的時候,其實是在對應的rc*.d目錄下刪除相應用來啟動和停止service的service的symbollink檔案;

rc*.d目錄下與某個service關聯的2個symbollink檔案中,以K(kill)開始的表示此檔案用來停止service,以S(start)開始的表示此檔案用來啟動service。

例如service nfsserver沒有設定自動啟動選項的時候,我們在/rc*.d下看不到用來啟動和停止service的symbollink檔案

# chkconfig --list | grep nfsserver
nfsserver 0:off 1:off 2:off 3:off 4:off 5:off 6:off

# ls /etc/rc.d/rc3.d | grep nfsserver
# ls /etc/rc.d/rc5.d | grep nfsserver

當我們為nfsserver service設定自動啟動選項後,我們就可以看到相應的symbollink檔案了
# chkconfig --add nfsserver
nfsserver 0:off 1:off 2:off 3:on 4:off 5:on 6:off

# cd /etc/rc.d/rc3.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../init.d/nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../init.d/nfsserver

# cd /etc/rc.d/rc5.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver ->../init.d/nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../init.d/nfsserver

當你取消此service在某個runlevel的自動啟動選項的設定的時候,對應的rc*.d目錄下的symbollink檔案也將被自動的刪除,

# chkconfig --level 5 nfsserver off
# ls /etc/rc.d/rc5.d | grep nfsserver

完!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.