Many sysv init scripts used a corresponding file in /etc/default
to allow the administrator to configure it. Upstart jobs can be modified using .override
files. How do I override or configure systemd units, now that systemd is the default in Ubuntu?
systemd
units need not obey files in/etc/default
.systemd
is easily configurable, but requires that you know the syntax of systemd unit files.Packages ship unit files typically in
/lib/systemd/system/
. These are not to be edited. Instead,systemd
allows you to override these files by creating appropriate files in/etc/systemd/system/
.For a given service
foo
, the package would provide/lib/systemd/system/foo.service
. You can check its status usingsystemctl status foo
, or view its logs usingjournalctl -u foo
. To override something in the definition offoo
, do:This creates a directory in
/etc/systemd/system
named after the unit, and anoverride.conf
file in that directory (/etc/systemd/system/foo.service.d/override.conf
). You can add or override settings using this file (or other.conf
files in/etc/systemd/system/foo.service.d/
). This is also applicable to non-service units - you could dosystemctl edit foo.mount
,systemctl edit foo.timer
, etc.Overriding command arguments
Take the
getty
service for example. Say I want to have TTY2 autologin to my user (this is not advisable, but just an example). TTY2 is run by thegetty@tty2
service (tty2
being an instance of the template/lib/systemd/system/getty@service
). To do this, I have to modify thegetty@tty2
service.In particular, I have to change the
ExecStart
line, which currently is:To override this, do:
And add:
Note that:
ExecStart
before setting it again, as it is an additive setting, similar to other lists likeEnvironment
(as a whole, not per-variable) andEnvironmentFile
; and opposed to overriding settings likeRestartSec
orType
.ExecStart
can have multiple entries only forType=oneshot
services. Note that dependency settings likeBefore
,After
,Wants
, etc. are also lists, but cannot be cleared using this way. You'll have to override/replace the entire service for that (see below).ExecStart
is in the[Service]
section, so my override has to putExecStart
in the[Service]
section as well. Often, having a look at the actual service file usingsystemctl cat
will tell you what you need to override and which section it is in.Usually, if you edit a systemd unit file, for it to take effect, you need to run:
However,
systemctl edit
automatically does this for you.Now:
And if I do:
and press CtrlAltF2, presto! I'll be logged into my account on that TTY.
As I said before,
getty@tty2
is an instance of a template. So, what if I wanted to override all instances of that template? That can be done by editing the template itself (removing the instance identifier - in this casetty2
):Overriding the environment
A common use case of
/etc/default
files is setting environment variables. Usually,/etc/default
is a shell script, so you could use shell language constructs in it. Withsystemd
, however, this is not the case. You can specify environment variables in two ways:Via a file
Say you have set the environment variables in a file:
Then, you can add to the override:
In particular, if your
/etc/default/grub
contains only assignments and no shell syntax, you could use it as theEnvironmentFile
.Via
Environment
entriesThe above could also be accomplished using the following override:
However, this can get tricky with multiple variables, spaces, etc. Have a look at one of my other answers for an example of such an instance.
Variations in editing
Replacing the existing unit entirely
If you want to make massive changes to the existing unit, such that you're effectively replacing it altogether, you could just do:
Temporary edits
In the systemd file hierarchy,
/run
takes precedence over/etc
, which in turn takes precedence over/lib
. Everything said so far also applies to using/run/systemd/system
instead of/etc/systemd/system
. Typically/run
is a transient filesystem whose contents are lost on reboot, so if you want to override a unit only until reboot, you can do:Undoing changes
You can simply remove the corresponding override file, and do
systemctl daemon-reload
to have systemd read the updated unit definition.You can also revert all changes:
Further Reading
Via this mechanism, it becomes very easy to override
systemd
units, as well as to undo such changes (by simply removing the override file). These are not the only settings which can be modified.The following links would be useful:
systemd
systemd
manpages, in particular, the manpages ofsystemd.unit
andsystemd.service