Override systemd unit files
Luke Hebert
- 4 minutes read - 761 wordsNeed to tweak a single value in a systemd unit or timer file without risking your changes getting wiped out by the next package update? systemctl edit handles that cleanly, and this walkthrough shows you exactly how to create drop-in overrides, do full replacements, and revert when you change your mind.
Finding the right place
The systemd files that come with software packages usually get installed in a path like /usr/lib/systemd/system/ (Arch, Manjaro) or /lib/systemd/system/ (Debian, Ubuntu, Mint), not in /etc/systemd/system.
The first idea would be to edit these files directly in place and change the desired values there.
Editing package-managed unit files directly is a bad idea. You will most likely forget about the change, and when a new version of the containing package gets installed, the package manager will overwrite the file and revert your 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 is better, but there is an even cleaner 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 true for all 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.targetThe only change would be setting the OnCalendar option from weekly to daily:
$ sudo systemctl edit fstrim.timerThis 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=dailyIf 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 Mar 9 22:23 override.confNow, 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 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.servicePlease 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.serviceChanging 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 an environment variable called SYSTEMD_EDITOR that will be used for this instead of EDITOR or VISUAL. For example:
SYSTEMD_EDITOR=/usr/bin/vimBesides 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.