Systemd a standard process for managing start-up services in Linux operating systems. It is used for controlling which programs run when the Linux system boots up. It is a system manager and has become the new standard for Linux operating systems. Systemd allows you to create a custom systemd service to run and manage any process. In this tutorial, we will explain how to manage services with systemd on Linux.
Prerequisites
- A server running Linux with Systemd, I will use Ubuntu 20.04 server here.
- A root password is configured the server.
Verify Systemd
By default, Systemd comes pre-installed in all major operating systems. You can verify the installed version of Systemd with the following command:
systemctl --version
You should see the following output:
systemd 245 (245.4-4ubuntu3) +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid
You can find the location of the Systemd with the following command:
whereis systemd
You should get the following output:
systemd: /usr/bin/systemd /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
To find systemctl location, run the following command:
whereis systemctl
You should get the following output:
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
You can also verify whether the Systemd is running or not with the following command:
ps -eaf | grep systemd
You should get the following output:
root 269 1 0 09:57 ? 00:00:01 /lib/systemd/systemd-journald root 294 1 0 09:57 ? 00:00:00 /lib/systemd/systemd-udevd systemd+ 297 1 0 09:57 ? 00:00:00 /lib/systemd/systemd-networkd message+ 319 1 0 09:57 ? 00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only root 329 1 0 09:57 ? 00:00:00 /lib/systemd/systemd-logind systemd+ 382 1 0 09:57 ? 00:00:00 /lib/systemd/systemd-resolved root 1128 1 0 10:47 ? 00:00:00 /lib/systemd/systemd --user root 1177 1152 0 10:48 pts/0 00:00:00 grep --color=auto systemd
You can also analyze the time taken by each process at system boot with the following command:
systemd-analyze blame
You should get the following output:
3.662s csm.service 1.899s apt-daily-upgrade.service 1.798s dev-sda1.device 1.400s systemd-logind.service 1.336s fstrim.service 1.207s systemd-networkd.service 1.131s systemd-resolved.service 856ms man-db.service 842ms systemd-journald.service 415ms e2scrub_reap.service 320ms keyboard-setup.service 295ms networkd-dispatcher.service 239ms systemd-udev-trigger.service
To analyze the critical chain at boot with the following command:
systemd-analyze critical-chain
You should get the following output:
The time when unit became active or started is printed after the "@" character. The time the unit took to start is printed after the "+" character. graphical.target @4.948s ??multi-user.target @4.946s ??csm.service @1.282s +3.662s ??basic.target @1.277s ??sockets.target @1.276s ??dbus.socket @1.274s ??sysinit.target @1.257s ??systemd-update-utmp.service @1.247s +8ms ??systemd-tmpfiles-setup.service @1.228s +17ms ??systemd-journal-flush.service @1.201s +24ms ??systemd-journald.service @356ms +842ms ??systemd-journald.socket @313ms ??system.slice @225ms ??-.slice @225ms
Manage Services with Systemd
Systemctl provides an easier way to manage the systemd services.
To start the Apache service, run the following command:
systemctl start apache2
To restart the Apache service, run the following command:
systemctl restart apache2
To stop the Apache service, run the following command:
systemctl stop apache2
To reload the Apache service, run the following command:
systemctl reload apache2
To enable the Apache service to start after system reboot, run the following command:
systemctl enable apache2
To remove the Apache service from the system start up, run the following command:
systemctl disable apache2
To kill the Apache service, run the following command:
systemctl kill apache2
To mask the Apache service, run the following command:
systemctl mask apache2
To unmask the Apache process, run the following command:
systemctl unmask apache2
To check whether the Apache service is enabled or not, run the following command:
systemctl is-enabled apache2
To check the status of the Apache service, run the following command:
systemctl status apache2
If the service is running, you should get the following output:
? apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-01-09 10:53:12 UTC; 26s ago Docs: https://httpd.apache.org/docs/2.4/ Main PID: 1708 (apache2) Tasks: 55 (limit: 2353) Memory: 5.8M CGroup: /system.slice/apache2.service ??1708 /usr/sbin/apache2 -k start ??1709 /usr/sbin/apache2 -k start ??1710 /usr/sbin/apache2 -k start Jan 09 10:53:12 ubuntu2004 systemd[1]: Starting The Apache HTTP Server...
After renaming or deleting any systemd file, you will need to reload it to apply the changes. To reload the systemd service file, run the following command:
systemctl daemon-reload
Manage Systemd Unit Files
The above commands will be useful for managing single services. However, it can not used to find the current state of the system. There are lot of other commands that are useful for providing this information.
To see a list of all available units, run the following command:
systemctl list-unit-files
You should see a list of all unit files in the following output:
UNIT FILE STATE VENDOR PRESET proc-sys-fs-binfmt_misc.automount static enabled -.mount generated enabled dev-hugepages.mount static enabled dev-mqueue.mount static enabled proc-sys-fs-binfmt_misc.mount disabled enabled sys-fs-fuse-connections.mount static enabled sys-kernel-config.mount static enabled sys-kernel-debug.mount static enabled sys-kernel-tracing.mount static enabled acpid.path enabled enabled ntp-systemd-netif.path enabled enabled systemd-ask-password-console.path static enabled systemd-ask-password-wall.path static enabled session-2.scope transient enabled acpid.service disabled enabled apt-daily-upgrade.service static enabled apt-daily.service static enabled autovt@.service enabled enabled console-getty.service disabled disabled console-setup.service enabled enabled container-getty@.service static enabled cron.service enabled enabled cryptdisks-early.service masked enabled cryptdisks.service masked enabled csm.service enabled enabled dbus-org.freedesktop.hostname1.service static enabled dbus-org.freedesktop.locale1.service static enabled
To see a list of all active units, run the following command:
systemctl list-units
You should see the following output:
UNIT LOAD ACTIVE SUB DESCRIPTION > proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable Fil> sys-devices-pci0000:00-0000:00:03.0-virtio0-net-eth0.device loaded active plugged Virtio network device > sys-devices-pci0000:00-0000:00:04.0-virtio1-net-eth1.device loaded active plugged Virtio network device > sys-devices-pci0000:00-0000:00:05.0-virtio2-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged QEMU_HARDDISK 1 > sys-devices-pci0000:00-0000:00:05.0-virtio2-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged QEMU_HARDDISK > sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS10.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS11.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS12.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS13.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS14.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS15.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS16.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS17.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS18.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS19.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS20.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS21.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS22.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS23.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS24.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS25.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS26.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS27.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS28.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS29.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS30.device loaded active plugged /sys/devices/platform/se> sys-devices-platform-serial8250-tty-ttyS31.device loaded active plugged /sys/devices/platform/se>
To see a list of all failed units, run the following command:
systemctl --failed
To list all services including enabled and disabled services, run the following command:
systemctl list-unit-files --type=service
You should see the following output:
UNIT FILE STATE VENDOR PRESET acpid.service disabled enabled apache-htcacheclean.service disabled enabled apache-htcacheclean@.service disabled enabled apache2.service enabled enabled apache2@.service disabled enabled apt-daily-upgrade.service static enabled apt-daily.service static enabled autovt@.service enabled enabled console-getty.service disabled disabled console-setup.service enabled enabled container-getty@.service static enabled cron.service enabled enabled cryptdisks-early.service masked enabled cryptdisks.service masked enabled csm.service enabled enabled dbus-org.freedesktop.hostname1.service static enabled dbus-org.freedesktop.locale1.service static enabled dbus-org.freedesktop.login1.service static enabled dbus-org.freedesktop.resolve1.service enabled enabled dbus-org.freedesktop.timedate1.service static enabled dbus-org.freedesktop.timesync1.service masked enabled dbus.service static enabled debug-shell.service disabled disabled dmesg.service enabled enabled e2scrub@.service static enabled e2scrub_all.service static enabled e2scrub_fail@.service static enabled e2scrub_reap.service enabled enabled emergency.service static enabled fstrim.service static enabled getty-static.service static enabled getty@.service enabled enabled
You can see the properties of SSH unit, run the following command:
systemctl show sshd.service
You should see the following output:
Id=sshd.service Names=sshd.service Requires=basic.target Wants=system.slice WantedBy=multi-user.target Conflicts=shutdown.target Before=shutdown.target multi-user.target After=syslog.target network.target auditd.service systemd-journald.socket basic.target system.slice Description=OpenSSH server daemon
Manage Sockets with Systemctl
You can also manage sockets using the Systemctl command.
To list all available system sockets, run the following command:
systemctl list-unit-files --type=socket
You should see the following output:
UNIT FILE STATE VENDOR PRESET acpid.socket enabled enabled dbus.socket static enabled ssh.socket disabled enabled syslog.socket static disabled systemd-fsckd.socket static enabled systemd-initctl.socket static enabled systemd-journald-audit.socket static enabled systemd-journald-dev-log.socket static enabled systemd-journald-varlink@.socket static enabled systemd-journald.socket static enabled systemd-journald@.socket static enabled systemd-networkd.socket disabled enabled systemd-rfkill.socket static enabled systemd-udevd-control.socket static enabled systemd-udevd-kernel.socket static enabled
To start the SSH socket, run the following command:
systemctl start ssh.socket
To stop the SSH socket, run the following command:
systemctl stop ssh.socket
To restart the SSH socket, run the following command:
systemctl restart ssh.socket
To check the status of the SSH socket, run the following command:
systemctl status ssh.socket
To enable and disable the SSH socket at boot time, run the following command:
systemctl enable ssh.socket
systemctl disable ssh.socket
Systemctl Additional Commands
You can perform several tasks using the systemctl command. If you want to put the system into single-user mode, run the following command:
systemctl rescue
To halt the system, run the following command:
systemctl halt
To shutdown the system, run the following command:
systemctl poweroff
To restart the system, run the following command:
systemctl reboot
Conclusion
In the above guide, you learned how to manage and control systemd service on Linux. I hope this will helps you to interact with and control your systemd instance. Feel free to ask me if you have any questions.