systemd: units, targets, systemctl
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
systemctl enable --now httpd— start now and at boot; RHCSA has the condition "must keep working after a reboot" in every task.systemctl status,is-active,is-enabled,--failed,list-units --type=service.- Unit files:
/usr/lib/systemd/system/(package),/etc/systemd/system/(admin, takes precedence); after a change,daemon-reload. - Targets:
multi-user.target(server),graphical.target;systemctl get-default/set-default. systemctl maskblocks a service completely (e.g. iptables conflicting with firewalld);systemctl editcreates an override.
Now and at boot
We covered systemd in detail in the Ubuntu course. On RHEL the main difference is philosophical: installing a package does not start the service.
[ali@srv-web01 ~]$ systemctl is-active httpd; systemctl is-enabled httpd inactive disabled [ali@srv-web01 ~]$ sudo systemctl enable --now httpd Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. [ali@srv-web01 ~]$ systemctl is-active httpd; systemctl is-enabled httpd active enabled
| Command | Now | At boot |
|---|---|---|
start / stop | ✓ | — |
enable / disable | — | ✓ |
enable --now / disable --now | ✓ | ✓ |
restart | Stop + start | — |
reload | Re-read configuration | — |
mask / unmask | Blocks it (start does not work either) | ✓ |
In RHCSA almost every task has the condition "must keep working after a reboot". The examiner reboots the server and checks — running start without enable means losing the task.
Reading the state
[ali@srv-web01 ~]$ systemctl list-units --type=service --state=running | head -n 5 UNIT LOAD ACTIVE SUB DESCRIPTION auditd.service loaded active running Security Auditing Service chronyd.service loaded active running NTP client/server crond.service loaded active running Command Scheduler firewalld.service loaded active running firewalld - dynamic firewall daemon [ali@srv-web01 ~]$ systemctl list-unit-files --type=service | grep enabled | wc -l 31 [ali@srv-web01 ~]$ systemctl --failed UNIT LOAD ACTIVE SUB DESCRIPTION 0 loaded units listed.
Service names on RHEL differ from Ubuntu: sshd (ssh on Ubuntu), crond (cron), chronyd (chrony), httpd (apache2).
Unit files and overrides
[ali@srv-web01 ~]$ systemctl cat httpd | head -n 12 # /usr/lib/systemd/system/httpd.service [Unit] Description=The Apache HTTP Server Wants=httpd-init.service After=network.target remote-fs.target nss-lookup.target httpd-init.service Documentation=man:httpd.service(8) [Service] Type=notify Environment=LANG=C ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
An override so httpd restarts by itself when it fails:
[ali@srv-web01 ~]$ sudo mkdir -p /etc/systemd/system/httpd.service.d [ali@srv-web01 ~]$ printf "[Service]\nRestart=on-failure\nRestartSec=5\n" | sudo tee /etc/systemd/system/httpd.service.d/restart.conf [ali@srv-web01 ~]$ sudo systemctl daemon-reload [ali@srv-web01 ~]$ systemctl show httpd -p Restart Restart=on-failure
sudo systemctl edit httpd creates the same file interactively. Do not edit the file in /usr/lib/systemd/system/ directly — it will be lost when the package updates.
Targets
[ali@srv-web01 ~]$ systemctl get-default multi-user.target [ali@srv-web01 ~]$ sudo systemctl set-default multi-user.target [ali@srv-web01 ~]$ systemctl list-dependencies multi-user.target | head -n 6 multi-user.target ● ├─auditd.service ● ├─chronyd.service ● ├─crond.service ● ├─firewalld.service ● ├─httpd.service
| Target | Old runlevel | Meaning |
|---|---|---|
poweroff.target | 0 | Power off |
rescue.target | 1 | Single user, recovery |
multi-user.target | 3 | Server: network, services, no graphics |
graphical.target | 5 | Graphical desktop |
reboot.target | 6 | Reboot |
To switch a server with a graphical interface to text mode at boot: set-default multi-user.target. To switch now (without rebooting): systemctl isolate multi-user.target.
mask: blocking completely
When two services do the same job (for example firewalld and the old iptables-services), so you do not start one by accident:
[ali@srv-web01 ~]$ sudo systemctl mask iptables Created symlink /etc/systemd/system/iptables.service → /dev/null. [ali@srv-web01 ~]$ sudo systemctl start iptables Failed to start iptables.service: Unit iptables.service is masked.
mask points the unit file at /dev/null. To undo: systemctl unmask iptables.
Lab: start httpd
The "Start httpd" task in the Red Hat simulator:
- Start httpd now and at boot.
- Open the test page from the server itself.
[ali@srv-web01 ~]$ sudo systemctl enable --now httpd [ali@srv-web01 ~]$ systemctl is-active httpd; systemctl is-enabled httpd active enabled [ali@srv-web01 ~]$ curl -s http://localhost | grep -o "<title>.*</title>"
- (On your own VM) create a
Restart=on-failureoverride for httpd, rundaemon-reload, check withsystemctl show httpd -p Restart. Then kill the httpd process withkill -9and see it come back by itself after 5 seconds. systemctl get-default— it should bemulti-user.target.
Check: the task is green; is-enabled → enabled.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1Which command makes a service run now and come back after a reboot?
2What is the SSH service called on RHEL?
3What is the right way to change a package's unit file?
4What does systemctl mask iptables do?
5The server should boot into text mode, not graphics. Which command?
Comments
Sign in to comment.