【systemctl】Linuxコマンド_サービスの状態確認・設定を実施する (2023)

LinuxOSではSSHやHTTPなどサーバーとして動作することができます。
そのためにはパッケージをインストールし、プログラムとして動作することが必要となります。

このように起動時に実行されるプログラムや、パッケージインストールしたプログラムをまとめて「サービス」と言います。

「サービス」を起動・停止したり、そのサービスの状態を確認する操作として、「systemctl」コマンドを実施します。
「systemctl」コマンドはCentOS系やFedoraなどで使用されます。

※Ubuntu、Debian系では「service」コマンドとなります。

目次

(Video) Linux - サービスの開始、停止、再起動 (Systemd、systemctl、service、init.d )

  • 1 「systemctl」コマンドの使い方(Linux)
    • 1.1 LinuxOS上のサービスの状態確認
    • 1.2 サービスの起動・停止
    • 1.3 システム再起動、停止後もサービスが自動に起動するようにする
    • 1.4 「systemctl」コマンドオプション

「systemctl」コマンドの使い方(Linux)

CentOS系やFedoraなどで全てのサービスの状態を確認するコマンドとなります。

$ systemctl -a list-units UNIT LOAD ACTIVE SUB DESCRIPTION ● boot.automount not-found inactive dead boot.automount proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File> dev-cdrom.device loaded active plugged QEMU_DVD-ROM dev-disk-by\x2did-ata\x2dQEMU_DVD\x2dROM_QM00003.device loaded active plugged QEMU_DVD-ROM dev-disk-by\x2did-ata\x2dQEMU_DVD\x2dROM_QM00004.device loaded active plugged QEMU_DVD-ROM config-2 dev-disk-by\x2dlabel-config\x2d2.device loaded active plugged QEMU_DVD-ROM config-2 dev-disk-by\x2dlabel-rootfs.device loaded active plugged /dev/disk/by-label/rootfs dev-disk-by\x2dpartlabel-biosboot.device loaded active plugged /dev/disk/by-partlabel/bi> dev-disk-by\x2dpartlabel-rootfs.device loaded active plugged /dev/disk/by-partlabel/ro> dev-disk-by\x2dpartuuid-4ed7871c\x2de08f\x2d4868\x2db0ee\x2da058f644e70a.device loaded active plugged /dev> dev-disk-by\x2dpartuuid-77c211c5\x2dfedd\x2d48fd\x2d8cb9\x2d99300970f8df.device loaded active plugged /dev> dev-disk-by\x2dpath-pci\x2d0000:00:01.1\x2data\x2d2.device loaded active plugged QEMU_DVD-ROM config-2 dev-disk-by\x2dpath-pci\x2d0000:00:05.0.device loaded active plugged /dev/disk/by-path/pci-000>------中略------

一部のサービスのリストを確認したい場合は「grep」コマンドで表示を絞ることも可能です。

$ systemctl -a list-units | grep sshd● sshd-keygen.service not-found inactive dead sshd-keygen.service sshd-keygen@ecdsa.service loaded inactive dead OpenSSH ecdsa Server Key Generation sshd-keygen@ed25519.service loaded inactive dead OpenSSH ed25519 Server Key Generation sshd-keygen@rsa.service loaded inactive dead OpenSSH rsa Server Key Generation sshd.service loaded active running OpenSSH server daemon system-sshd\x2dkeygen.slice loaded active active system-sshd\x2dkeygen.slice sshd-keygen.target loaded active active sshd-keygen.target 

LinuxOS上のサービスの状態確認

「systemctl」コマンドではサービスの状態を確認することができます。
以下では「sshd」サービスの状態を表示した例となります。

(Video) systemctl と journalctl を使用して Linux サービスを管理する方法 |システム管理者の基本

オプションは「status」を利用します。

$ systemctl status sshd● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2022-02-05 16:29:17 JST; 3 weeks 6 days ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 33158 (sshd) Tasks: 3 (limit: 2778) Memory: 19.5M CGroup: /system.slice/sshd.service ├─ 33158 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc -oMACs=hmac-sha2-> ├─665562 sshd: root [priv] └─665563 sshd: root [net] 3月 05 15:07:44 118-27-15-211 sshd[665556]: Failed password for invalid user monitor from 78.100.61.58 port 38906 ssh2 3月 05 15:07:45 118-27-15-211 sshd[665558]: Received disconnect from 143.198.164.233 port 58698:11: Bye Bye [preauth] 3月 05 15:07:45 118-27-15-211 sshd[665558]: Disconnected from invalid user steve 143.198.164.233 port 58698 [preauth] 3月 05 15:07:46 118-27-15-211 sshd[665556]: Received disconnect from 78.100.61.58 port 38906:11: Bye Bye [preauth] 3月 05 15:07:46 118-27-15-211 sshd[665556]: Disconnected from invalid user monitor 78.100.61.58 port 38906 [preauth] 3月 05 15:08:15 118-27-15-211 sshd[665560]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=111.95.141.34 user=root 3月 05 15:08:17 118-27-15-211 sshd[665560]: Failed password for root from 111.95.141.34 port 39734 ssh2 3月 05 15:08:17 118-27-15-211 sshd[665562]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=121.1.248.95 user=root 3月 05 15:08:17 118-27-15-211 sshd[665560]: Received disconnect from 111.95.141.34 port 39734:11: Bye Bye [preauth] 3月 05 15:08:17 118-27-15-211 sshd[665560]: Disconnected from authenticating user root 111.95.141.34 port 39734 [preauth]lines 1-23/23 (END)

上記の情報では「sshd」サービスが有効化(enable)されており、サービス実行中(active)の状態となります。
この状態でサービスは利用できる状態となるので、このサーバーにSSHアクセスができることが確認できます。

サービスの起動・停止

実際に「chrony」サービスを起動してみます。
サービスを確認すると以下のように停止状態となります。

$ systemctl status chronyd● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:chronyd(8) man:chrony.conf(5)

サービスを起動するには「start」コマンドを実行します。

$ systemctl start chronyd

ステータスを表示すると「chrony」サービスが起動していることが確認できます。

(Video) システムCTL | systemd で実行中のサービスを一覧表示する

$ systemctl status chronyd● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled) Active: active (running) since Sat 2022-03-05 15:28:47 JST; 16s left Docs: man:chronyd(8) man:chrony.conf(5) Process: 665697 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS) Process: 665693 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 665695 (chronyd) Tasks: 1 (limit: 2778) Memory: 3.0M CGroup: /system.slice/chronyd.service └─665695 /usr/sbin/chronyd 3月 05 15:28:47 118-27-15-211 systemd[1]: Starting NTP client/server...------中略------

次にサービスを停止するには「stop」コマンドを実行します。

$ systemctl stop chronyd

サービスが停止していることが確認できます。

$ systemctl status chronyd● chronyd.service - NTP client/server Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:chronyd(8) man:chrony.conf(5)

システム再起動、停止後もサービスが自動に起動するようにする

LinuxOSの再起動後や停止後は上記のstartオプションだけだと停止状態となります。
システム起動時に自動で該当のサービスを起動するようにするには「enable」コマンドを実行します。

$ systemctl enable chronyd

また、逆にサービスの自動起動を無効にするには「disable」コマンドを実行します。

$ systemctl disable chronyd

「systemctl」コマンドオプション

「systemctl」コマンドのオプションは以下の通りとなります。

(Video) ブートプロセスとsystemd(Linux学習)

$ systemctl --helpsystemctl [OPTIONS...] {COMMAND} ...Query or send control commands to the systemd manager. -h --help Show this help --version Show package version --system Connect to system manager --user Connect to user service manager -H --host=[USER@]HOST Operate on remote host -M --machine=CONTAINER Operate on local container -t --type=TYPE List units of a particular type --state=STATE List units with particular LOAD or SUB or ACTIVE state -p --property=NAME Show only properties by this name -a --all Show all properties/all units currently in memory, including dead/empty ones. To list all units installed on the system, use the 'list-unit-files' command instead. --failed Same as --state=failed -l --full Don't ellipsize unit names on output -r --recursive Show unit list of host and local containers --reverse Show reverse dependencies with 'list-dependencies' --job-mode=MODE Specify how to deal with already queued jobs, when queueing a new job -T --show-transaction When enqueuing a unit job, show full transaction --show-types When showing sockets, explicitly show their type --value When showing properties, only print the value --check-inhibitors=MODE Specify if checking inhibitors before shutting down, sleeping or hibernating -i Shortcut for --check-inhibitors=no --kill-who=WHO Who to send signal to -s --signal=SIGNAL Which signal to send --now Start or stop unit in addition to enabling or disabling it --dry-run Only print what would be done -q --quiet Suppress output --wait For (re)start, wait until service stopped again --no-block Do not wait until operation finished --no-wall Don't send wall message before halt/power-off/reboot --no-reload Don't reload daemon after en-/dis-abling unit files --no-legend Do not print a legend (column headers and hints) --no-pager Do not pipe output into a pager --no-ask-password Do not ask for system passwords --global Enable/disable/mask unit files globally --runtime Enable/disable/mask unit files temporarily until next reboot -f --force When enabling unit files, override existing symlinks When shutting down, execute action immediately --preset-mode= Apply only enable, only disable, or all presets --root=PATH Enable/disable/mask unit files in the specified root directory -n --lines=INTEGER Number of journal entries to show -o --output=STRING Change journal output mode (short, short-precise, short-iso, short-iso-precise, short-full, short-monotonic, short-unix, verbose, export, json, json-pretty, json-sse, cat) --firmware-setup Tell the firmware to show the setup menu on next boot --plain Print unit dependencies as a list instead of a treeUnit Commands: list-units [PATTERN...] List units currently in memory list-sockets [PATTERN...] List socket units currently in memory, ordered by address list-timers [PATTERN...] List timer units currently in memory, ordered by next elapse start UNIT... Start (activate) one or more units stop UNIT... Stop (deactivate) one or more units reload UNIT... Reload one or more units restart UNIT... Start or restart one or more units try-restart UNIT... Restart one or more units if active reload-or-restart UNIT... Reload one or more units if possible, otherwise start or restart try-reload-or-restart UNIT... If active, reload one or more units, if supported, otherwise restart isolate UNIT Start one unit and stop all others kill UNIT... Send signal to processes of a unit freeze PATTERN... Freeze execution of unit processes thaw PATTERN... Resume execution of a frozen unit is-active PATTERN... Check whether units are active is-failed PATTERN... Check whether units are failed status [PATTERN...|PID...] Show runtime status of one or more units show [PATTERN...|JOB...] Show properties of one or more units/jobs or the manager cat PATTERN... Show files and drop-ins of specified units set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit help PATTERN...|PID... Show manual for one or more units reset-failed [PATTERN...] Reset failed state for all, one, or more units list-dependencies [UNIT] Recursively show units which are required or wanted by this unit or by which this unit is required or wantedUnit File Commands: list-unit-files [PATTERN...] List installed unit files enable [UNIT...|PATH...] Enable one or more unit files disable UNIT... Disable one or more unit files reenable UNIT... Reenable one or more unit files preset UNIT... Enable/disable one or more unit files based on preset configuration preset-all Enable/disable all unit files based on preset configuration is-enabled UNIT... Check whether unit files are enabled mask UNIT... Mask one or more units unmask UNIT... Unmask one or more units link PATH... Link one or more units files into the search path revert UNIT... Revert one or more unit files to vendor version add-wants TARGET UNIT... Add 'Wants' dependency for the target on specified one or more units add-requires TARGET UNIT... Add 'Requires' dependency for the target on specified one or more units edit UNIT... Edit one or more unit files get-default Get the name of the default target set-default TARGET Set the default targetMachine Commands: list-machines [PATTERN...] List local containers and hostJob Commands: list-jobs [PATTERN...] List jobs cancel [JOB...] Cancel all, one, or more jobsEnvironment Commands: show-environment Dump environment set-environment VARIABLE=VALUE... Set one or more environment variables unset-environment VARIABLE... Unset one or more environment variables import-environment [VARIABLE...] Import all or some environment variablesManager Lifecycle Commands: daemon-reload Reload systemd manager configuration daemon-reexec Reexecute systemd managerSystem Commands: is-system-running Check whether system is fully running default Enter system default mode rescue Enter system rescue mode emergency Enter system emergency mode halt Shut down and halt the system poweroff Shut down and power-off the system reboot [ARG] Shut down and reboot the system kexec Shut down and reboot the system with kexec exit [EXIT_CODE] Request user instance or container exit switch-root ROOT [INIT] Change to a different root file system suspend Suspend the system hibernate Hibernate the system hybrid-sleep Hibernate and suspend the system suspend-then-hibernate Suspend the system, wake after a period of time and put it into hibernate

「systemctl」コマンドの使い方は以上となります。

Linuxコマンド集へ

(Video) 【初級向け】安全・確実な作業で求められるLinuxコマンド


エンジニアのオンライン学習

エンジニアにおすすめのオンライン教材比較
ITエンジニアが自宅で学習ができるオンラインスクール比較

エンジニアのおすすめ学習「Progate」と「Udemy」比較

Videos

1. 【LinuC/LPIC合格講座】「ブートプロセス」を徹底理解【ITエンジニア基礎入門】#11
(ウズウズカレッジ l デジタル(IT・DX)分野のリスキリング就職)
2. LPIC-1を取得しよう!深掘りシリーズ第四弾 システム起動とSystemd
(【公式】Linux Professional Institute 日本支部 LPI)
3. Linux でシェル スクリプトを開始するための Systemd タイマー ファイルの設定
(Penguin Propaganda)
4. 【Webサーバー講座】第13回 firewall の設定【独り言】
(9cubed)
5. 今更聞けないsystemd入門 (nasa9084) - builderscon tokyo 2019
(builderscon)
6. LPIC-1を取得しよう!深掘りシリーズ第四弾 システム起動とSystemd 2020-10-24 B-3
(OSPN.jp)
Top Articles
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated: 04/06/2023

Views: 6321

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.