Linux command – systemctl

Loading

自從 Linux 開機使用 systemd 之後, 不管是 Ubuntu (Ubuntu 16.04 也採用 systemd 的方式來啟動服務,管理一樣採用 systemctl .) 或是 Redhat 的管理方式以及指令都改了很多,

Ubuntu 14.04 版本是採用 upstart 請自行參考 https://benjr.tw/38611, Ubuntu upstart 指令請參考 initctl – https://benjr.tw/94015 來作控制.

從 RedHat / CentOS 7 之後 #service , #chkconfig 就沒有辦法管理 systemd 所啟動的服務.相關的指令要從新學習了,使用 #chkconfig 系統也會提示要改用 #systemctl 來控制跟 systemd 所啟動的服務.

[root@localhost ~]# chkconfig

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
rhnsd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

第一次接觸 #systemctl (同 #systemctl list-units)會嚇一跳,訊息也太多了吧! 可以使用 #grep 搜尋你要找的服務.

[root@localhost ~]# systemctl 
  UNIT                               LOAD      ACTIVE SUB       DESCRIPTION
 proc-sys-fs-binfmt_misc.automount  loaded    active waiting   Arbitrary Executable File Format
  sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-block-sr0.device loaded    
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded    
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded  
....

透過 status 來觀看個別服務的狀態.

[root@localhost ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-02-21 16:39:06 CST; 2min 52s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 1090 (sshd)
    Tasks: 1 (limit: 23817)
   Memory: 5.4M
   CGroup: /system.slice/sshd.service
           └─1090 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr>

loaded , active (running) 各代表什麼意思呢!!
loaded 該服務是否被載入
active (running) 該服務是否啟動 (目前服務的狀態)
每一服務的狀態不太一樣,後面會透過不同的指令來觀看其狀態.

剛剛的指令並看不出來那些服務在開機時會被啟動,需要其他參數 list-unit-files 來查看.

[root@localhost ~]# systemctl list-unit-files
UNIT FILE                                   STATE
proc-sys-fs-binfmt_misc.automount           static
dev-hugepages.mount                         static
dev-mqueue.mount                            static
proc-sys-fs-binfmt_misc.mount               static
sys-fs-fuse-connections.mount               static
sys-kernel-config.mount                     static
sys-kernel-debug.mount                      static
tmp.mount                                   disabled
brandbot.path                               disabled
systemd-ask-password-console.path           static
systemd-ask-password-plymouth.path          static
systemd-ask-password-wall.path              static
session-1.scope                             static
arp-ethers.service                          disabled
auditd.service                              enabled
....

左邊的 UNIT FILE 是指查看 /usr/lib/systemd/system/ 內關於 systemd 的設定檔案 (因為我們用的參數是 list-unit-files)

右邊的是 STATE 有三種狀態

  • enabled – 開機時自動被執行.
  • disabled – 開機時不被執行.
  • static – 無法 enable 需要由 enabled 的服務 daemon 來決定這一些相依性的服務 daemon 需要被執行.我們可以透過 #systemctl list-dependencies 指令來看相依性.
    [root@localhost ~]# systemctl list-dependencies
    default.target
    ├─auditd.service
    ├─brandbot.path
    .....
    ├─basic.target
    │ ├─firewalld.service
    │ ├─microcode.service
    │ ├─rhel-autorelabel-mark.service
    │ ├─rhel-autorelabel.service
    │ ├─rhel-configure.service
    │ ├─rhel-dmesg.service
    │ ├─rhel-loadmodules.service
    │ ├─paths.target
    │ ├─slices.target
    │ │ ├─-.slice
    │ │ └─system.slice
    .....
    

除了 list-unit-files 外還有下面幾種方式來觀看 systemd 的服務 deamon

  • #systemctl list-units :List loaded units
  • #systemctl list-sockets :List loaded sockets ordered by address
  • #systemctl list-jobs :List jobs
    檢視哪些服務正在跑或是等待.

    [root@localhost ~]# systemctl list-jobs
        JOB UNIT         TYPE  STATE
    1111223 pmcd.service start running
    
    1 jobs listed.
    

已經瞭解了目前服務開機時是否載入,如何透過指令的方式去修改 (如同 #chkconfig xx on),這時則要使用

  • #systemctl enable :設定開機時該 unit 會自動被啟動.
  • #systemctl disable :設定開機時該 unit 不會被啟動.
  • #systemctl is-enable :檢查該 unit 在開機時是否啟用
  • #systemctl status :檢查該 unit 目前的狀態,可以看到是否正在執行,開機時是否執行…等資訊.

首先來看看服務 daemon 的狀態 status

[root@localhost ~]# systemctl status NetworkManager
NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled)
   Active: active (running) since Tue 2016-01-26 21:21:32 EST; 58min ago
 Main PID: 2359 (NetworkManager)
   CGroup: /system.slice/NetworkManager.service
           ├─2359 /usr/sbin/NetworkManager --no-daemon
           └─2367 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /v...

Jan 26 22:06:59 localhost.localdomain dhclient[2367]: DHCPREQUEST on enp0s3 t...
Jan 26 22:06:59 localhost.localdomain dhclient[2367]: DHCPACK from 192.168.42...
Jan 26 22:06:59 localhost.localdomain NetworkManager[2359]: <info>    address...
Jan 26 22:06:59 localhost.localdomain NetworkManager[2359]: <info>    plen 24...
Jan 26 22:06:59 localhost.localdomain NetworkManager[2359]: <info>    gateway...
Jan 26 22:06:59 localhost.localdomain NetworkManager[2359]: <info>    server ...
Jan 26 22:06:59 localhost.localdomain NetworkManager[2359]: <info>    lease t...
Jan 26 22:06:59 localhost.localdomain NetworkManager[2359]: <info>    nameser...
Jan 26 22:06:59 localhost.localdomain NetworkManager[2359]: <info>  (enp0s3):...
Jan 26 22:06:59 localhost.localdomain dhclient[2367]: bound to 192.168.42.172...
Hint: Some lines were ellipsized, use -l to show in full.

狀態可以分為兩區,服務狀態與服務狀態的訊息(還可以透過參數 -l 來查看所有的訊息)

透過參數 is-enabled 來看服務 daemon 的開機時是否啟動.

[root@localhost ~]# systemctl is-enabled NetworkManager
enabled
[root@localhost ~]# systemctl disable NetworkManager
rm '/etc/systemd/system/multi-user.target.wants/NetworkManager.service'
rm '/etc/systemd/system/dbus-org.freedesktop.NetworkManager.service'
rm '/etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service'
[root@localhost ~]# systemctl is-enabled NetworkManager
disabled
[root@localhost ~]# systemctl enable NetworkManager
ln -s '/usr/lib/systemd/system/NetworkManager.service' '/etc/systemd/system/dbus-org.freedesktop.NetworkManager.service'
ln -s '/usr/lib/systemd/system/NetworkManager.service' '/etc/systemd/system/multi-user.target.wants/NetworkManager.service'
ln -s '/usr/lib/systemd/system/NetworkManager-dispatcher.service' '/etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service'
[root@localhost ~]# systemctl is-enabled NetworkManager
enabled

如果要立刻改變服務狀態則需要透過

  • #systemctl start :立刻啟動該 unit
  • #systemctl stop :立刻關閉該 unit
  • #systemctl restart :動作跟 stop 再 start 是同樣意思.
  • #systemctl reload :跟 restart 不同的是,只有重新載入設定檔.
  • #systemctl is-active :檢查該 unit 目前有沒有正在運作中.
[root@localhost ~]# systemctl is-active NetworkManager
active
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# systemctl is-active NetworkManager
inactive
[root@localhost ~]# systemctl start NetworkManager
[root@localhost ~]# systemctl is-active NetworkManager
active
[root@localhost ~]# systemctl restart NetworkManager
[root@localhost ~]# systemctl is-active NetworkManager
active
[root@localhost ~]# systemctl reload NetworkManager
Failed to issue method call: Job type reload is not applicable for unit NetworkManager.service.
沒有解決問題,試試搜尋本站其他內容

7 thoughts on “Linux command – systemctl

  1. 自動引用通知: Ubuntu Upstart – Benjr.tw
  2. 自動引用通知: Ubuntu + SSH – Benjr.tw
  3. 自動引用通知: upstart – initctl – Benjr.tw

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料