systemd Units & Services

Unit files, service types, dependency ordering, socket activation, and service hardening directives.

Overview

systemd is the init system and service manager on virtually all modern Linux distributions. It parallelizes service startup by tracking inter-service dependencies, and it describes everything it manages as units, small ini-style files. This chapter covers where unit files live, how systemd knows a service has started, how dependencies work, and how to lock a service down.

Unit File Locations

systemd reads unit files from three places, and which one you edit matters.

  • /usr/lib/systemd/system/ holds the units that distribution packages install. Do not edit them.
  • /etc/systemd/system/ holds the administrator's units and overrides, and is the preferred place for your changes.
  • ~/.config/systemd/user/ holds user units.

Key concepts

  • Service type=notify
  • After= vs Requires=
  • WantedBy=multi-user.target
  • Restart=on-failure
  • Socket activation

Interview questions

  • Write a systemd service unit for a Node.js API server that should restart on failure and start after the network is up.
  • Your systemd service is stuck in "activating" state. How do you diagnose it?

Sign up free to read the full topic

More Linux topics

Start free on Cariara · Coding problems · System design studio