Unit File
Unit files are configuration files that contain directives for units. For example, when and how the unit will start, dependency on other units, which command will be triggered, etc. Unit files are usually saved under the /usr/lib/systemd/ directory or /etc/systemd/system directory. Edited unit files should be saved under the /etc/systemd/system directory while default unit files are kept under the /usr/lib/systemd/ directory.
Unit file structure
There are three sections in a unit file. The second section is a unit-type-specific section. For example, if the unit is a service unit, [Service]
is the section’s name. If it is used for a socket unit, [Socket]
is the section’s name. The unit-type-specific sections differ by type. Here are explanations for the unit file of a service unit.
[Unit]
: This section is used to describe general directives that are not unit-type specific. Directives in this section include dependencies, order of unit file executions, conflicting units, etc.[Service]
: This section includes directives for commands to be triggered when the unit is started, stopped, or reloaded. It also covers unit behaviors when the process is completed.[Install]
: This section is an optional section and is used to set the behavior of a unit when it is enabled.
This is an example of a unit file for sshd.service.
[Unit]
Description=OpenBSD Secure Shell server
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run
[Service]
EnvironmentFile=-/etc/default/ssh
ExecStartPre=/usr/sbin/sshd -t
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
RuntimeDirectory=sshd
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
Alias=sshd.service
Example of Unit X's unit file
Explaining unit file syntax is complicated as there are many interdependent units that are related to the unit defined by the unit file. For a clearer explanation, we will refer to the unit defined by the unit file as 'Unit X'.
[Unit] section
This section is used to describe common directives including:
Description
: a description of this unit.Documentation
: URI (Uniform Resource Identifier) of this unit documentAfter
: units' start order information. Unit X starts only after the units listed here are active
Before
: units' start order information. Unit X should be active before the units listed here start.
Requires
: units' dependency information. Unit X is dependent on the units listed here. If one of the units listed here fails to be activated, Unit X won't be activated
Wants
: units' dependency information. Unit X is less strictly dependent on the units listed here. Unit X can be activated even though the units listed here are not activated.
Conflicts
: the units listed here cannot be started when Unit X is running.
[Service] section
This section covers service unit-specific directives. If the unit is a socket unit or a timer unit, directive contents will be different from the directive contents described below. This section includes:
Type
: the unit process startup typeEnvironmentFile
: files listed here are used to support loading environment variablesExecStart
: commands listed here will be executed when Unit X is startedExecStop
: commands listed here will be executed when Unit X is stoppedExecReload
: commands listed here will be executed when Unit X is reloadedRestart
: set restart options – always, on-success, on-failure, etc.
[Install] section
This section is an optional section and is used to set the behavior of a unit when it is enabled. This section includes :
RequiredBy
: when Unit X is enabled, this unit's symbolic link is created under the .requires directory of the units listed here, meaning that when the units listed here try to start, Unit X will be called.
WantedBy
: when Unit X is enabled, this unit's symbolic link is created under the .wants directory of the units listed here, meaning that when the units listed here try to start, Unit X will be called.