5,738 瀏覽數

RedHat – systemctl

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

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

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

[root@benjr ~]# 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@benjr ~]# 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  
....

[root@benjr ~]#  systemctl |grep -u ssh
  sshd.service                                                                                                     loaded    active running   OpenSSH server daemon

這些各代表什麼意思呢!!

UNIT          LOAD      ACTIVE SUB       DESCRIPTION
sshd.service  loaded    active running   OpenSSH server daemon

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

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

[root@benjr ~]# 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@benjr ~]# 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

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

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

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

[root@benjr ~]# 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@benjr ~]# systemctl is-enabled NetworkManager
enabled
[root@benjr ~]# 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@benjr ~]# systemctl is-enabled NetworkManager
disabled
[root@benjr ~]# 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@benjr ~]# systemctl is-enabled NetworkManager
enabled

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

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

7 Replies to “RedHat – systemctl”

  1. 自動參照通知: 安裝 CoreOS 使用設定檔(2) – Benjr.tw

  2. 自動參照通知: Ubuntu Upstart – Benjr.tw

  3. 自動參照通知: Ubuntu + SSH – Benjr.tw

  4. 自動參照通知: RHEL6 – NetworkManager – Benjr.tw

  5. 自動參照通知: RHEL 7 (Systemd) – 防火牆 – Benjr.tw

  6. 自動參照通知: upstart – initctl – Benjr.tw

  7. 自動參照通知: Linux command – service – Benjr.tw

發表迴響