Using Systemd to Manage Services, Run Levels and Logs on a Linux System
Systemd is not only a service manager but also a system manager for Linux. It’s designed to be backward compatible with SysV init scripts and used by many popular Linux distributions like Debian 8+, Ubuntu 15.04+, Fedora, Redhat 7+ /CentOS 7+, Arch Linux, OpenSUSE. So it will be very helpful to know the usage of Systemd.
This tutorial shows you some basic commands that will be useful to manage services, run levels and logs.
To check systemd version on your Linux distribution, run
Systemd can help you start, stop, restart services and also allows you to autostart services at boot time.
To list systemd services that are active, run
systemctl list-units --type service
Check the status of a specific service, for example, SSH daemon.
systemctl status ssh
To start a service, use the
sudo systemctl start ssh
Check if it’s active (running)
systemctl is-active ssh
Enable a service to autostart at boot time
sudo systemctl enable ssh
Check if it’s enabled
systemctl is-enabled ssh
Prevent a service from starting up at boot time
sudo systemctl disable ssh
Stop a service
sudo systemctl stop ssh
To prevent a service from starting maually, use the
sudo systemctl mask ssh
Masked services can’t be started with
systemctl start command until it’s unmasked.
sudo systemctl unmask ssh
Managing Run Levels
The concept of run levels is replaced by targets in systemd. The
multi-user.target is equivalent to run level 3 and the
graphical.target is equivalent to run level 5. You can still use the runlevel command to show the current run level.
Use the following command to check the status of the default target, which by default is usually the graphical target (run level 5).
systemctl status default.target
systemctl isolate command to change target. For example, change to the multi-user.target (run level 3):
sudo systemctl isolate multi-user.target
And to change back to graphical target (run level 5):
sudo systemctl isolate graphical.target
To set multi-user.target as the default target, use the following command:
sudo systemctl set-default multi-user.target
This command creates a symbolic link.
If you reboot now, you will be taken to multi-user target.
To list active targets, run
systemctl list-units --type target
The systemd software suite includes a
journalctl utility which can be used to manage logs on Linux. Linux traditionally saves logs under
/var/log/ directory. On a Linux distribution with systemd, you may not be able to find logs such as postfix mail logs (/var/log/mail.log) in that directory.
You can view recent logs with:
grep to search for logs related to your search term
sudo journalctl | grep <search term>
View logs since the current boot
sudo journalctl -b
View logs since the previous boot
sudo journalctl -b -1
Here’s a little trick that show logs being generated in real time.
sudo journalctl -f
Show logs of a unit, for instance, the ssh service unit.
sudo journalctl -u ssh
To view Postfix logs, run
sudo journalctl -u postfix