chkconfig命令檢查、設置系統的各種服務。這是Red Hat公司遵循GPL規則所開發的程序,它可查詢操作系統在每一個執行等級中會執行哪些系統服務,其中包括各類常駐服務。謹記chkconfig不是立即自動禁止或激活一個服務,它只是簡單的改變了符號連接。
chkconfig常見命令參數
用法: chkconfig [--list] [--type <type>] [name] chkconfig --add <name> chkconfig --del <name> chkconfig --override <name> chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
chkconfig原理通俗理解
chkconfig 處理的命令類似於我們平時執行的 /etc/init.d/sshd restart這樣的命令
每一個運行級別(0-6)對應一個 /etc/rc.d/rc3.d/ 這樣的 目錄
[root@localhost ~]# chkconfig |grep sshd # 運行級別是2345的時候開啟sshd服務
[root@localhost rc3.d]# ll /etc/rc.d/rc3.d/S55sshd #s表示start,s55表示開機啟動的時候是第55個開啟的服務
[root@localhost rc3.d]# grep 'chkconfig' /etc/init.d/sshd # chkconfig: 2345 55 25 # 注釋腳本里有默認的開啟關閉的參數,這里開始是55 注:利用yum,rqm安裝的服務,啟動命令都會自動放在init.d下面,並且接受chkconfig管理
自定義啟動服務
自己寫chkconfig管理的腳本,放在/etc/init.d目錄下
vim /etc/init.d/FTL
# chkconfig: 345 77 69 # description: FTL is a protocol for secure remote shell access. \ # This serddvice starts up the OpenSSH server daemon. . /etc/init.d/functions case "$1" in start) action "FTL Linux is $1ing"/bin/true ;; esac
chmod +x /etc/init.d/FTL # 增加執行權限 chkconfig --add FTL # 添加到啟動服務 chkconfig --list FTL # 查看啟動服務,顯示默認的345級別開, 默認修改/etc/rc3.d/ /etc/rc5.d/
ll /etc/rc3.d/ | grep FTL # 默認第77個開啟服務
/etc/init.d/FTL start # 因為文件寫了一個可以開啟的函數
chkconfig管理腳本的要求:
1.執行 /etc/init.d/FTL start 格式執行正常服務
2.腳本開頭增加如下內容;
# chkconfig: 345 77 69 【345是啟動級別,77是第77個啟動程序,69是第69個關閉程序】
# description: FTL is Coming
3.chkconfig 是針對程序是否需要機器后開啟
# /etc/init.d/FTL start 讓程序當前運行
4.可以參考/etc/rc.d/rc3.d/下的文件去寫
常用的命令展示
界面設置自啟動服務
ntsysv
顯示某個服務,例如sshd
[root@localhost ~]# chkconfig --list sshd
顯示當前系統開啟的服務
[root@localhost ~]# chkconfig | egrep 3:on
關閉某個服務
[root@localhost ~]# chkconfig --list|grep FTL [root@localhost ~]# chkconfig FTL off [root@localhost ~]# chkconfig --list|grep FTL
開啟/關閉服務的3級別
[root@localhost ~]# chkconfig --list|grep FTL [root@localhost ~]# chkconfig --level 3 FTL on 【開啟3級別】 [root@localhost ~]# chkconfig --level 3 FTL off 【關閉3級別】
關閉我們不常用的服務【Linux服務最小原則】
chkconfig |grep 3:on | awk '{print $1}' | grep -Ev "sshd|network|crond|sysstat|rsyslog" | xargs -I{} chkconfig {} off ==>for name in `chkconfig | grep 3:on |awk '{print $1}'` ; do chkconfig $name off; done; ==命令用反引號 tab鍵上 ==>chkconfig | awk '{print $1}' | grep -Ev "sshd|network|crond|sysstat|rsyslog" | sed -r 's#(.*)#chkconfig /1 off#g' | bash |bash 之前輸出的只是字符串 |bash 之后是將內容交給bash處理 ==>chkconfig | awk '{print $1}' | grep -Ev "sshd|network|crond|sysstat|rsyslog" | awk '{print "chkconfig " $1 " off" }' | bash