2

I recently noticed a server performing apt-get update && apt-get dist-upgrade

Amongst other things, docker was updated and restarted.

However, I noticed some changes I made to the service file previously were now gone. The file at /lib/systemd/system/docker.service seems to have been reset.

Is this possible? I don't recall if I ran daemon-reload after these changes.

EDIT: To be clear: I'm wondering is dist-upgrade basically removed the service file and replaced it with a default one?

And does daemon-reload prevent this in the future?

EDIT2: Ok, looks like sudo systemctl edit docker.service is the way to go and performs a daemon-reload on save.

KdgDev
  • 235
  • 1
  • 3
  • 14
  • 6
    Does this answer your question? [How do I override or configure systemd services?](https://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services) – muru Jun 25 '21 at 11:13
  • @muru No, my question is more "did dist-upgrade forcibly replace the entire service file with a new one, ignoring the existing one?" – KdgDev Jun 25 '21 at 12:39
  • 2
    @muru Ah, I read a bit further and it does detail not to edit /lib/systemd/ files. Yeah this is going to be it. – KdgDev Jun 25 '21 at 12:43

1 Answers1

9

Don't edit files in /lib/systemd/ or /usr/share/systemd as they will get overwritten on updates.

Instead, copy the file to /etc/systemd/ and make the changes there.

The /etc/ directory (at least for systemd) is considered the place to store local config files. All other /**/systemd/ directories are considered sources of default and sample config files that should be replaced on any upgrade.

Another reason to not edit these package supplied config files is that if you copy it to /etc/systemd/... and edit it and make a mistake, you can always compare with the original file.

systemctl daemon-reload doesn't prevent anything. It just tells systemd to re-examine all of its config and use whatever has changed.

user10489
  • 3,564
  • 2
  • 5
  • 22
  • think `/usr/local/lib/systemd/system/` is better than `/etc/systemd...` [1](https://unix.stackexchange.com/questions/224992/where-do-i-put-my-systemd-unit-file) –  Jun 25 '21 at 13:11
  • @bac0n the systemd-sanctioned location is `/etc/systemd`. That’s what `systemctl edit` uses. – Stephen Kitt Jun 25 '21 at 20:51
  • I wouldn't edit the one in /usr/local/lib/systemd either, I'd copy that to /etc/systemd as well before editing. – user10489 Jun 25 '21 at 22:20
  • @StephenKitt, what you [suggest](https://steveazz.xyz/micro/systemd-drop-in/) is something completely different and deserves its own answer and even be the preferred one, still, I think, copying files to `/etc/systemd/` is not recommended, e.g., you lose the ability to *mask* units. –  Jun 26 '21 at 06:20
  • @bac0n: Actually, you're wrong. You mask units *by* putting a blank file in /etc/systemd/ basically by replacing the system default with a blank file. If you are putting a real (non-empty) file there anyway, you can disable the unit instead of masking it. – user10489 Jun 26 '21 at 06:25
  • but where do you put the edited file? –  Jun 26 '21 at 06:27
  • The edited file goes in /etc/systemd/system. This masks the default with a replacement file. If you want to disable the unit temporarily, you can just disable it, no need to mask. – user10489 Jun 26 '21 at 06:28
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/126878/discussion-between-user10489-and-bac0n). – user10489 Jun 26 '21 at 06:28
  • 3
    @bac0n it’s already covered in [muru’s answer here](https://askubuntu.com/a/659268/412264), no point in duplicating it IMO ;-). – Stephen Kitt Jun 26 '21 at 07:38