Red Hat Enterprise Linux 7(CentOS 7)のsystemdによる管理

カテゴリ:Linuxコマンド

Red Hat Enterprise Linux 7 (CentOS 7)からは、サービスの開始や停止、自動起動、システムの再起動、起動レベルなどの設定が systemd に統合されています。
systemd の主な管理コマンドについて解説します。
※なお、以降のコマンド結果の例は CentOS Linux release 7.3.1611 で確認したものです。
# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 

systemd の管理は systemctl コマンドを使用して行います。

全サービスの一覧表示

インストールされている、すべてのサービスを一覧表示する場合は、以下を実行します。

systemctl list-units --type service

# systemctl list-units --type service
  UNIT                        LOAD   ACTIVE SUB     DESCRIPTION
  abrt-ccpp.service           loaded active exited  Install ABRT coredump hook
  abrt-oops.service           loaded active running ABRT kernel log watcher
  abrt-xorg.service           loaded active running ABRT Xorg log watcher
  abrtd.service               loaded active running ABRT Automated Bug Reporting
  accounts-daemon.service     loaded active running Accounts Service
  alsa-state.service          loaded active running Manage Sound Card State (res
  atd.service                 loaded active running Job spooling tools
  auditd.service              loaded active running Security Auditing Service
…

サービスの開始・停止・再起動

httpd や postfix などのサービスはこれまでは、service httpd start などのように実行していましたが、systemd 環境では、以下のように、開始・停止・再起動を行います。

開始
systemctl start httpd.service
systemctl start postfix.service

停止
systemctl stop httpd.service
systemctl stop postfix.service

再起動
systemctl restart httpd.service
systemctl restart postfix.service

なお、サービス名の後ろに付与する .service は省略できます。

systemctl start httpd

サービスのステータス確認

これまでは、サービスのステータスの確認は、service httpd status のように実行していましたが、systemd 環境では、以下のように実行します。

systemctl status httpd.service

# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 月 2017-01-09 09:11:17 JST; 9s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 4840 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─4840 /usr/sbin/httpd -DFOREGROUND
           ├─4841 /usr/sbin/httpd -DFOREGROUND
           ├─4842 /usr/sbin/httpd -DFOREGROUND
           ├─4843 /usr/sbin/httpd -DFOREGROUND
           ├─4844 /usr/sbin/httpd -DFOREGROUND
           └─4845 /usr/sbin/httpd -DFOREGROUND

 1月 09 09:11:17 localhost.localdomain systemd[1]: Starting The Apache HTTP ...
 1月 09 09:11:17 localhost.localdomain httpd[4840]: AH00558: httpd: Could no...
 1月 09 09:11:17 localhost.localdomain systemd[1]: Started The Apache HTTP S...
Hint: Some lines were ellipsized, use -l to show in full.

また全サービスのステータスはこれまでは、service --status-all を実行していましたが、systemd 環境では、以下のように実行します。

systemctl list-units --type service --all

# systemctl list-units --type service --all
  UNIT                       LOAD      ACTIVE   SUB     DESCRIPTION
  abrt-ccpp.service          loaded    active   exited  Install ABRT coredump ho
  abrt-oops.service          loaded    active   running ABRT kernel log watcher
  abrt-vmcore.service        loaded    inactive dead    Harvest vmcores for ABRT
  abrt-xorg.service          loaded    active   running ABRT Xorg log watcher
  abrtd.service              loaded    active   running ABRT Automated Bug Repor
  accounts-daemon.service    loaded    active   running Accounts Service
  alsa-restore.service       loaded    inactive dead    Save/Restore Sound Card 
  alsa-state.service         loaded    active   running Manage Sound Card State 
● apparmor.service           not-found inactive dead    apparmor.service
  atd.service                loaded    active   running Job spooling tools
…

サービスの自動起動の有効・無効の設定

サービスの自動起動の設定は、これまでは chkconfig httpd on のように実行していましたが、systemd 環境では、以下のように実行します。

有効
systemctl enable httpd.service

# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

無効
systemctl disable httpd.service

# systemctl disable httpd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

なお、サービスの自動起動の設定を確認する場合は、ステータス確認と同じコマンドです。

systemctl status httpd.service

もしくは

systemctl is-enabled httpd.service

# systemctl is-enabled httpd.service
enabled

全サービスの自動起動の設定の確認の場合は、以下を実行します。

systemctl list-unit-files --type service

# systemctl list-unit-files --type service
UNIT FILE                                     STATE   
abrt-ccpp.service                             enabled 
abrt-oops.service                             enabled 
abrt-pstoreoops.service                       disabled
abrt-vmcore.service                           enabled 
abrt-xorg.service                             enabled 
abrtd.service                                 enabled 
accounts-daemon.service                       enabled 
alsa-restore.service                          static  
alsa-state.service                            static  
alsa-store.service                            static  
anaconda-direct.service                       static  
anaconda-nm-config.service                    static  
anaconda-noshell.service                      static  
anaconda-pre.service                          static  
anaconda-shell@.service                       static  
anaconda-sshd.service                         static  
anaconda-tmux@.service                        static  
anaconda.service                              static  
arp-ethers.service                            disabled
atd.service                                   enabled 
auditd.service                                enabled 
auth-rpcgss-module.service                    static  
...

ターゲット(旧ランレベル)を指定する

これまでは、システムの起動モードはランレベルで指定していましたが、systemd 環境ではランレベルはターゲットに置き換わっています。
例えばランレベル3であれば、ターゲットは multi-user.target になります。

デフォルトのターゲットを確認する場合は、以下のコマンドを実行します。

systemctl get-default

# systemctl get-default
multi-user.target

デフォルトのターゲットを変更する場合は、以下のコマンドを実行します。
※graphical.target はGUIモード(旧ランレベル5)を意味します。

systemctl set-default graphical.target

# systemctl set-default graphical.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.

現在組み込まれているターゲットの一覧を表示する場合は、以下のコマンドを実行します。

systemctl list-units --type target

# systemctl list-units --type target
UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
basic.target           loaded active active Basic System
cryptsetup.target      loaded active active Encrypted Volumes
getty.target           loaded active active Login Prompts
graphical.target       loaded active active Graphical Interface
local-fs-pre.target    loaded active active Local File Systems (Pre)
local-fs.target        loaded active active Local File Systems
multi-user.target      loaded active active Multi-User System
…

現在のターゲットを切り替える場合は、以下のコマンドを実行します。
※GUIモードに切り替える場合

systemctl isolate graphical.target

システムの再起動・シャットダウン

これまでは、システムの再起動は、init 6 もしくは reboot などを実行していましたが、systemd 環境では、以下のように実行します。

再起動
systemctl reboot

シャットダウン
systemctl poweroff

なお、shutdown コマンドは systemd 環境でも引き続き使用可能です。

shutdown -r now

以上がよく使われる主な systemd 関連コマンドになるかと思います。

公開日時:2017年01月09日 10:22:23

なお、VPS選びで迷ったらこちらの記事で主要VPSのメモリ容量ごとの月額、年額料金を比較していますので、是非参考にしてみてください。

Linuxコマンドに戻る

「Linuxコマンド」に関する他のTips

このページのトップに戻る