How to Manage Services with systemd on Ubuntu Server

How to Manage Services with systemd on Ubuntu Server

systemd is the default init system and service manager on Ubuntu Server (since 15.04, fully entrenched by 24.04 LTS and later). It handles starting, stopping, monitoring, restarting, and supervising daemons, as well as managing dependencies, boot ordering, logging (via journald), resource limits, and socket activation.

The primary command-line tool is systemctl, which controls units (services are one type of unit). All operations usually require sudo because systemd manages system-wide resources.

1. Core Service Management Commands

These commands work on service units (ending in .service, though the suffix is often optional in commands).

  • Check status — Most important diagnostic command Shows: active/inactive/failed state, PID, recent journal lines, resource usage, process tree, exit codes. sudo systemctl status nginx Use -l for full output, -n 50 to limit log lines.
  • Start / Stop / Restart / Reload
    • sudo systemctl start nginx — activates the service
    • sudo systemctl stop nginx — gracefully stops it
    • sudo systemctl restart nginx — stop then start (disrupts connections)
    • sudo systemctl reload nginx — reloads config without dropping connections (if the service supports SIGHUP or similar)
  • Enable / Disable at Boot
    • sudo systemctl enable nginx — creates symlinks so the service starts automatically on boot (WantedBy=multi-user.target by default)
    • sudo systemctl disable nginx — removes the symlinks (does not stop a running instance)
  • Mask / Unmask — Ultimate protection
    • sudo systemctl mask nginx — prevents the service from ever starting (even manually), useful for disabling dangerous or conflicting units
    • sudo systemctl unmask nginx — reverses masking
  • Reload systemd daemon — Required after manual unit file edits sudo systemctl daemon-reload — re-reads all unit files without restarting running services

2. Listing and Discovering Services

  • Active (running) services: systemctl list-units –type=service –state=active
  • All loaded units (running or not): systemctl list-units –type=service
  • All available unit files (installed but possibly not loaded): systemctl list-unit-files –type=service
  • Enabled vs. disabled: systemctl list-unit-files –type=service | grep enabled
  • Failed units: systemctl –failed

3. Understanding Unit File Structure

Service behavior is defined in unit files (INI-style format). Locations (override priority highest to lowest):

  • /etc/systemd/system/ — custom or overridden units (highest priority)
  • /etc/systemd/system/*.service.d/*.conf — drop-in overrides (recommended for modifications)
  • /lib/systemd/system/ or /usr/lib/systemd/system/ — distribution-provided defaults

Typical structure of a .service file:

  • [Unit] — metadata & dependencies Common keys: Description=, After=, Before=, Requires=, Wants=, Conflicts=, PartOf=
  • [Service] — how the process runs Key directives:
    • Type= (simple, forking, oneshot, notify, dbus)
    • ExecStart= — main command to execute
    • ExecStop=, ExecReload=
    • Restart= (no, on-success, on-failure, on-abnormal, on-watchdog, always, on-abort)
    • RestartSec= — delay before restart
    • User=, Group= — run as non-root
    • WorkingDirectory=, Environment=, EnvironmentFile=
    • LimitNOFILE=, PrivateTmp=yes, ProtectSystem=strict — security hardening
  • [Install] — boot integration WantedBy=multi-user.target (normal multi-user mode) RequiredBy= (stronger dependency)

4. Best Practices for Safe & Reliable Management (2026 Context)

  • Never edit /lib/systemd/system/ files directly — upgrades overwrite them. Use sudo systemctl edit nginx → creates /etc/systemd/system/nginx.service.d/override.conf Or sudo systemctl edit –full nginx for complete replacement.
  • Prefer drop-in overrides for small changes (e.g., add Restart=always, change User=).
  • After any edit:
    1. sudo systemctl daemon-reload
    2. sudo systemctl restart nginx (or reload if supported)
    3. sudo systemctl status nginx — verify
  • Use Restart= wisely:
    • on-failure or on-abnormal for most production services
    • always for critical always-on daemons (with RestartSec= to avoid restart loops)
  • Leverage journalctl for logs: journalctl -u nginx -e (recent logs) journalctl -u nginx –since “2026-02-01”-f for live tail
  • Analyze boot impact: systemd-analyze blame — slowest units systemd-analyze critical-chain — dependency chain to multi-user.target
  • For custom/background scripts: use Type=oneshot + RemainAfterExit=yes or Type=notify with systemd-notify for readiness signaling.
  • Disable unnecessary services to reduce attack surface and boot time: sudo systemctl disable bluetooth (if unused)

systemd provides declarative, dependency-aware, and journal-integrated service management — far more powerful than SysV init or Upstart. Master systemctl status, edit, daemon-reload, and journalctl -u — they solve 90% of day-to-day issues.

If you’re dealing with a specific service (nginx, docker, postgresql, custom script) or symptom (failed to start, restart loop, slow boot), share details for more precise guidance.

Fast • Reliable • Affordable VPS - DO It Now!

Get top VPS hosting with VPS.DO’s fast, low-cost plans. Try risk-free with our 7-day no-questions-asked refund and start today!