Systemd Unit Files Full Guide with Examples

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 to get information in detail about Systemd Unit Files.

Systemd Unit Files Full Guide with Examples

To complete this guide, 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. Here we provide a list of what they exactly do.

Unit TypesDefinition
service Service managing processes like start, restart, and stop.
mountControl the file-system mount point.
swapencapsulate, activate, and deactivate the swap partition.
socketActivate a service when the service receives incoming traffic on a listening socket.
targetControl synchronization of unit files during the boot process.
deviceImplement device-based activation such as a device driver.
automountProvide and Control on-demand mounting of file systems.
timerSchedule the activation of other units.
pathMonitor files/directories and activate/deactivate a service if the specified file or directory is accessed.
sliceManage system resources such as CPU, and memory.
scopeorganize and manage foreign processes.

Step 2 – Structure of Unit Files in Systemd

At this point, 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 own 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 at 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 point, 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 point, 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

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

And 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 point, 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:

Top 5 Open Source DNS Servers For Linux and Windows

Update phpMyAdmin to the Latest Version in Linux

Install and Uninstall OpenRGB from Ubuntu

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay informed and not overwhelmed, subscribe now!