Skip to content

Beginner · Processes, services and logs · topic 15 of 57 · 35 min

systemd services and systemctl

Explanation

systemd starts and supervises services. Each service is described by a unit file. systemctl start/stop/restart controls it now, enable/disable decides whether it starts at boot (enable --now does both), status shows its state and last log lines, and mask blocks it completely.

What systemd does

A service (or daemon) is a program that runs in the background without a user, like the web server httpd or the SSH server sshd. systemd is the first process (PID 1). It starts services at boot in the right order, restarts them if they crash (when told to), collects their logs, and stops them cleanly at shutdown.

systemd manages units. A unit is anything systemd manages, described by a small text file. The type is the ending of its name:

Unit typeExampleWhat it is
.servicesshd.serviceA program to run
.socketcockpit.socketA network socket that starts a service on demand
.targetmulti-user.targetA group of units, like a "run level"
.timerdnf-makecache.timerA schedule that starts a service
.mounthome.mountA mounted file system
.pathcups.pathWatches a path and starts a unit when it changes

Everyday systemctl

bash
# systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: inactive (dead)
# systemctl enable --now httpd       # start now AND at every boot
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
# systemctl is-active httpd
active
# systemctl is-enabled httpd
enabled
# systemctl restart httpd    # stop then start
# systemctl reload httpd     # re-read config without stopping (if the service supports it)
# systemctl stop httpd
# systemctl disable httpd    # do not start at boot
You can leave off .service; systemctl adds it.

Two separate ideas trip people up. Active means running now. Enabled means it will start at boot. A service can be active but disabled (it will be gone after a reboot), or enabled but inactive (it failed, or you stopped it). In exams and in real work, most tasks need both: enable --now.

Listing units

bash
$ systemctl list-units --type=service --state=running
$ systemctl list-units --failed
  UNIT          LOAD   ACTIVE SUB    DESCRIPTION
● kdump.service loaded failed failed Crash recovery kernel arming
$ systemctl list-unit-files --type=service | grep enabled
$ systemctl cat sshd       # show the unit file
$ systemctl list-dependencies multi-user.target

Unit files and where they live

Packages install unit files in /usr/lib/systemd/system/. Your own units and changes go in /etc/systemd/system/, which wins over /usr/lib. Do not edit files in /usr/lib directly; an update will overwrite them. Instead run systemctl edit name, which creates a small override ("drop-in") file. After changing any unit file by hand, run systemctl daemon-reload so systemd reads it again.

ini
# /etc/systemd/system/notes-api.service
[Unit]
Description=Notes API
After=network-online.target
Wants=network-online.target

[Service]
User=notes
ExecStart=/usr/bin/python3 /opt/notes/app.py
Restart=on-failure

[Install]
WantedBy=multi-user.target
A small service of your own. Restart=on-failure makes systemd restart it if it crashes. WantedBy is used by enable.

mask: stronger than disable

disable only removes the boot-time link; another service or a person can still start it. mask links the unit to /dev/null so it cannot be started at all, by anyone, until unmask. Use it when two services conflict, for example to make sure iptables never runs alongside firewalld.

bash
# systemctl mask iptables
Created symlink /etc/systemd/system/iptables.service → /dev/null.
# systemctl start iptables
Failed to start iptables.service: Unit iptables.service is masked.
# systemctl unmask iptables

Common mistakes

  • Running only systemctl start in an exam task that says "the service must start at boot". The reboot check fails. Use enable --now.
  • Editing a unit file and forgetting systemctl daemon-reload. systemd keeps using the old version and warns you in status.
  • Reading "Active: failed" and immediately restarting. First read the log lines under status, or journalctl -u name, to see WHY.
Tip: systemctl status shows the last ten log lines of the service. Nine times out of ten the reason for a failure is right there.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

systemd services and systemctl · 1/7

systemd = PID 1

systemd is process number one. It starts, watches and stops services.

Prefer a video by a teacher? Find free videos on “systemctl systemd services enable start mask tutorial rhel” on YouTube ↗

In real life: A hospital lab system that did not come back after a power cut

A diagnostics lab in Lucknow had a power cut. When the server came back, the reports portal was down, although it had been working for months. The admin found that someone had started the service by hand months earlier but never enabled it. He ran systemctl enable --now, and added "check is-enabled" to the team's go-live checklist.

bash
# systemctl is-active labportal; systemctl is-enabled labportal
inactive
disabled
# systemctl enable --now labportal
Created symlink /etc/systemd/system/multi-user.target.wants/labportal.service → /etc/systemd/system/labportal.service.

Practice (0/3 done)

Task 1 · Question

Which command starts chronyd now AND makes it start at every boot?

Task 2 · Question

You changed /etc/systemd/system/notes-api.service. What must you run before restarting it?

Task 3 · Practice

Install httpd, make sure it runs now and after a reboot, check both states, then stop it and mask it, try to start it, and finally unmask and start it again.

Quick quiz

Question 1

1.A service shows "active" and "disabled". What happens after a reboot?

Question 2

2.Where should you put your own unit files?

Question 3

3.What does systemctl mask do?

Question 4

4.Which shows all units that have failed?

Question 5

5.What is the difference between restart and reload?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

A service shows "active" and "disabled". What happens after a reboot?

Answer: It does not start

Disabled means it is not started at boot.

Where should you put your own unit files?

Answer: /etc/systemd/system

/etc/systemd/system is for admin changes and wins over /usr/lib.

What does systemctl mask do?

Answer: Makes the unit impossible to start until unmasked

It links the unit to /dev/null.

Which shows all units that have failed?

Answer: systemctl list-units --failed

list-units --failed lists failed units.

What is the difference between restart and reload?

Answer: restart stops and starts; reload re-reads config without stopping, if supported

reload keeps the process and reloads its config; not every service supports it.

You started a service with systemctl start, but after a reboot it is not running. What did you forget?Related

Answer: systemctl enable

start runs it now; enable makes it start at boot.

From Processes, signals and systemd services

Which service must run for at jobs to execute?Related

Answer: atd

atd runs at jobs.

From One-time jobs with at, and systemd timers

Why does systemctl --user fail after su - webops?Related

Answer: su does not start a full login session with the user's systemd and runtime directory

Log in directly (SSH) as the user.

From Persistent storage and containers as systemd services

What does systemctl isolate do?Related

Answer: Switches the running system to that target now

isolate starts the target and stops units not needed by it.

From Targets: the boot goal of the system

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “Processes, services and logs” before its exam: Targets: the boot goal of the system, Logs: journalctl, rsyslog and a persistent journal, Tuning profiles with tuned, and time with chrony.
  2. Module 3 exam: Processes, services and logs · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
  3. Then the next module, Software and networking. 57 topics left in the course before the final exam.

← Previous: Signals, kill and process priority