Defining systemd units
By convention, packages normally ship unit files for systemd to /lib/systemd/system/
, whereas user-defined services are typically placed in /etc/systemd/system/
(when a service is present in both locations, the one in /etc
takes precedence).
Configuration is done with ini-style key/value pairs. The options accepted differ for every unit type, so refer to the manpages linked from systemd units for more detail on this.
Warning
After modifying any of these unit files, systemctl daemon-reload
must be issued to let systemd load the new values.
These values won’t be applied to any running processes unless the relevant unit is then reloaded, for example via systemctl restart foo
.
Override files
systemd allows unit configuration to be extended and overwritten through “drop-in” files located at /etc/systemd/system/<unit>.d/*.conf
.
For example, to override settings for the service foo
, create the file /etc/systemd/system/foo.service.d/override.conf
.
Within this file, repeat the section headers and any keys you wish to set:
[Service]
ExecStart=
ExecStart=/usr/bin/foo --my-custom-flag
Note that some settings are additive (like ExecStart
above and also After
, Environment
and EnvironmentFile
), while some settings are overwritten entirely.
For additive settings like ExecStart
, you clear the previous value by including a blank entry, as in the example above.
Remember that after creating any of these override files, systemctl daemon-reload
must be issued.
When making changes interactively, systemctl edit <unit>
automates all of this.
Enabling units to start on boot
If you create a new unit file and activate it, it won’t be enabled by default. Meaning, it won’t be started automatically next time the system boots.
To make this happen, two things are needed:
- An
[Install]
section in the unit file. systemctl enable <unit>
must be run.
In the install section, the following is typically what you’ll want for a standalone service which doesn’t depend on anything else:
[Install]
WantedBy=multi-user.target
If you require networking interfaces to be created and have IP addresses assigned before the service can start successfully, also include the following:
[Unit]
After=network-online.target
Wants=network-online.target
For more information, see: