systemctlの使い方。サービス一覧を見たい。起動・停止したい。Rocky,Alma,CentOS

Linux

<景品表示法に基づく表記> 本サイトのコンテンツには、商品プロモーションが含まれている場合があります。

Rocky,Alma,CentOSのサービスは、systemctl コマンドで管理します。

全てのサービスの設定ファイルは、/usr/lib/systemd/system ディレクトリにあります。
ランレベル毎に保存されるディレクトリでは、/usr/lib/systemd/system ディレクトリのファイルにシンボリック・リンクされます。

systemctl の構成

systemdにてサービスを追加する際の手順をまとめたものです。

 

ランレベルに応じたディレクトリは、/etc/systemd/system です。

# ls -l /etc/systemd/system/
合計 16
-rw-r--r--  1 root root  430  4月  5 20:41 acronis_mms.service
-rw-r--r--  1 root root  270  4月  5 20:41 acronis_schedule.service
drwxr-xr-x. 2 root root   57  2月 15 17:43 basic.target.wants
lrwxrwxrwx. 1 root root   46  2月 15 17:42 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root   57  2月 15 17:42 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx. 1 root root   37  2月 15 17:48 default.target -> /lib/systemd/system/multi-user.target
drwxr-xr-x. 2 root root   87  2月 15 17:42 default.target.wants
drwxr-xr-x. 2 root root   32  2月 15 17:42 getty.target.wants
drwxr-xr-x. 2 root root   35  2月 15 17:42 local-fs.target.wants
drwxr-xr-x. 2 root root 4096  6月 12 17:32 multi-user.target.wants
drwxr-xr-x. 2 root root   48  2月 15 17:42 network-online.target.wants
drwxr-xr-x. 2 root root   29  2月 15 17:42 sockets.target.wants
drwxr-xr-x. 2 root root  217  2月 15 17:43 sysinit.target.wants
drwxr-xr-x. 2 root root   44  2月 15 17:42 system-update.target.wants
drwxr-xr-x  2 root root   29  2月 15 18:07 vmtoolsd.service.requires
-rw-r--r--  1 root root  217  2月 15 18:20 vpnserver.service

ランレベル3のディレクトリは、/etc/systemd/system/multi-user.target.wants です。

# ls -al
合計 8
drwxr-xr-x.  2 root root 4096  6月 12 17:32 .
drwxr-xr-x. 12 root root 4096  6月 12 17:32 ..
lrwxrwxrwx.  1 root root   46  2月 15 17:42 NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx   1 root root   39  6月 12 17:32 acronis_mms.service -> /etc/systemd/system/acronis_mms.service
lrwxrwxrwx   1 root root   44  6月 12 17:32 acronis_schedule.service -> /etc/systemd/system/acronis_schedule.service
lrwxrwxrwx.  1 root root   38  2月 15 17:43 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx.  1 root root   39  2月 15 17:43 chronyd.service -> /usr/lib/systemd/system/chronyd.service
lrwxrwxrwx.  1 root root   37  2月 15 17:42 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx.  1 root root   42  2月 15 17:43 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service
lrwxrwxrwx.  1 root root   37  2月 15 17:43 kdump.service -> /usr/lib/systemd/system/kdump.service
lrwxrwxrwx   1 root root   36  2月 15 18:08 nrpe.service -> /usr/lib/systemd/system/nrpe.service
lrwxrwxrwx   1 root root   36  2月 15 18:08 ntpd.service -> /usr/lib/systemd/system/ntpd.service
lrwxrwxrwx.  1 root root   39  2月 15 17:43 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx.  1 root root   40  2月 15 17:42 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx.  1 root root   46  2月 15 17:42 rhel-configure.service -> /usr/lib/systemd/system/rhel-configure.service
lrwxrwxrwx.  1 root root   39  2月 15 17:43 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx   1 root root   37  2月 15 18:08 snmpd.service -> /usr/lib/systemd/system/snmpd.service
lrwxrwxrwx.  1 root root   36  2月 15 17:43 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx.  1 root root   37  2月 15 17:43 tuned.service -> /usr/lib/systemd/system/tuned.service
lrwxrwxrwx   1 root root   40  2月 15 18:07 vmtoolsd.service -> /usr/lib/systemd/system/vmtoolsd.service
lrwxrwxrwx   1 root root   37  2月 15 18:21 vpnserver.service -> /etc/systemd/system/vpnserver.service
lrwxrwxrwx   1 root root   38  2月 15 18:07 vsftpd.service -> /usr/lib/systemd/system/vsftpd.service

systemctl のコマンド

起動中のサービス一覧(list-units)

起動中のサービス一覧を見たい場合は、以下のコマンドを実行します。

# systemctl list-units --type=service
# systemctl list-units --type=service
  UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
  acronis_mms.service                loaded active running Acronis machine management service
  auditd.service                     loaded active running Security Auditing Service
  chronyd.service                    loaded active running NTP client/server
  crond.service                      loaded active running Command Scheduler
  dbus.service                       loaded active running D-Bus System Message Bus
  [email protected]                 loaded active running Getty on tty1
● kdump.service                      loaded failed failed  Crash recovery kernel arming
  kmod-static-nodes.service          loaded active exited  Create list of required static device nodes for the current kernel
  lvm2-lvmetad.service               loaded active running LVM2 metadata daemon
  lvm2-monitor.service               loaded active exited  Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling
  lvm2-pvscan@8:2.service            loaded active exited  LVM2 PV scan on device 8:2
  network.service                    loaded active exited  LSB: Bring up/down networking
  NetworkManager-wait-online.service loaded active exited  Network Manager Wait Online
  NetworkManager.service             loaded active running Network Manager
  nrpe.service                       loaded active running Nagios Remote Program Executor
  polkit.service                     loaded active running Authorization Manager
  postfix.service                    loaded active running Postfix Mail Transport Agent
  rhel-dmesg.service                 loaded active exited  Dump dmesg to /var/log/dmesg
  rhel-domainname.service            loaded active exited  Read and set NIS domainname from /etc/sysconfig/network
  rhel-import-state.service          loaded active exited  Import network configuration from initramfs
  rhel-readonly.service              loaded active exited  Configure read-only root support
  rsyslog.service                    loaded active running System Logging Service
  snmpd.service                      loaded active running Simple Network Management Protocol (SNMP) Daemon.
  sshd.service                       loaded active running OpenSSH server daemon
  systemd-journal-flush.service      loaded active exited  Flush Journal to Persistent Storage
  systemd-journald.service           loaded active running Journal Service
  systemd-logind.service             loaded active running Login Service
  systemd-random-seed.service        loaded active exited  Load/Save Random Seed
  systemd-remount-fs.service         loaded active exited  Remount Root and Kernel File Systems
  systemd-sysctl.service             loaded active exited  Apply Kernel Variables
  systemd-tmpfiles-setup-dev.service loaded active exited  Create Static Device Nodes in /dev
  systemd-tmpfiles-setup.service     loaded active exited  Create Volatile Files and Directories
  systemd-udev-trigger.service       loaded active exited  udev Coldplug all Devices
  systemd-udevd.service              loaded active running udev Kernel Device Manager
  systemd-update-utmp.service        loaded active exited  Update UTMP about System Boot/Shutdown
  systemd-user-sessions.service      loaded active exited  Permit User Sessions
  systemd-vconsole-setup.service     loaded active exited  Setup Virtual Console
  tuned.service                      loaded active running Dynamic System Tuning Daemon
  vgauthd.service                    loaded active running VGAuth Service for open-vm-tools
  vmtoolsd.service                   loaded active running Service for virtual machines hosted on VMware
  vpnserver.service                  loaded active running SoftEther VPN Server
  vsftpd.service                     loaded active running Vsftpd ftp daemon

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

42 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

全てのサービス一覧(list-unit-files)

定義されているサービスを全て見たい場合は、以下のコマンドを実行します。

# systemctl list-unit-files --type=service

サービスの起動

サービスを起動したい場合は、以下のようにコマンドを実行します。(SSHの場合)

# systemctl start sshd

サービスの停止

サービスを停止したい場合は、以下のようにコマンドを実行します。(SSHの場合)

# systemctl stop sshd

サービスの状態

サービスの状態を確認したい場合は、以下のようにコマンドを実行します。(SSHの場合)

# systemctl status sshd

サービスの自動起動

サービスの自動起動したい場合は、以下のようにコマンドを実行します。(SSHの場合)

# systemctl enable sshd

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

サービスの自動起動したい場合は、以下のようにコマンドを実行します。(SSHの場合)

# systemctl disable sshd