Author: Jivendra SahLast Updated: Fri, Oct 14, 2022
Systemd is a very popular
init system used by most Linux distributions. It replaced the older SysVinit system. In this article, you will get a basic idea of systemd and its ecosystem, and you will learn to perform the most common tasks using systemd.
Systemd is a Linux initialization system that starts all the other Linux processes to bring the system to a usable state. It manages the system state and processes. It is not a binary file, but a collection of programs and libraries that serve different functions. A few systemd components:
journald - system logging
logind - session management
networkd - network configuration management
Systemd operates on units. These units have unit files. Unit file stores important data about the unit which systemd requires. There are many types of unit files, some of which are:
.service (defines processes)
.device (defines hardware and devices)
.socket (defines inter-process communication sockets).
Systemd provides systemctl command to manage services and processes.
You can start, stop, restart, or reload a unit using the
Note: This guide uses
nginx.serviceunit to show command usage. Install nginx using your package manager if you want to use the commands in this guide.
Start a unit:
# systemctl start nginx.service
Stop a unit:
# systemctl stop nginx.service
Restart a service:
# systemctl restart nginx.service
Reload the configuration file of a unit without stopping it if the unit is capable of doing so:
# systemctl reload nginx.service
reload-or-restart reloads the unit if possible, otherwise restarts it:
# systemctl reload-or-restart nginx.service
Note: While using unit files, specify the name of the file along with the file type extension. For example, if you are specifying a socket file, use
socket_name.socketin the command. If you skip the file extension, systemd will treat the unit file as a
If you want a unit to start at boot, use
enable command. To do the opposite, use the
# systemctl enable nginx.service # systemctl disable nginx.service
Note: This command does not start or stop the service in the current session. To do so, use the
Making a unit prevents any user to start and enable it. Use the
mask command to mask a unit.
# systemctl mask nginx.service
Note: Like the
maskwill not stop a currently active unit. To stop the unit in the current session, use the
Checking the status of a unit is essential to managing your system.
status command lists the current status and some other details of the unit.
$ systemctl status nginx.service
â nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-07-01 17:01:57 UTC; 2 months 18 days ago Docs: man:nginx(8) Main PID: 745 (nginx) Tasks: 2 (limit: 1081) Memory: 8.2M CGroup: /system.slice/nginx.service ââ 745 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ââ719388 nginx: worker process
This output lists some important details about the unit, like:
Current state of the unit
If the unit is "enabled" or "disabled" at system boot.
If all you want to check is whether the unit is currently running or not, use the
$ systemctl is-active nginx.service
This will either return
inactive depending on the current state of the unit.
All the active (currently running) units define the state of a system. Systemd has commands that provide information about the state of your system.
To see all the active units, use:
$ systemctl list-units
Note: You can use the
systemctlcommand without the
list-unitsand you will get the same output.
To see a list of unit files that systemd loaded or attempted to load, append the
$ systemctl list-units --all
Note: This command excludes those units which systemd never attempted to load.
To see a full list of installed unit files on your system, use:
$ systemctl list-all-units
The systemd suite of applications includes a
journald service that collects and stores different types of log data from different parts of the system, like:
Standard output and standard error from service units
Kernel log messages from the kernel
Audit records from kernel audit subsystem
Other system log messages.
To see all the log entries stored by journald, including the ones generated in the previous boot sessions, use:
Note: Not all systems store log data from previous boot sessions. To change this behavior, edit the
/etc/systemd/journald.conffile and set the value of
persistentto store logs from previous boot sessions.
autoto disable this persistent storage of logs.
noneto completely disable logging of any data.
To view log data from the current boot session, append the
journalctl command with
$ journalctl -b
To view kernel logs, use the
$ journalctl -k
To view logs of a unit, use the
-u flag along with the unit's name.
$ journalctl -u nginx.service
To view the output in reverse order, use the
$ journalctl -u nginx.service -r
This will display the output in reverse order, showing the latest logs first. Also, you can use multiple flags together. For example, you can use the
-r flag and
-b flag together to list the logs generated in the current boot session in reverse order.
$ journalctl -u nginx.service -b -r
Unit files contain information about the unit that systemd uses to run and manage the unit. You can use the
cat command to view the contents of a unit file.
$ systemctl cat nginx.service
To see all the properties and details of a unit that are specific to your system, use
$ systemctl show nginx.service
Some unit files require other unit files to be in the active state before they can run. To see a list of dependency units of a unit file, use:
$ systemctl list-dependencies nginx.service
Some dependency units may depend on other units. To see a recursive list of dependency units along with their dependency units, use the
$ systemctl list-dependencies -all nginx.service
To modify a unit file, use the
# systemctl edit nginx.service
This creates a new directory
nginx.service.d (directory naming format:
unit.type.d) in the
/etc/systemd/system/ directory and creates a
.conf file (unit snippet file) in this directory. This file adds new properties and overrides the existing properties of the original unit file. To undo the modifications made on a unit file, use the
# systemctl revert unit.type
Systemd groups multiple units which serve a common goal into a Target file. For example,
graphical.target groups all the units required to initialize the graphical user interface. One target can depend on other units. Calling a target file brings the system to a specific state.
To get a list of all the available target files on your system, use
$ systemctl list-unit-files --type=target
Note: This guide uses basic.target target file to show command usage.
To see all the dependencies of a target, use:
$ systemctl list-dependencies basic.target
The default target file is the first target file that systemd calls during the system boot process. To know the default target of your system use:
$ systemctl get-default
To change the default target of your system, use:
# systemctl set-default multi-user.target
Systemctl is powerful enough to change the major states of a server, like the power state of your system. Some useful commands are
sudo systemctl poweroff - Power off the server
sudo systemctl reboot - Reboot the server
sudo systemctl suspend - Suspend the system
sudo systemctl rescue - boot into rescue mode