Cyberithub

30 Useful Commands to Manage Systemd Services through Systemctl

Advertisements

In this article, I will take you through 30 Useful Commands to Manage Systemd Services through Systemctl on Linux. As per Systemd Man Page, systemd is a system and service manager for Linux operating systems. When run as first process on boot (as PID 1), it acts as init system that brings up and maintains userspace services. In modern Linux Systems, systemctl tool provides tons of options to manage Systemd Services.

Important Terminology

Service Units: It start and control daemons and the processes they consist of.

Socket Units: It encapsulate local IPC or network sockets in the system, useful for socket-based activation.

Target Units: It provides synchronization points during boot-up. It is very much similar to runlevel in init.

Device Units: It expose kernel devices in systemd and may be used to implement device-based activation.

Mount Units: It control mount points in the file system.

Automount Units: It provide automount capabilities, for on-demand mounting of file systems as well as parallelized boot-up.

Timer Units: It is useful for triggering activation of other units based on timers.

Swap Units: It is very similar to mount units and encapsulate memory swap partitions or files of the operating system.

Path Units: It may be used to activate other services when file system objects change or are modified.

Slice Units: It may be used to group units which manage system processes (such as service and scope units) in a hierarchical tree for resource management purposes.

Scope Units: It is very similar to service units, but manage foreign processes instead of starting them as well.

30 Useful Commands to Manage Systemd Services through Systemctl 1

Top 20 Yum Command Examples in RedHat/CentOS 7

Top 25 UFW Firewall Commands Every Linux Admin Should Know

40 Best Examples of Find Command in Linux

1. Check Systemctl Version

If you want to check systemctl version, you need to use --version option to check that.

[root@localhost ~]# systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

NOTE:

Please note that here I am using root user to run all the commands. You can either use root user or any other user with sudo access to run all commands. If you do not know about providing sudo access to user, then you can check How to add user into Sudoers.

2. Check Systemd Manager Environment Block

To check systemd manager environment block, you need to use systemctl show-environment command.

[root@localhost ~]# systemctl show-environment
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

3. List All Systemd Units

In order to check all the current systemd units, you need to run systemctl -a command to check that.

[root@localhost ~]# systemctl -a
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
dev-block-8:2.device loaded active plugged LVM PV FLMd2O-XdOW-xpry-EzDj-Lc1L-F5Oh-Sqj5cV on /dev/sda2 2
dev-cdrom.device loaded active plugged VBOX_CD-ROM
dev-centos-root.device loaded active plugged /dev/centos/root
dev-centos-swap.device loaded active plugged /dev/centos/swap
dev-disk-by\x2did-ata\x2dVBOX_CD\x2dROM_VB2\x2d01700376.device loaded active plugged VBOX_CD-ROM
dev-disk-by\x2did-ata\x2dVBOX_HARDDISK_VB5309842c\x2d3a56194e.device loaded active plugged VBOX_HARDDISK
dev-disk-by\x2did-ata\x2dVBOX_HARDDISK_VB5309842c\x2d3a56194e\x2dpart1.device loaded active plugged VBOX_HARDDISK 1
dev-disk-by\x2did-ata\x2dVBOX_HARDDISK_VB5309842c\x2d3a56194e\x2dpart2.device loaded active plugged LVM PV FLMd2O-XdOW-xpry-EzDj-Lc1L-F5Oh-Sqj5cV on /dev/
dev-disk-by\x2did-dm\x2dname\x2dcentos\x2droot.device loaded active plugged /dev/disk/by-id/dm-name-centos-root
dev-disk-by\x2did-dm\x2dname\x2dcentos\x2dswap.device loaded active plugged /dev/disk/by-id/dm-name-centos-swap

4. Show all Unit Types

If you want to check all unit types, you need to use systemctl --show-types command to check that. In the output you can see all the devices, targets, mounts, sockets etc.

[root@localhost ~]# systemctl --show-types
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
sys-devices-pci0000:00-0000:00:01.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged VBOX_CD-ROM
sys-devices-pci0000:00-0000:00:03.0-net-enp0s3.device loaded active plugged 82540EM Gigabit Ethernet Controller (PRO/1000 MT Desktop Adapter)
sys-devices-pci0000:00-0000:00:05.0-sound-card0.device loaded active plugged 82801AA AC'97 Audio Controller
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged VBOX_HARDDISK 1
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged LVM PV FLMd2O-XdOW-xpry-EzDj-Lc1L-F5Oh-Sqj5cV
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged VBOX_HARDDISK

5. Show Any Failed Units

If you to see any of the failed units in your Server, you need to use --state=failed option as shown below. It will show all the units currently in failed state. This is a very important command to remember during troubleshooting of Linux services.

[root@localhost ~]# systemctl --state=failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● chronyd.service loaded failed failed NTP client/server
● cups.service loaded failed failed CUPS Printing Service
● docker-storage-setup.service loaded failed failed Docker Storage Setup
● epmd@0.0.0.0.service loaded failed failed Erlang Port Mapper Daemon

6. Check Service Status

If you want to check your service status, the easiest way is to check through systemctl status docker command. To know more about Docker Installation steps on CentOS 7, you can visit How to Install Docker on CentOS 7 with Best Practices.

[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2020-01-25 16:11:36 EST; 2h 31min ago
Docs: http://docs.docker.com
Main PID: 1258 (dockerd-current)
Tasks: 18
CGroup: /system.slice/docker.service
├─1258 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdr...
└─1350 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-d...

7. Stopping a Service

If you want to stop a running service, you need to use systemctl stop docker command. In a traditional way, you can also run service docker stop command and stop your service. Both will perform the same task.

[root@localhost ~]# systemctl stop docker

After stopping your service, you need to check the status of Service if it is stopped or not by using below command.

[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sat 2020-01-25 18:44:39 EST; 8s ago
Docs: http://docs.docker.com
Process: 1258 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=0/SUCCESS)
Main PID: 1258 (code=exited, status=0/SUCCESS)

NOTE:

Please note that here I am using docker service for this entire tutorial. You might be running different service in your Server. You can use any service you want as per your requirement. To know more about managing ports in docker, you can visit How to manage ports in Docker.

8. Starting a Service

To start a service which is currently in either stopped state or in not running state, you can do systemctl start docker to start docker service.

[root@localhost ~]# systemctl start docker

After starting the service, you can check the status by running systemctl status docker command.

[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2020-01-25 18:45:26 EST; 1s ago
Docs: http://docs.docker.com
Main PID: 24340 (dockerd-current)
Tasks: 17
CGroup: /system.slice/docker.service
├─24340 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupd...
└─24345 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-...

9. Restarting a Service

If you want to restart your docker service after doing any configuration changes, you can do that by running systemctl restart docker command.

[root@localhost ~]# systemctl restart docker

After restarting the docker service, you can again check the status of the service by running systemctl status docker command.

10. Checking Service Active State

If want to check the active state of your service, you need to use systemctl is-active docker command. This command can be very handy if you just wanted to know the running state of your service.

[root@localhost ~]# systemctl is-active docker
active

11. List all Units and Its State

If you want to check all units and its state, you need to use systemctl list-unit-files command.

[root@localhost ~]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-fs-nfsd.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
var-lib-nfs-rpc_pipefs.mount static
brandbot.path disabled

12. Enable Services without reloading Configuration

Sometimes you might have a scenario where you do not want to reload the configuration while enabling the service. In those scenarios, --no-reload option will be very useful.

[root@localhost ~]# systemctl enable --no-reload docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

13. Disable Services without reloading Configuration

If you want to disable your services without reloading configuration, then you need to use --no-reload along with disable argument as shown below.

[root@localhost ~]# systemctl disable --no-reload docker
Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.

14. List out all Service Dependencies

If you want to list out a Service dependencies then you need to use list-dependencies option. Here to check all the dependent services for docker service, you need to use systemctl list-dependencies docker command.

[root@localhost ~]# systemctl list-dependencies docker
docker.service
● ├─docker-cleanup.timer
● ├─docker-storage-setup.service
● ├─system.slice
● └─basic.target
● ├─iptables.service
● ├─microcode.service
● ├─rhel-dmesg.service
● ├─selinux-policy-migrate-local-changes@targeted.service
● ├─paths.target
● ├─slices.target
● │ ├─-.slice
● │ └─system.slice

........................................................................................................

If you want to check the dependencies in the list form instead in tree form, then you need to use --plain option along with list-dependencies option.

[root@localhost ~]# systemctl list-dependencies --plain docker
docker.service
docker-cleanup.timer
docker-storage-setup.service
system.slice
basic.target
iptables.service
microcode.service
rhel-dmesg.service
selinux-policy-migrate-local-changes@targeted.service
paths.target


..................................................................................................................

15. Check System Operational State

If you want to verify the System Operational State, then you need to use systemctl is-system-running command.

[root@localhost ~]# systemctl is-system-running
degraded

16. Power Off System through Systemctl commands

If you want to poweroff your system using systemctl command, then you need to run systemctl poweroff command as shown below.

[root@localhost ~]# systemctl poweroff

17. Reboot System through Systemctl commands

If you want to reboot your system using systemctl command, then you need to run systemctl reboot command as shown below.

[root@localhost ~]# systemctl reboot

18. Reload Systemd Manager Configuration

If you want to reload your daemon using systemctl command, then you need to run systemctl daemon-reload command as shown below.

[root@localhost ~]# systemctl daemon-reload

19. Mask and Unmask a Service

Sometimes you do not want your service to start at all, in those cases you can just mask the service which will not allow the service to start at all.

[root@localhost ~]# systemctl mask docker
Created symlink from /etc/systemd/system/docker.service to /dev/null.

If you want to disable the masking of your service, you can simply unmask the service by running systemctl unmask docker command.

[root@localhost ~]# systemctl unmask docker
Removed symlink /etc/systemd/system/docker.service.

20. Show all properties of a Unit

If you want to check all the current properties of a unit, you need to use systemctl show docker command.

[root@localhost ~]# systemctl show docker
Type=notify
Restart=on-abnormal
NotifyAccess=main
RestartUSec=100ms
TimeoutStartUSec=0
TimeoutStopUSec=1min 30s
WatchdogUSec=0
WatchdogTimestamp=Tue 2020-01-28 22:36:20 EST
WatchdogTimestampMonotonic=96043556017
StartLimitInterval=10000000
StartLimitBurst=5
StartLimitAction=none
FailureAction=none


............................................................................................................................................

21. Check Default Target in Your System

If you want to check default target in your system, you can do that by using get-default option as shown below. This command also means that show me the current default runlevel set in this Server.

[root@localhost ~]# systemctl get-default
multi-user.target

22. List all the Target in Your System

If you want to list all the target in your system, you can do that by using option --type=target along with list-unit-files option.

[root@localhost ~]# systemctl list-unit-files --type=target
UNIT FILE STATE
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static
ctrl-alt-del.target disabled
default.target enabled
emergency.target static
final.target static
getty-pre.target static


.............................................................................................................................................

23. Set Default Target in Your System

Let's you want to change your default target in your server. For that you need to use set-default option to change your default target. In this example I am changing my default target to printer.default.

[root@localhost ~]# systemctl set-default printer.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/printer.target.

Now you can check here if the default target is changed or not.

[root@localhost ~]# systemctl get-default
printer.target

24.  List all Service Unit Types Using Systemctl list services

To check all systemctl list services, you need to use --type=service along with list-unit-files option to check that as shown below.

[root@localhost ~]# systemctl list-unit-files --type=service
UNIT FILE STATE
arp-ethers.service disabled
atd.service enabled
auditd.service enabled
auth-rpcgss-module.service static
autovt@.service enabled
blk-availability.service disabled
brandbot.service static
chrony-dnssrv@.service static
chrony-wait.service disabled
chronyd.service enabled
conntrackd.service disabled
console-getty.service disabled


.....................................................................................................................

25. List all Socket Unit Types

If you want to check all the socket unit types, you can do that by using --type=socket option along with list-unit-files option as shown below.

[root@localhost ~]# systemctl list-unit-files --type=socket
UNIT FILE STATE
cups.socket enabled
dbus.socket static
dm-event.socket enabled
epmd.socket disabled
epmd@.socket disabled
iscsid.socket enabled
iscsiuio.socket enabled
lvm2-lvmetad.socket enabled
lvm2-lvmpolld.socket enabled
qemu-pr-helper.socket disabled
rpcbind.socket enabled
rsyncd.socket disabled
sshd.socket disabled
syslog.socket static
systemd-initctl.socket static
systemd-journald.socket static
systemd-shutdownd.socket static
systemd-udevd-control.socket static
systemd-udevd-kernel.socket static
virtlockd-admin.socket disabled
virtlockd.socket enabled
virtlogd-admin.socket disabled
virtlogd.socket enabled

23 unit files listed.

26. Check Target Status

To check the status of any target, you can use systemctl status <target name> command. In this example, I am checking the status of remote-fs.target as shown below.

[root@localhost ~]# systemctl status remote-fs.target
● remote-fs.target - Remote File Systems
Loaded: loaded (/usr/lib/systemd/system/remote-fs.target; enabled; vendor preset: enabled)
Active: active since Mon 2020-01-27 19:55:47 EST; 1 day 3h ago
Docs: man:systemd.special(7)

27. Isolate Target in Your System

You might have seen changing the run level through init scripts. In Systemd it is done by using isolate option with target. Here Target is nothing but the replacement of runlevel from init under which all the services will start. So using Run Level 3 in init is as same as using multi-user.target in Systemd which is similar way of saying to switch my mode to Multi User GUI Mode.

[root@localhost ~]# systemctl isolate multi-user.target

28. List all Timers 

To check all the timers currently running for your Linux Services, you need to use systemctl list-timers --all command. Timers are generally used to start your services. It is same as using cron job in your system to perform certain tasks at certain point of time.

[root@localhost ~]# systemctl list-timers --all
NEXT LEFT LAST PASSED UNIT ACTIVATES
Wed 2020-01-29 00:00:00 EST 42min left Tue 2020-01-28 23:00:08 EST 17min ago docker-cleanup.timer docker-cleanup.service
Wed 2020-01-29 00:00:00 EST 42min left Tue 2020-01-28 00:00:05 EST 23h ago unbound-anchor.timer unbound-anchor.service
Wed 2020-01-29 20:10:45 EST 20h left Tue 2020-01-28 20:10:45 EST 3h 6min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
n/a n/a n/a n/a systemd-readahead-done.timer systemd-readahead-done.service

4 timers listed.

29. Edit Service through Systemctl

If you would like to override some of the configuration setting of any service, you need to use edit option with systemctl. Notice override.conf file from the output which it is creating to override the service docker configuration.

[root@localhost ~]# systemctl edit docker
Editing "/etc/systemd/system/docker.service.d/override.conf"

30. Check Other Options with Systemctl

To check all the other functionality available with systemctl commands, you can use -h option as shown below.

[root@localhost ~]# systemctl -h
systemctl [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
-H --host=[USER@]HOST
Operate on remote host
-M --machine=CONTAINER
Operate on local container

 

Also Read: What is GlusterFS Shared Storage and types of Volumes

Leave a Comment