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.s
Subscribe now for
uninterrupted access.