Garden of KnowledgeApplied Sciences › Computer Science › Software › Security › Certifications › LFCS

q22-systemd-targets-and-services

Énoncé§

Solve this question on: web-srv1

  1. Set the default boot target to multi-user.target.
  2. Enable and start the nginx service so it survives a reboot.
  3. Mask the bluetooth.service so it can never be started.
  4. Write the list of currently failed units into /opt/course/22/failed.

Solution§

Step 1 — Change the default target§

systemctl get-default                       # current default
sudo systemctl set-default multi-user.target

Common targets (equivalent to old runlevels):

TargetEquivalentPurpose
poweroff.targetrunlevel 0shutdown
rescue.targetrunlevel 1single-user / maintenance
multi-user.targetrunlevel 3multi-user, no GUI
graphical.targetrunlevel 5multi-user + GUI
reboot.targetrunlevel 6reboot
emergency.targetminimal shell, no services

Switch on the fly (no reboot):

sudo systemctl isolate rescue.target

Step 2 — Enable and start a service§

sudo systemctl enable --now nginx
systemctl status nginx
systemctl is-enabled nginx
systemctl is-active nginx

Useful variants:

Step 3 — Mask a service§

A masked service is symlinked to /dev/null — it cannot be started, even as a dependency:

sudo systemctl mask bluetooth.service
sudo systemctl unmask bluetooth.service     # to undo

Step 4 — List failed units§

systemctl --failed --no-legend | awk '{print $2}' > /opt/course/22/failed

Useful day-to-day commands§

systemctl list-units --type=service                  # active services
systemctl list-unit-files --type=service             # all installed
systemctl list-dependencies graphical.target
systemctl cat sshd.service                           # show effective unit file
sudo systemctl edit sshd.service                     # create drop-in override in /etc/systemd/system/sshd.service.d/
sudo systemctl edit --full sshd.service              # full override

Drop-ins live in /etc/systemd/system/<unit>.d/override.conf and take precedence over /lib/systemd/system/<unit> — never edit vendor files directly.

—The Gardener