Linux chkconfig命令詳解

chkconfig命令檢查、設置系統的各類服務。這是Red Hat公司遵循GPL規則所開發的程序,它可查詢操做系統在每個執行等級中會執行哪些系統服務,其中包括各種常駐服務。謹記chkconfig不是當即自動禁止或激活一個服務,它只是簡單的改變了符號鏈接。html

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/  這樣的 目錄shell

[root@localhost ~]# chkconfig |grep sshd               # 運行級別是2345的時候開啓sshd服務

image

[root@localhost rc3.d]# ll /etc/rc.d/rc3.d/S55sshd #s表示start,s55表示開機啓動的時候是第55個開啓的服務

image

[root@localhost rc3.d]# grep 'chkconfig' /etc/init.d/sshd 
# chkconfig: 2345 55 25        # 註釋腳本里有默認的開啓關閉的參數,這裏開始是55
 注:利用yum,rqm安裝的服務,啓動命令都會自動放在init.d下面,而且接受chkconfig管理

image

自定義啓動服務

本身寫chkconfig管理的腳本,放在/etc/init.d目錄下vim

vim /etc/init.d/FTLbash

# 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/  

image

ll /etc/rc3.d/ | grep FTL           # 默認第77個開啓服務


image

 /etc/init.d/FTL start                  # 由於文件寫了一個能夠開啓的函數

image

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/下的文件去寫 ssh

經常使用的命令展現

界面設置自啓動服務ide

ntsysv

 

顯示某個服務,例如sshd函數

[root@localhost ~]# chkconfig --list  sshd 

image

顯示當前系統開啓的服務spa

[root@localhost ~]# chkconfig | egrep 3:on

image

關閉某個服務操作系統

[root@localhost ~]# chkconfig --list|grep FTL
[root@localhost ~]# chkconfig FTL off
[root@localhost ~]# chkconfig --list|grep FTL

image

開啓/關閉服務的3級別3d

[root@localhost ~]# chkconfig --list|grep FTL
[root@localhost ~]# chkconfig --level 3 FTL on          【開啓3級別】
[root@localhost ~]# chkconfig --level 3 FTL off          【關閉3級別】

image

關閉咱們不經常使用的服務【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
相關文章
相關標籤/搜索