Override systemd unit files

Sometimes you may want to change one or a few values in a existing systemd unit or timer file, that has been installed by the package manager of your Linux distribution. Here is how.

Finding the right place

The systemd files that come with the software packages, usually get installed in a path like /usr/lib/systemd/system/ (Arch, Manjaro) or /lib/systemd/system/ (Debian, Ubuntu, Mint) and not in /etc/systemd/system.

Now the first idea would be to edit these files directly in place and change the desired values there. But this is actually a bad idea, since you will, most likely, forget about the change and if a new version of the containing package gets installed, the package manager will overwrite the file and thereby revert the change.

The second idea may be, to copy the file into /etc/systemd/system/ and change the value there, since this folder takes precedence over the paths mentioned above.
This idea is better, but still, there is an even better solution:

“systemctl edit” to the rescue

The recommended way is to use the systemctl edit command. This will create an additional folder in /etc/systemd/system and place a file named override.conf in it. This file will then contain only the changes you made.

Note, that in the following example, I will be using a timer file, but the same is also true for all the unit files, with filenames ending in .service. Like, for example, smbd.service, sshd.service or docker.service.

Here is an example: Let’s say you want to change the time of trimming the filesystems on SSD drives from weekly to daily. This is what the stock timer file looks like:

$ cat /lib/systemd/system/fstrim.timer
[Unit]
Description=Discard unused blocks once a week
Documentation=man:fstrim
ConditionVirtualization=!container

[Timer]
OnCalendar=weekly
AccuracySec=1h
Persistent=true

[Install]
WantedBy=timers.target

The only change would be, setting the OnCalendar option from weekly to daily:

$ sudo systemctl edit fstrim.timer

This will open an editor and allows you to type in the line(s), that need to be changed. It’s important to also write down the section header, in this case [Timer] and clear the existing value of the option first, because OnCalendar can actually appear multiple times in the same timer file:

[Timer]
OnCalendar=
OnCalendar=daily

If the value wouldn’t be cleared first, the timer would fire daily and weekly! Save and close the editor. systemd will then create a folder with the same name as the unit file or timer file.

If the option may only appear once, it will be replaced and it is not needed to be cleared first.

$ ls -l /etc/systemd/system/fstrim.timer.d
total 4
-rw-r--r-- 1 root root 25 Mär 9 22:23 override.conf

Now, when the file gets read by systemd in the future, the changes in override.conf will be applied to the stock configuration file, even if the latter gets replaced in the future by the package it belongs to.

If you want to edit the changes just made, at a later point in time, just run (in this case) systemctl edit fstrim.timer again! You could also edit the override.conf directly, but this way, systemd wouldn’t automatically reload the configuration files and apply the changes.

Also every change is easily visible by the *.d folders created.

Replacing the whole thing

If you actually want to completely replace the existing configuration unit or timer file, you can do this:

$ sudo systemctl edit --full foobar.service

Pease note the --full option! This will copy the original file to /etc/systemd/system and open it in the editor to change it. After that, this file will be loaded instead of the stock file.

Revert to the original state

To undo the changes, you could remove the created files/folders and reload systemd (systemctl daemon-reload), but there is also a command for that, which takes care of all that for you:

$ sudo systemctl revert foobar.service
Removed /etc/systemd/system/smbd.service.

Changing the editor used by the edit command

You may or may not find the editor used by systemd is actually not the editor you want it to be. If it is, simply ignore the following. There is a environment variable called SYSTEMD_EDITOR that will be used for this instead of EDITOR or VISUAL. For example:

SYSTEMD_EDITOR=/usr/bin/vim

Besides from defining the above variable in your local .bashrc or .zshenv, you need to edit the sudoers file with the sudo visudo command, then add the following line to this file:

Defaults env_keep += "SYSTEMD_EDITOR"

Save and close the editor. Now the variable will be preserved, when changing to the root user context, when using the sudo command.

Leave a Reply

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