サーバエンジニアの構築めも

Linux関連を中心としたサーバの構築記録

[CentOS 7] Systemd

CentOS 7では、initに替わってsystemdがサービスを管理するようになった。
サービス管理のコマンドもchckconfigからsystemdctlに変更となっている。

サービスの一覧表示

# systemctl -t service list-unit-files
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-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
~ snip ~

「STATE」の意味は以下の通り。

enabled 自動起動が有効
disabled 自動起動が無効
static 起動が必須のサービスで設定変更はできない

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

# systemctl status firewalld.service
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Tue 2016-01-19 17:57:39 JST; 1h 13min ago
 Main PID: 24714 (firewalld)
   CGroup: /system.slice/firewalld.service
           `-24714 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

起動設定だけであれば、以下のコマンドでも確認できる。

# systemctl is-enabled firewalld.service
enabled

サービスの停止

# systemctl stop firewalld.service
# systemctl status firewalld.service
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: inactive (dead) since Tue 2016-01-19 19:12:55 JST; 42s ago
  Process: 24714 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 24714 (code=exited, status=0/SUCCESS)

サービスの強制停止

# systemctl kill firewalld.service

サービスの起動

# systemctl start firewalld.service
# systemctl status firewalld.service
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: active (running) since Tue 2016-01-19 19:14:25 JST; 23s ago
 Main PID: 26347 (firewalld)
   CGroup: /system.slice/firewalld.service
           `-26347 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

サービスの再起動

# systemctl restart firewalld.service

サービスの自動起動の無効化

# systemctl disable firewalld.service
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
# systemctl is-enabled firewalld.service
disabled

サービスの自動起動の有効化

# systemctl enable firewalld.service
ln -s '/usr/lib/systemd/system/firewalld.service' '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
ln -s '/usr/lib/systemd/system/firewalld.service' '/etc/systemd/system/basic.target.wants/firewalld.service'
# systemctl is-enabled firewalld.service
enabled