Skip to content
Visual explainer 3h to learn 5 steps

Managing services with systemctl

Start, stop, enable and inspect services and understand unit dependencies.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 10 minutes to read.

Topic 14 of 42

In short

systemd is PID 1: it starts services, mounts, sockets and timers, all described as units. systemctl starts and stops units now, enables or disables them at boot, and shows their status with recent log lines. Masking a unit stops anything from starting it.

Explained simply

systemd is the first program the kernel starts, with PID 1. Its job is to bring the system up and keep it running: it starts services, mounts disks, sets up the network, runs scheduled jobs and restarts things that crash.

Everything systemd manages is a unit, described by a small text file. A web server is a service unit, a scheduled job is a timer unit, a mounted disk is a mount unit, and a group of units that together make a system state, like normal multi-user operation, is a target unit.

You control units with systemctl. Two ideas matter most and are easy to mix up. start and stop act right now. enable and disable decide what happens at the next boot. A service can be running but not enabled, so it vanishes after a reboot, or enabled but not running yet.

systemctl status tells you most of what you need: whether the unit is loaded, enabled, running, its main PID, its memory use and the last few log lines. When something does not work, that command is always the first step.

Press Play on a diagram to walk through it one step at a time.

CycleService unit states
  1. 1
    inactive

    Not running.

  2. 2
    activating

    ExecStart is running; waiting for readiness.

  3. 3
    active (running)

    Main process is up.

  4. 4
    deactivating

    Stop requested; SIGTERM sent.

  5. 5
    inactive or failed

    Failed if it exited with an error.

  6. Back to step 1
CompareNow versus at boot
Acts now
  • start, stop, restart
  • reload
  • isolate a target
  • kill
Acts at boot
  • enable, disable
  • set-default
  • Only changes symlinks
  • Add --now to also act now
Both, and stronger
  • mask --now
  • Stops it and blocks all starts
  • unmask to undo
  • Use for conflicting services

In depth

Unit types and their states

The suffix of a unit name tells you its type: .service for programs, .socket for a listening socket that starts a service on first connection, .timer for schedules, .target for groups of units, .mount and .automount for file systems, .swap, .path for watching files, and .slice and .scope for grouping processes for resource control.

Two kinds of state exist. The runtime state, shown by systemctl list-units, says whether a unit is active, inactive, failed, activating or deactivating. The install state, shown by systemctl list-unit-files, says what happens at boot: enabled, disabled, static (no [Install] section, so it only starts when another unit needs it), masked, or generated (created at boot, for example from /etc/fstab).

MapCommon unit types
systemd units
  • .service
    • sshd.service
    • httpd.service
  • .socket
    • Starts a service on first connection
  • .timer
    • Runs a service on a schedule
  • .target
    • multi-user.target
    • graphical.target
  • .mount and .swap
    • Generated from /etc/fstab
  • .slice
    • user.slice, system.slice for resource limits

What enable and mask really do

enable is just symlinks. systemctl enable httpd reads the [Install] section of httpd.service, finds WantedBy=multi-user.target and creates /etc/systemd/system/multi-user.target.wants/httpd.service pointing at the unit file. At boot, when multi-user.target starts, it pulls in everything in its .wants directory. disable removes those links. Neither one starts or stops anything unless you add --now.

mask creates /etc/systemd/system/httpd.service as a link to /dev/null. Because /etc wins over the vendor directory, systemd sees an empty unit and refuses every start, manual or automatic. unmask removes the link.

Distributions differ on installs: on RHEL, a newly installed service is usually left disabled and stopped until you enable it, while on Ubuntu, installing a package such as nginx normally enables and starts it straight away.

Flowsystemctl enable --now httpd
  1. 1
    Read [Install]

    WantedBy=multi-user.target

  2. 2
    Create a symlink

    In multi-user.target.wants/

  3. 3
    Reload manager

    systemd notices the new link

  4. 4
    --now: start it

    Same as systemctl start httpd

  5. 5
    Next boot

    multi-user.target pulls it in

Requirements versus ordering

systemd keeps two separate kinds of relationship. Requirement settings say what must be started together. Wants= is weak: start the other unit too, but carry on if it fails. Requires= is strong: if the required unit fails to start or is stopped later, this unit is stopped too. BindsTo= is even stricter, and PartOf= passes stop and restart from one unit to another.

Ordering settings, After= and Before=, only say which starts first. They do not cause anything to start. A unit with Wants=db.service but no After=db.service starts in parallel with the database, which is a common cause of services that fail only at boot.

systemctl list-dependencies httpd shows what a unit pulls in, and --reverse shows who pulls it in. systemd-analyze blame and systemd-analyze critical-chain show what made the last boot slow.

CompareTwo kinds of dependency
Requirement: Wants=, Requires=
  • Decides what also gets started
  • Wants= tolerates failure
  • Requires= stops this unit if the other fails
  • Says nothing about order
Ordering: After=, Before=
  • Decides who starts first
  • Starts nothing by itself
  • Usually paired with Wants= or Requires=
  • Also reversed on shutdown

Status, restart and reload in detail

In systemctl status, the Loaded line shows the unit file path, its install state and the vendor preset. The Active line shows the state and since when. Below come the main PID, the number of tasks, memory use, the cgroup tree with every child process, and the last ten journal lines.

restart stops and starts the service, dropping connections. reload asks the running service to re-read its configuration, if it supports that, without stopping. reload-or-restart picks reload when possible, and try-restart only restarts if it is already running, which suits scripts.

For scripts, systemctl is-active, is-enabled and is-failed print one word and return an exit code. systemctl show httpd -p MainPID -p ActiveState prints single properties. After you fix a failed unit, systemctl reset-failed clears the failed state and the restart counter.

  • systemctl --failed lists failed units
  • systemctl list-units --type=service --state=running
  • systemctl kill -s HUP httpd signals every process of the unit

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Start, stop and restart services~35m
  2. 2
    Enable and disable services at boot~35m
  3. 3
    Read status and failed units~35m
  4. 4
    Mask units to prevent starting~35m
  5. 5
    List dependencies of a unit~35m

Key points

  • start and stop affect now; enable and disable affect boot. enable --now does both.
  • systemctl status shows state, main PID, cgroup and the last log lines.
  • systemctl --failed lists everything that failed.
  • mask links a unit to /dev/null so it cannot be started at all.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
sudo systemctl enable --now httpd
systemctl status httpd
systemctl is-enabled httpd; systemctl is-active httpd
systemctl --failed
sudo systemctl mask --now cups
systemctl list-dependencies multi-user.target | head

Start and enable, inspect, find failures and mask.

Make sure only one firewall service can run

A server has both firewalld and iptables-services installed, and the old iptables service keeps getting started by a script. Step 1: check the state of both. Step 2: stop and mask iptables so nothing can start it, even by accident. Step 3: enable and start firewalld. Step 4: confirm the mask by trying to start iptables, which fails with a clear message.

bash
systemctl is-enabled firewalld iptables
sudo systemctl mask --now iptables
sudo systemctl enable --now firewalld
systemctl is-active firewalld        # active
sudo systemctl start iptables
# Failed to start iptables.service: Unit iptables.service is masked.
ls -l /etc/systemd/system/iptables.service   # -> /dev/null

Investigate a service that fails at boot

After a reboot the app service is failed, but starting it by hand works. That pattern usually means an ordering problem. Step 1: list failed units and read the status. Step 2: read the logs from this boot only. Step 3: look at what the unit waits for. Here the log says it could not reach the database, and the unit has Wants=postgresql.service but no After=, so both started at the same time. Adding After=postgresql.service in a drop-in fixes it.

bash
systemctl --failed
systemctl status app
journalctl -b -u app | tail -20
systemctl show app -p Wants -p After
sudo systemctl edit app       # add: [Unit] After=postgresql.service
sudo systemctl reset-failed app
sudo systemctl restart app

Quick reference

  • systemctl enable --now NAME starts a service and enables it at boot
  • systemctl list-unit-files --state=enabled shows what starts at boot
  • systemctl is-active NAME and is-enabled NAME print one word; great in scripts
  • systemctl mask --now NAME stops it and blocks every future start
  • systemctl list-dependencies --reverse NAME shows who pulls a unit in
  • Wants= and Requires= start units; After= and Before= only order them
  • systemctl reload-or-restart NAME reloads if supported, else restarts
  • systemd-analyze blame lists units by how long they took at boot

Common mistakes

  • Starting a service but forgetting to enable it, so it is gone after reboot.
  • Editing a unit file and forgetting systemctl daemon-reload.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1systemctl status shows a service as active (running) and disabled. What happens at the next reboot?

Question 2

2What does systemctl enable actually create?

Question 3

3A unit has After=network-online.target but no Wants= or Requires= for it. What does After= do on its own?

Question 4

4What does the static install state mean?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Install and start the chronyd time service so that it also runs after a reboot. Show both its active and enabled states.

  2. Exercise 2medium

    Find which target pulls in sshd at boot and prove it by showing the symlink that enable created.

  3. Exercise 3hard

    Write a one-line health check that prints the name of every failed unit and exits non-zero if there is at least one, suitable for a monitoring script.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between systemctl and service?
service is the old SysV-style command. On systemd systems it just forwards to systemctl, so service httpd restart and systemctl restart httpd do the same thing.
Do I need to type .service after the unit name?
No. systemctl assumes .service when no suffix is given, so systemctl status sshd works. Other types need their suffix, such as cleanup.timer.
When should I mask instead of disable?
Disable when you just do not want it at boot. Mask when nothing may start it at all, for example a service that conflicts with the one you use.
Why does my service show inactive (dead) right after starting?
Its main process exited. Check journalctl -u NAME; often the program forked into the background while the unit uses Type=simple, or it hit an error at start-up.

Go deeper

ShareXLinkedInWhatsApp