Systemctlは、systemdがシステムおよびサービスマネージャーを管理するために使用するユーティリティです。 Ubuntu、Debian、Fedora、Linux Mint、OpenSuSE、Redhatなどの多くの最新のLinuxディストリビューションは、デフォルトのinitシステムとしてsystemdを採用しています。
systemctlを使用すると、サービスの開始、停止、リロード、再起動、ユニットの一覧表示、サービスステータスの確認、サービスの有効化/無効化、ターゲット(ランレベル)の管理、および電源管理を行うことができます。
このチュートリアルでは、systemctlコマンドを使用してLinuxでsystemdサービスを管理する方法を紹介します。
1。 Systemctl Start / Stop / Restart / Reload Service
Linuxでサービスを開始するには、systemctlに続けて「start」スペースサービス名を実行します。
例:
systemctl start dnsmasq
serviceコマンドとは対照的に、systemctlstartコマンドは出力を提供しません。

サービスを停止するには、systemctlstopservice-nameを使用します。例:
systemctl stop dnsmasq

サービスを再起動するには、systemctlrestartservice-nameを使用します。
例:
systemctl restart dnsmasq

再起動せずにサービスの構成(たとえばssh)をリロードするには、systemctlreloadservice-nameを使用します。
例:
systemctl reload sshd

2。 Systemctlチェックサービスステータス
サービスが実行されているかどうかを確認するために、systemctlステータスチェックを使用できます。
systemctl status dnsmasq

3。起動時にサービスを確認して有効/無効にする
起動時にサービスを有効にするには(これはsysvinitシステムの「chkconfigon」に対応します)、systemctlenableservice-nameを使用します。
例:
systemctl enable dnsmasq.service

同様に、サービスは起動時に無効にして実行できます:
systemctl disable dnsmasq.service

起動時にサービスが有効になっているかどうかを確認するには、次のコマンドを実行します。
systemctl is-enabled dnsmasq.service

4。 Systemctlリストユニット
実行中のすべてのユニットを一覧表示するには、オプションを指定せずにsystemctlコマンドを実行します。 list-unitsオプションも同じことをします。
systemctl
または
systemctl list-units

故障したユニットは、-failedオプションで一覧表示できます。
systemctl --failed

すべてのアクティブなサービスを一覧表示するには、次を実行します:
systemctl list-units -t service

6。 Systemctlreboot/shutdownコマンド
shutdownコマンドと同様に、systemctlコマンドを使用してシステムを停止、再起動、または休止状態にします。
次のコマンドは、システムをシャットダウンしてマシンの電源を切り、ログインしているすべてのユーザーに通知を送信します。
systemctl poweroff
次のコマンドはシステムをシャットダウンしますが、マシンの電源はオフになりません。これにより、ログインしているすべてのユーザーに通知が送信されます。
systemctl halt
マシンの電源を切りますが、ログインしているすべてのユーザーに通知を送信することはありません。
systemctl --no-wall poweroff
シャットダウンの詳細を表示するには、以下のコマンドを実行します。
journalctl -u systemd-shutdownd
7。 Systemcltからリモートシステムの管理
通常、上記のsystemctlコマンドはすべて、systemctlコマンド自体を使用してリモートホストを管理するために使用できます。これは、リモートホストとの通信にsshを使用します。次のように、ユーザーとホストをsystemctlコマンドに追加するだけです。
systemctl status sshd -H [email protected]

8。ターゲットの管理
Systemdには、sysVinitシステムのランレベルと同様の目的を持つターゲットの概念があります。
sysVinitのランレベルはほとんど数値(0,1,2、...)でした。 sysVinitのランレベルとそれに対応するsystemdのランレベルは次のとおりです。
0 runlevel0.target, poweroff.target
1, s, single runlevel1.target, rescue.target
2, 4 runlevel2.target, runlevel4.target, multi-user.target
3 runlevel3.target, multi-user.target
5 runlevel5.target, graphical.target
6 runlevel6.target, reboot.target
emergency emergency.target
現在のターゲットを変更するには、次のように入力します。
systemctl isolate graphical.target

現在のターゲットを確認したい場合は、対応するすべてのユニットをリストする必要があります。
systemctl list-units --type=target

ここにリストされている「graphical.target」を見ることができます。これが私たちが目標を変更したものです。次に、ランレベルを再度multi-user.targetに変更して、この出力を分析しましょう。
systemctl isolate multi-user.target
systemctl list-units --type=target

デフォルトのターゲットを一覧表示するには、次のように入力します。
systemctl get-default

デフォルトのターゲットは、set-defaultコマンドで設定できます。次のように入力します。
systemctl set-default graphical.target

その他の便利なsystemdコマンド
systemdへのログイン
systemdには、journaldと呼ばれる独自のログシステムがあります。 sysVinitのsyslogデーモンを置き換えます。
journalctl

すべてのブートメッセージを表示するには、コマンド「journalctl-b」を実行します。
journalctl -b

次のコマンドは、システムログをリアルタイムで追跡します(tail -fと同様)。
journalctl -f

特定のサービスまたは実行可能ファイルに固有のログを確認するには、journalctlを次のように使用します。
journalctl /usr/sbin/dnsmasq

起動プロセスの期間を見つける
次のコマンドを使用してsystemdのブートプロセス期間を検索するには:
systemd-analyze

systemd-analyze時間にも同じ情報が表示されます。
systemd-analyze time

初期化にかかった時間順に並べられたすべての実行中のユニットのリストを印刷するには、systemd-analyzeblameを使用します。
systemd-analyze blame

Hostnamectlコマンド
ホスト名を表示および変更するには、hostnamectlコマンドを使用します。
hostnamectl

結論
このチュートリアルでは、Linuxディストリビューションでsystemdサービスを管理するためのsystemctlコマンドについて学習しました。読んで楽しんでいただければ幸いです。以下のコメントセクションに提案を残してください。
関連項目:
- LinuxでのSystemdBootプロセスの詳細
- カラフル! systemdとsysVinitLinuxのチートシート