Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Packages and Services

Managing Services with systemd and systemctl

Control Linux services with systemctl: start, stop, enable, mask, check status, read logs with journalctl, understand targets and find failed units after boot.

A service is a long-running program, such as a web server, a database or the SSH daemon. On nearly every mainstream distribution, systemd is the first process (PID 1). It starts the rest of the system, restarts services that fail and records their output. You talk to it with systemctl and journalctl.

Units

systemd manages units, described by files. The type is the file suffix.

Unit type Meaning
.service A program or daemon
.socket A listening socket that can start a service on demand
.timer A schedule that activates another unit
.mount A filesystem mount point
.target A group of units, like a run level
.path Activates a unit when a file changes

Everyday systemctl commands

Terminal
systemctl status nginx          # state, recent log lines, PID
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx    # stop then start
sudo systemctl reload nginx     # re-read config without dropping connections (if supported)
sudo systemctl enable nginx     # start at boot
sudo systemctl disable nginx
sudo systemctl enable --now nginx   # enable and start together
systemctl is-active nginx
systemctl is-enabled nginx

Active and enabled are separate. Active means running now; enabled means it starts at boot. A common surprise is a service you started but forgot to enable, which vanishes after a reboot.

Names omit .service by default. On Debian the SSH unit is ssh; on Red Hat it is sshd.

Finding services

Terminal
systemctl list-units --type=service              # loaded units
systemctl list-unit-files --type=service         # installed, with enabled state
systemctl --failed                               # what is broken
systemctl list-dependencies nginx
systemctl cat nginx                              # show the unit file

Masking

disable stops auto-start but anything can still start it manually or as a dependency. mask links the unit to /dev/null so it cannot start at all.

Terminal
sudo systemctl mask bluetooth
sudo systemctl unmask bluetooth

Logs with journalctl

Terminal
journalctl -u nginx                 # one unit
journalctl -u nginx -f              # follow
journalctl -u nginx --since "1 hour ago"
journalctl -b                       # this boot
journalctl -b -1                    # previous boot
journalctl -p err -b                # errors and worse since boot
journalctl --disk-usage

On most systems the journal is stored in memory only unless the directory /var/log/journal exists (or Storage=persistent is set in /etc/systemd/journald.conf). Make it persistent if you want logs from before a reboot (Monitoring and logs).

Targets

Targets replace the old run levels.

Terminal
systemctl get-default                 # usually multi-user.target on servers
sudo systemctl set-default multi-user.target
sudo systemctl isolate rescue.target  # go to single-user mode now
systemctl list-units --type=target

multi-user.target is a text-mode system with networking, graphical.target adds a desktop and rescue.target is a minimal repair mode.

Understanding states and failures

State Meaning
active (running) Started and still running
active (exited) A one-shot job finished successfully
inactive (dead) Not running
failed Exited with error, was killed or timed out
activating Starting

When a service is failed:

Terminal
systemctl status myapp -l
journalctl -u myapp -n 50 --no-pager
sudo systemctl reset-failed myapp

Read the last log lines first; the cause is usually printed right before the exit.

flowchart LR
    A["Boot"] --> B["systemd PID 1"]
    B --> C["default.target"]
    C --> D["Units start in dependency order"]
    D --> E["Failed? Restart= policy"]

systemd starts units in parallel and respects their dependencies.

A stand-in: parse systemctl status

This model reads made-up systemctl is-active and is-enabled results and explains each combination.

Python Stand-in: active versus enabled

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: summarize a journal

This model counts priorities in made-up journal lines, as you might when scanning journalctl -b.

Python Stand-in: count log levels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Starting a service but not enabling it.
  • Editing a file under /usr/lib/systemd/system (use drop-ins, next lesson).
  • Using restart when reload would keep connections alive.
  • Not reading journalctl -u before guessing.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does systemctl enable do?

    enable configures start at boot; use --now to also start it.
  2. 2. Which command lists all failed units?

    systemctl --failed lists units in a failed state.
  3. 3. What is the difference between disable and mask?

    Masked units link to /dev/null and cannot be started.
  4. 4. How do you follow a service's logs live?

    -f follows new entries like tail -f.

Summary

  • systemd manages units; systemctl controls them and journalctl reads logs.
  • Active and enabled are separate states; use enable --now.
  • mask blocks a unit completely; --failed finds broken ones.
  • Targets define the boot state, multi-user.target for servers.

Next: Writing Unit Files, Drop-ins and Timers