[Linux]服務管理:service、systemctl、chkconfig

服務管理介紹

服務(Service)本質是進程,但是是運行在后臺的,通常都會監聽某個端口,等待其他程序的請求,比如(mysql、sshd、防火墻等),因此我們又稱為守護進程,是Linux中非常重要的一個知識點。

服務進程原理圖.png
一、service管理指令

service 服務名 [start | stop |restart |reload |stauts]

注意:在CentOS7.0后,不再使用service,而是systemctl 。centos7.0是向下兼容的,也是可以用service.

示例

  1. 查看當前防火墻的狀況,關閉防火墻和重啟防火墻。

    • //... ... 查看當前防火墻的狀況  Active: inactive (dead)  說明是關閉狀態
      [root@wcl ~]# service iptables status
      Redirecting to /bin/systemctl status iptables.service
      ● iptables.service - IPv4 firewall with iptables
         Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
         Active: inactive (dead)  
      
    • //... ...那么我們來開啟防火墻
      [root@wcl ~]# service iptables start
      Redirecting to /bin/systemctl start iptables.service
      //... ...開啟完防火墻,再來重新查看一下當前防火墻的狀態  Active: active (exited) :說明防火墻成功開啟
      [root@wcl ~]# service iptables status
      Redirecting to /bin/systemctl status iptables.service
      ● iptables.service - IPv4 firewall with iptables
         Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
         Active: active (exited) since 三 2018-05-02 11:33:45 CST; 20s ago
        Process: 27387 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
       Main PID: 27387 (code=exited, status=0/SUCCESS)
      
      5月 02 11:33:45 wcl systemd[1]: Starting IPv4 firewall with iptables...
      5月 02 11:33:45 wcl iptables.init[27387]: iptables: Applying firewall rules: [  確定  ]
      5月 02 11:33:45 wcl systemd[1]: Started IPv4 firewall with iptables.
      
    • //... ...  關閉防火墻
      [root@wcl ~]# service iptables stop
      Redirecting to /bin/systemctl stop iptables.service
      //... ...  查看防火墻狀態  :   Active: inactive (dead):已經關閉
      [root@wcl ~]# service iptables status
      Redirecting to /bin/systemctl status iptables.service
      ● iptables.service - IPv4 firewall with iptables
         Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
         Active: inactive (dead) since 三 2018-05-02 11:36:24 CST; 5s ago
        Process: 27463 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS)
        Process: 27387 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
       Main PID: 27387 (code=exited, status=0/SUCCESS)
      
      // ... ... ... ...此處省略部分顯示內容
      5月 02 11:36:24 wcl systemd[1]: Stopped IPv4 firewall with iptables.
      [root@wcl ~]# systemctl status firewalld
      ● firewalld.service - firewalld - dynamic firewall daemon
         Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
         Active: inactive (dead)
           Docs: man:firewalld(1)
      

      備注總結

      1. service iptables status:查看防火墻狀態
      2. service iptables start:開啟防火墻服務
      3. service iptables stop:關閉防火墻服務;
      4. 同理,我們可以在Centos7.0中用systemctl指令
      5. systemctl status firewalld:查看防火墻狀態
      6. systemctl start firewalld:開啟防火墻服務
      7. systemctl stop firewalld:關閉防火墻服務;
      8. 細節注意 :關閉或者啟動防火墻后,能夠立即生效,但這種方式只是臨時生效,當重啟服務后,還是要回歸以前的服務設置。如果希望設置某個服務自啟動或者關閉永久生效,要使用chkconfig指令
二、查看服務名

ls -l /etc/init.d/:列出系統中有哪些服務

[root@wcl ~]# ls -l /etc/init.d/        
總用量 64
-rw-r--r-- 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x 1 root root  9980 4月  11 2015 jexec
-rwxr-xr-x 1 root root 10604 4月  28 17:24 mysqld
-rwxr-xr-x 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x 1 root root  7293 5月   3 2017 network
-rw-r--r-- 1 root root  1160 3月   7 21:27 README
三、服務的運行級別(Runlevel)

vim /etc/inittab:查看或者修改默認級別

下面我來查看一下我的linux系統中的服務運行級別

[root@wcl ~]# vim /etc/inittab
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#// 翻譯:systemd使用“目標”而不是運行級別。 默認情況下,有兩個主要目標
# multi-user.target: analogous to runlevel 3    // 翻譯:multi-user.target:類似于運行級別3
# graphical.target: analogous to runlevel 5     // 翻譯:graphical.target:類似于運行級別5
#
# To view current default target, run:          //翻譯:要查看當前的默認目標,請運行
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
// 根據/etc/inittab文件內容 ,我知道了我的linux系統運行級別為3
[root@wcl ~]# systemctl get-default
multi-user.target     

//詳情參見服務運行級別 [Linux] 實用指令:運行級別和找回root密碼

四、linux開機的流程
Linux開機路程說明.png
五、chkconfig指令

通過chkconfig命令可以給每個服務的各個運行級別設置自啟動/關閉

chkconfig --list|grep xxx:篩選查看xxx服務

示例

  1. 查看所有服務

    • [root@wcl ~]# chkconfig --list
      注:該輸出結果只顯示 SysV 服務,并不包含
      原生 systemd 服務。SysV 配置數據
      可能被原生 systemd 配置覆蓋。 
      
            要列出 systemd 服務,請執行 'systemctl list-unit-files'。
            查看在具體 target 啟用的服務請執行
            'systemctl list-dependencies [target]'。
      
      jexec           0:關    1:開    2:開    3:開    4:開    5:開    6:關
      mysqld          0:關    1:關    2:開    3:開    4:開    5:開    6:關
      netconsole      0:關    1:關    2:關    3:關    4:關    5:關    6:關
      network         0:關    1:關    2:開    3:開    4:開    5:開    6:關
      
  2. 查看單個mysqld服務,有兩種方式;

    ? 方式1:chkconfig --list | grep 服務名

    ? 方式2:chkconfig 服務名--list

    • //方式1: chkconfig --list | grep mysqld
      [root@wcl ~]# chkconfig --list | grep mysqld
      mysqld          0:關    1:關    2:開    3:開    4:開    5:開    6:關
      
    • // 方式2:chkconfig mysqld --list
      [root@wcl ~]# chkconfig mysqld --list
      mysqld          0:關    1:關    2:開    3:開    4:開    5:開    6:關
      
  3. 關閉mysqld服務運行級別5的服務

    chkconfig --level 服務運行級別 服務名 on/off:開啟關閉某服務運行級別的服務

    • [root@wcl ~]# chkconfig --level 5 mysqld off       //關閉
      [root@wcl ~]# chkconfig mysqld --list     //查看驗證是否成功關閉  
      mysqld          0:關    1:關    2:開    3:開    4:開    5:關    6:關
      
      

    注意

    注:該輸出結果只顯示 SysV 服務,并不包含
    原生 systemd 服務。SysV 配置數據
    可能被原生 systemd 配置覆蓋。

      要列出 systemd 服務,請執行 'systemctl list-unit-files'。
      查看在具體 target 啟用的服務請執行
      'systemctl list-dependencies [target]'。
    

上面指令中查詢的結果出現這段內容,只因為我是在Centos7上面操作的原因。Centos7和之前的老版本差別較大。

細節注意chkconfig重新設置服務后自啟動或關閉,需要重啟機器reboot才能生效。


$$
Practice example:練習實例
$$

  1. 查看sshd的服務運行狀態:service sshd status

  2. 顯示當前系統中所有服務的各個運行級別的運行狀態:chkconfig --list

  3. 將sshd服務在運行級別5下設置為不自動啟動:chkconfig --level 5 sshd off

  4. 在所有運行級別下,關閉防火墻:chkconfig iptables off

  5. 在所有運行級別下,開啟防火墻:chkconfig iptables on


    額外補充總結

chkconfig --del mysqld:刪除服務mysqld

chkconfig --add mysqld:添加服務mysqld

chkconfig mysqld off:所有運行級別下關閉服務mysqld

chkconfig mysqld on:所有運行級別下開啟服務mysqld

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,143評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,553評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,620評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,416評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,940評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,170評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,709評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,597評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,029評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,407評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,663評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,403評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,746評論 2 370

推薦閱讀更多精彩內容