Beginner · Processes, services and logs · topic 15 of 57 · 35 min
systemd services and systemctl
Explanation
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 type | Example | What it is |
|---|---|---|
| .service | sshd.service | A program to run |
| .socket | cockpit.socket | A network socket that starts a service on demand |
| .target | multi-user.target | A group of units, like a "run level" |
| .timer | dnf-makecache.timer | A schedule that starts a service |
| .mount | home.mount | A mounted file system |
| .path | cups.path | Watches a path and starts a unit when it changes |
Everyday systemctl
# 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 bootTwo 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
$ 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.targetUnit 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.
# /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.targetmask: 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.
# 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 iptablesCommon 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.
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 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.
# 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)
Which command starts chronyd now AND makes it start at every boot?
You changed /etc/systemd/system/notes-api.service. What must you run before restarting it?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Targets: the boot goal of the systemProcesses, services and logs · same module
- Processes, signals and systemd servicesDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- One-time jobs with at, and systemd timersAutomation basics: scheduling, archives and shell scripts
- Persistent storage and containers as systemd servicesBoot, recovery and containers
- Permanent mounts with /etc/fstab and UUIDsStorage: partitions, file systems and LVM
- Linux basics for backend developersBackend Development · Deployment and scaling
Questions people ask about this
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.
Which service must run for at jobs to execute?Related
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.
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.
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
- 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.
- Module 3 exam: Processes, services and logs · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
- Then the next module, Software and networking. 57 topics left in the course before the final exam.