I need to enable systemd services before I'm actually booting the system (for unattended install purposes). Therefore I can't use systemctl. I could use systemd-nspawn, but I think it not that well suited for scripting.

I know that systemctl creates a symlink, but is that all I have to do and can I do it without 'disturbing' systemd?

This seems to work, but can you confirm it?

ln -s /usr/lib/systemd/system/sshd.service /etc/systemd/system/multi-user.target.wants/sshd.service
  • 2
    systemd will read the [Install] Section of the unit file. So ln -s will not always work and have risk of corrupted the system consistency – 炸鱼薯条德里克 Oct 4 '18 at 23:19
  • 2
    But you can use systemd-nspawn to boot the installed system as a container and use systemctl --machine – 炸鱼薯条德里克 Oct 4 '18 at 23:22
  • Good to know that it can cause issues. I'll update my answer with a warning. So --machine executes the command in the container thats a very good solution. – rudib Oct 4 '18 at 23:41

How to use systemd-nspawn to enable a service or perform changes on systemd:

tmux new-session -d -s mysession "systemd-nspawn --boot --machine=machine_name -D /install/directory"
systemctl --machine=machine_name enable sshd
machinectl poweroff machine_name

Warning: Just using a symlink may cause issues as systemd processes the [Install] section of the unit file and the symlink may cause corruption. Improvements thanks to @神秘德里克.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.