Share your love
Systemd Unit Files Full Guide with Examples – 5 Easy Steps
In this tutorial, we want to discuss a Full Guide about Systemd Unit Files with Examples. Systemd is the default init system for Linux distributions and also it is a service manager. The word Unit refers to resources that Systemd knows how to control, manage, and operate. The Unit Files are the configuration files that define the Units.
Now follow the steps below provided by the Orcacore website to get detailed information about Systemd Unit Files Full Guide with Examples.
Table of Contents
Systemd Unit Files Full Guide with Examples
To complete the Systemd Unit Files Full Guide with Examples, you must have access to your Linux distribution as a root or non-root user with sudo privileges and follow the steps below.
Note: To get an initial setup on Linux distros, you can visit the Orcacore website and search for initial server guides.
Step 1 – List Available Unit Files Types
In the Linux distros, there are 11 types of units. These unit types work together and bring functionality to your system. You can use the following command to list your unit types:
systemctl -t help
In your output, you should see:
Output
Available unit types:
service
mount
swap
socket
target
device
automount
timer
path
slice
scope
Each of these unit types does a task in your Linux system. In the below step of Systemd Unit Files Full Guide with Examples, we provide a list of what they exactly do.
Unit Types | Definition |
service | Service managing processes like start, restart, and stop. |
mount | Control the file-system mount point. |
swap | encapsulate, activate, and deactivate the swap partition. |
socket | Activate a service when the service receives incoming traffic on a listening socket. |
target | Control synchronization of unit files during the boot process. |
device | Implement device-based activation such as a device driver. |
automount | Provide and Control on-demand mounting of file systems. |
timer | Schedule the activation of other units. |
path | Monitor files/directories and activate/deactivate a service if the specified file or directory is accessed. |
slice | Manage system resources such as CPU, and memory. |
scope | organize and manage foreign processes. |
Step 2 – Structure of Unit Files in Systemd
At this step of the Systemd Unit Files Full Guide with Examples, we want to show you the general structure of unit files. These unit files are organized with sections defined in [–]. As you must know, section names are case-sensitive. Each section has its variables or directives and their value.
[Section]
Directive1=value
Directive2=value
. . .
Most of the unit files start with [Unit] which stores the unit’s metadata and configures its relationship with other units. Also, it mostly ends with the [Install] section which specifies whether a unit is enabled or disabled.
Step 3 – Systemd Unit Files Location
The default systemd unit file location is in the /lib/systemd/system directory. It stores the OS files and programs you have installed.
Also, the /run/systemd/system stores runtime unit definitions. systemd uses this location to hold files dynamically created at runtime.
Step 4 – Manage Systemd Unit Files with Examples
At this step of Systemd Unit Files Full Guide with Examples, you can easily use your systemd unit files to manage your services like start, stop, or restart a service. Follow the steps below to see how it works.
Check Unit Files Status
The systemctl status command will use to check whether a service is active or inactive on your system.
To check active and inactive units, you can run the command below:
sudo systemctl status
If you want to check the status of a specific unit type, you can run the command below:
sudo systemctl list-units -t [unit file type]
For example, check the mount type status:
sudo systemctl list-units -t mount
Output
UNIT LOAD ACTIVE SUB DESCR>
-.mount loaded active mounted Root >
dev-hugepages.mount loaded active mounted Huge >
dev-mqueue.mount loaded active mounted POSIX>
run-credentials-systemd\x2dsysusers.service.mount loaded active mounted /run/>
run-snapd-ns-lxd.mnt.mount loaded active mounted /run/>
run-snapd-ns.mount loaded active mounted /run/>
run-user-0.mount loaded active mounted /run/>
snap-core20-1405.mount loaded active mounted Mount>
snap-core20-1587.mount loaded active mounted Mount>
snap-lxd-22923.mount loaded active mounted Mount>
snap-lxd-24322.mount loaded active mounted Mount>
snap-snapd-15534.mount loaded active mounted Mount>
snap-snapd-16292.mount loaded active mounted Mount>
sys-fs-fuse-connections.mount loaded active mounted FUSE >
sys-kernel-config.mount loaded active mounted Kerne>
sys-kernel-debug.mount loaded active mounted Kerne>
sys-kernel-tracing.mount loaded active mounted Kerne>
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.
To check enabled and disabled unit types, you can use the command below:
sudo systemctl list-unit-files
Output
UNIT FILE STATE VENDOR PRESET
proc-sys-fs-binfmt_misc.automount static -
-.mount generated -
dev-hugepages.mount static -
dev-mqueue.mount static -
proc-sys-fs-binfmt_misc.mount disabled disabled
snap-core20-1405.mount enabled enabled
apport-autoreport.path enabled enabled
systemd-ask-password-console.path static -
systemd-ask-password-plymouth.path static -
systemd-ask-password-wall.path static -
session-1.scope transient -
apparmor.service enabled enabled
apport-autoreport.service static -
apport-forward@.service static -
apport.service generated -
apt-daily-upgrade.service static -
apt-daily.service static -
autovt@.service alias -
...
Also, for a specific unit type, you can use the command below:
sudo systemctl list-unit-files | grep [unit name]
Start a Service in Systemd
At this step of Systemd Unit Files Full Guide with Examples, you can use the systemctl start command to activate your service. For example, we have Apache installed on our Ubuntu server. To start the service, you can use the command below:
sudo systemctl start apache2
Then, you can use the systemctl status command to check whether it is activated or not:
sudo systemctl status apache2
Output
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset:>
Active: active (running) since Thu 2023-09-07 10:50:15 UTC; 4min 30s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 12204 (apache2)
Tasks: 55 (limit: 4575)
Memory: 5.1M
CPU: 111ms
CGroup: /system.slice/apache2.service
...
Stop a Service in Systemd
At this step of Systemd Unit Files Full Guide with Examples, If you plan to stop your desired service, you can easily use the systemctl stop command. For example, we stop our Apache service by using the command below:
sudo systemctl stop apache2
Then, check your service status it should be dead and inactive:
Output
○ apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset:>
Active: inactive (dead) since Thu 2023-09-07 10:57:34 UTC; 3s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 12625 ExecStop=/usr/sbin/apachectl graceful-stop (code=exited, statu>
Main PID: 12204 (code=exited, status=0/SUCCESS)
CPU: 206ms
...
Restart and Reload a Service in Systemd
The restart process is used to stop and start the service again. The reload process is used when you want to apply the new settings to a service.
To restart a service, for example, Apache, you can use the command below:
sudo systemctl restart apache2
For reloading the service, you can run the command below:
sudo systemctl reload apache2
Enable a Service in Systemd Unit Files
If you want your service to start on the next boot time automatically, you must enable your service. For example, to enable the Apache service, you can use the command below:
sudo systemctl enable apache2
Output
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable apache2
Also, you can use a command to start and enable the service at the same time. To do this, you can use the command below:
sudo systemctl enable --now apache2
Disable a Service in Systemd
If you want to stop the service in the next boot, you need to disable it. For example, to disable Apache, you can run the command below:
sudo systemctl disable apache2
Output
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable apache2
Check your service status, it should be activated but not enabled:
Output
Loaded: loaded (/lib/systemd/system/apache2.service; disabled; vendor preset>
Active: active (running) since Thu 2023-09-07 11:06:06 UTC; 11s ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 12854 (apache2)
Tasks: 55 (limit: 4575)
Memory: 4.8M
CPU: 68ms
CGroup: /system.slice/apache2.service
...
Step 5 – How To Check the Service Logs in Systemd Unit Files?
At this step of Systemd Unit Files Full Guide with Examples, we want to talk about a tool journalctl that is used for checking the logs. If your service doesn’t start and you have no idea what to do, you can use the journalctl to check your logs.
First, you can check whether this tool is active or not with the command below:
sudo systemctl is-active systemd-journald
Output
active
Then, check your logs with the following command:
sudo journalctl -xe
Conclusion
At this point, you have learned that systemd unit files are used for managing your services and making everything easy. You can easily define a systemd unit file for a service. Also, you can use the systemctl commands to start, enable, stop, or disable a service on your Linux system. Hope you enjoy this guide on Systemd Unit Files Full Guide with Examples.
You may be interested in these articles too:
Install OpenSSL 3 on Ubuntu 20.04
Top 5 Open Source DNS Servers For Linux and Windows
Update phpMyAdmin to the Latest Version in Linux
Install and Uninstall OpenRGB from Ubuntu
Install OpenSSL on Windows Server 2022
Install Sendmail and Set up SMTP on Debian 12
FAQs
What are Systemd Unit Files?
They are configuration files used by systemd to manage services, devices, and system resources on Linux systems.
Where are systemd unit files stored?
Unit files are stored in /lib/systemd/system for system units and /etc/systemd/system for user-defined units.
How can you list available unit file types?
You can use systemctl -t help
to list the available unit types like service, timer, mount, etc.
How can you check the status of a service?
As explained in the Systemd Unit Files Full Guide with Examples, you can use: sudo systemctl status [service_name]