Where to place user created systemd unit files

The recommended place is /etc/systemd/system/nginx.service

Then issue the command :

systemctl enable nginx

And finally

systemctl start nginx

Usually, in Ubuntu, the package provided unit files go in /lib/systemd/system/ directory e.g. /lib/systemd/system/nginx.service whereas the user provided or any modification to the package provided unit file(s) go in /etc/systemd/system/ directory.

Now, to override, you can:

  • Name the unit file as the package provided one e.g. to override /lib/systemd/system/nginx.service, you can create the file /etc/systemd/system/nginx.service and put the directives in there

  • Or you can create a drop-in snippet ending in .conf in a directory that is named after the original unit file with .d appended to the directory name e.g. you can create a file named /etc/systemd/system/nginx.service.d/override.conf and put statements in there


Now, you can leverage the systemctl edit command to do all the manual stuffs for you i.e. it will create the necessary override file for you (at first it will create a temporary file (with intermediate directory(ies) for drop-in snippets), and upon saving it will commit and rename() the temp file to desired final file), and you just need to edit the file to put in your overrides.

For example, to create an override snippet for ngnx.service (/lib/systemd/system/nginx.service) mentioned above:

systemctl edit nginx.service

By default, it will use the file /etc/systemd/system/nginx.service.d/override.conf. If --full is used then the full replacement file /etc/systemd/system/nginx.service will be used (the original content from /lib/systemd/system/nginx.service will be copied to this file).

You can also use --runtime to create the snippet in /run/systemd/system/ (at first drop-in snippet, then whole so --runtime and --full are not mutually-exclusive) that will be temporary of course.

You can obviously choose the editor to use, the order of precedence is:

$SYSTEMD_EDITOR > $EDITOR > $VISUAL > editor > nano > vim > vi

Tags:

Systemd