5

Given the CLI tool systemctl edit can be used to edit existing systemd units such as services, timers, sockets, devices, mounts, automounts, targets, swap, path, slice, scope or nspawn files and you have another subcommand that can delete/reset systemd units can I also create a new systemctl file like this?

Most guides online tell you to use some custom text editor and place the file somewhere (where you first need to find the correct systemd directory, yet again…). Also, you need to reload the daemon if you copy the files manually via systemctl daemon-reload.

As a sysadmin I may however just quickly create a new unit in the default location, just as systemctl edit would do for editing/overriding an existing entry. I just like how it pops me directly into my favorite CLI text editor (nano or so) and I can edit my content right away.

I tried systemctl add and systemctl create, but no one of these two commands exists.

I did not find that information on the net nor any Stackexchange answer here…

rugk
  • 3,086
  • Note this question is not about how to create such a unit/file fully automatically, but rather manually. For the automatic way see this question: Create a systemd unit file from command line. – rugk May 19 '21 at 21:46
  • “I did not find that information on the net” seems a bit strange given that your own answer includes a link to the information on the net... – Stephen Kitt May 20 '21 at 04:40
  • Maybe I should have said “on Stackexchange”. The man page shows it yes, but that is hidden and not as easily discoverage as this question and answer and all websites always seem to describe the mentioned other way. That’s why I wanted this jeopardy-answer here. :) – rugk May 23 '21 at 16:38

1 Answers1

6

Actually, if you try to run the systemctl edit command with a new, not-yet-existing service, it will tell you exactly what to do:

$ systemctl edit happy-unicorns.service
No files found for happy-unicorns.service.
Run ‘systemctl edit --force --full happy-unicorns.service' to create a new unit.

As such to create a new system unit (in this case a service), indeed just run:

$ systemctl edit --force --full happy-unicorns.service

It will happily popup your text editor (which one can be specified e.g. by setting the environmental variable $EDITOR as a usual thing for Linux tools).

The meaning:

  • --full will edit the whole unit file instead of just create an override. This means, in our example it will actually use the full service file in a proper location /etc/systemd/system/.#happy-unicorns.service11738733f89dc655 instead of creating a directory and override for the service in e.g. /etc/systemd/system/happy-unicorns.service.d/.#override.conf98be493089631328.
    Note: You see systemctl appends some random numbers and marks the file as temporary in order to not apply the changes directly. They are still sanity-checked and the file is moved when everything is correct.
  • --force actually needs to be specified to create a new file instead of editing an existing one.
  • If you want to create the file at other places (i.e. user or global scope) by adding the usual switches: You can add --user for your current user --global for all users. --system for the current system is the default value.

I agree it may not be so obvious to find, given it’s not a separate subcommand, but it makes quite much sense when you think of just editing any file and letting systemctl decide, whether it’s a file that already exists or not.

See also the systemctl man page for more information.

Stephen Kitt
  • 434,908
rugk
  • 3,086