Understanding Systemd Unit Files

If you’ve worked with Linux for very long, you likely have used Systemd when managing your server. If you’ve worked with Linux even longer, you’ll likely remember familiarizing yourself with Systemd basics when the change was made from SysVinit. If you’re just starting out and need an introduction to Systemd, I’d recommend you start with the community article here.

In this documentation, we’ll be taking a closer look at the Systemd unit files. A ‘unit’ refers to a resource which the system knows how to operate and manage. These resources are further defined using configuration files known as unit files.

Unit files can be found in three main directories which may differ slightly depending on your OS. These directories also determine the order in which Unit files are acted upon. As soon as Systemd finds a specific unit file for a unit, it no longer searches for it. So, if you wished to supercede a unit file in /run/systemd/system, you could place the file in /etc/systemd/system:

/etc/systemd/system – Local configuration

/run/systemd/system – Runtime units

/usr/lib/systemd/system – Units of installed packages

Within the directories, unit file names take on the following form:


unit_name identifies the name of the unit, and type_extension identifies the type of unit. For example we might have sshd.service. There are a number of different unit types, the table below gives an overview of these:

Unit Type File Extension Description
Service unit .service A system service.
Target unit .target A group of systemd units.
Automount unit .automount A file system automount point.
Device unit .device A device file recognized by the kernel.
Mount unit .mount A file system mount point.
Path unit .path A file or directory in a file system.
Scope unit .scope An externally created process.
Slice unit .slice A group of hierarchically organized units that manage system processes.
Snapshot unit .snapshot A saved state of the systemd manager.
Socket unit .socket An inter-process communication socket.
Swap unit .swap A swap device or a swap file.
Timer unit .timer A systemd timer.

Unit File Structure

A unit file will usually consist of three sections:

  • [Unit] — This is the informational section. It will contains generic options that are not dependent on the type of the unit. Here you’re likely to find a description of the service, as well as what other services it depends on before it can start.

  • [Service] — This section contains the commands and environment variables required to run the service. It may also contain a command which gracefully shuts down the service.

  • [Install] — This section will tell the service which target requires this service. This section can also be used to define aliases for the service.

Let’s take a look at the sshd.service file as an example. Commentary describing each line is provided in italic:

# cat sshd.service
Description=OpenSSH server daemon #Describes what this service is
Documentation=man:sshd(8) man:sshd_config(5) #Provides URIs for unit's documentation
After=network.target sshd-keygen.service #Indicates this unit starts only after these services
Wants=sshd-keygen.service #Indicates what is known as a weak dependency, it will not prevent our unit from starting, but identifies recommended dependencies.

Type=notify #This specifies the unit process start type which affects functionality of the ExecStart options. 
EnvironmentFile=/etc/sysconfig/sshd #EnvironmentFile dictates an exact path to a file where Systemd reads the environment variables from. It's also possible to replace this with Environment= and include the variables directly in the unit file
ExecStart=/usr/sbin/sshd -D $OPTIONS #This specifies the script or command executed when the unit is started. 
ExecReload=/bin/kill -HUP $MAINPID #This specifies the script or command to execute when the unit is reloaded.
KillMode=process #Specifies how processes of this unit shall be killed. One of control-group, process, mixed, none.
Restart=on-failure #With this, systemd will automatically restart the unit if it exits without a directive from systemctl.
RestartSec=42s #With Restart enabled, this dictates the time between when the unit has stopped, and when it will attempt to restart.

WantedBy=multi-user.target #With WantedBy=multi-user.target enabled, a directory called multi-user.target.wants will be created within /etc/systemd/system and a symbolic link to the current unit will be placed within, creating a dependency.


Understanding units and unit files will help in administration of the server. Thanks to the UnitFiles, you do not need to understand script or code to determine much of what a unit/services requires in order to operate. Further understanding of unit files will also allow a user to create their own unit files for units, giving them further flexibility.

For extensive information around Units and Unit Files in Systemd, you can run the following command to open up the man page on units and unit configuration:

# man systemd.unit

Leave a Reply

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