Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 11Naam in het boek: Working with Systemd

Start, stop and configure services with systemd

systemd starts everything after the kernel: services, mounts, sockets and targets. This chapter shows how to read units, start and enable services, check their state, change their settings safely and understand dependencies.

  • 14 Lessen
  • 4 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Examendoelen in dit hoofdstuk

  • Services starten en stoppen en ze bij het opstarten laten starten

11.1

Understand systemd and units

systemd manages units. A unit is one thing to manage, described in a small text file. The file name ends with the unit type.

TypeExampleManages
.servicesshd.serviceA program that runs (daemon)
.socketcockpit.socketA port or socket; starts the service on the first connection
.mounthome.mountA mounted file system
.targetmulti-user.targetA group of units (like a runlevel)
.timerfstrim.timerA time plan that starts a service
.path, .slice, .device, .swapOther types
systemctl -t help
systemctl list-units -t service
systemctl list-unit-files -t service

11.2

Find the unit files

Unit files live in three places. A file in /etc wins over the same name in /usr/lib.

FolderWho writes itPriority
/usr/lib/systemd/system/Packages (do not edit)Lowest
/run/systemd/system/Created at run timeMiddle
/etc/systemd/system/You, the adminHighest
systemctl cat sshd.service
systemctl show sshd.service -p FragmentPath,DropInPaths

Let op

Never edit files in /usr/lib/systemd/system/ — a package update overwrites them. Use systemctl edit (see below).

11.3

Read a service unit

A service unit has three sections: [Unit] (description and order), [Service] (how to run it) and [Install] (what enable does).

/usr/lib/systemd/system/sshd.service (short)
[Unit]
Description=OpenSSH server daemon
After=network.target sshd-keygen.target
Wants=sshd-keygen.target
 
[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=42s
 
[Install]
WantedBy=multi-user.target
LineMeaning
ExecStartThe command that starts the service
Restart=on-failureStart it again when it crashes
After=Start after these units (order only)
Wants= / Requires=Also start these units (soft / hard dependency)
WantedBy=multi-user.targetenable links the service into this target

11.4

Know mount and socket units

systemd turns every line in /etc/fstab into a .mount unit. A .socket unit listens on a port and starts the service only when a client connects.

systemctl list-units -t mount
systemctl status home.mount
systemctl cat cockpit.socket
systemctl list-sockets

Notitie

The name of a mount unit is the path with - instead of /: /data/backup → data-backup.mount. systemd-escape -p /data/backup prints it.

11.5

Understand targets

A target is a group of units that brings the system into a state. They replace the old runlevels.

TargetStateOld runlevel
graphical.targetMulti-user with a desktop5
multi-user.targetMulti-user, text only (servers)3
rescue.targetMinimal system, single user, root login1
emergency.targetEven less: root file system read-only—
systemctl get-default
systemctl list-units -t target
systemctl list-dependencies multi-user.target | head

Boot targets are covered in the boot chapter.

11.6

Start, stop and enable services

start/stop change the service now; enable/disable decide if it starts at boot. enable --now does both.

systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl reload httpd
systemctl enable httpd
systemctl enable --now httpd
systemctl disable --now httpd
CommandNowAt boot
start / stopyesno
enable / disablenoyes
enable --now / disable --nowyesyes
reloadre-read the config without stopping—

Examentip

Every service you set up on the exam must also run after a reboot. Always use systemctl enable --now SERVICE, and check with systemctl is-enabled SERVICE.

11.7

Check the state of a service

systemctl status shows if a service is loaded, enabled, active, its PID, cgroup and the last log lines.

systemctl status sshd
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
     Active: active (running) since Sun 2026-10-25 07:58:30 CET; 2h 54min ago
   Main PID: 1010 (sshd)
systemctl is-active sshd
systemctl is-enabled sshd
systemctl --failed
WordMeaning
loadedThe unit file was read
enabled / disabledStarts at boot / does not
staticCannot be enabled itself; another unit starts it
maskedBlocked: cannot be started at all
active (running)Runs now
active (exited)Ran once and finished (OK for one-shot units)
inactive (dead)Not running
failedCrashed or could not start

11.8

Block a service with mask

mask links the unit to /dev/null, so nobody — not even a dependency — can start it. Use it for services that must never run together, or must never run.

systemctl mask --now iptables
systemctl start iptables
Failed to start iptables.service: Unit iptables.service is masked.
systemctl unmask iptables

11.9

Understand dependencies

Units can need other units. Order (After, Before) and need (Wants, Requires) are separate settings.

SettingMeaning
Requires=BStart B too; if B fails or stops, stop me too
Wants=BStart B too; I do not care if it fails
Requisite=BB must already be running, or I fail
Conflicts=BNever run together with B
After=B / Before=BOnly the order of start and stop
systemctl list-dependencies httpd
systemctl list-dependencies --reverse sshd

11.10

Change service settings safely

systemctl edit creates a drop-in file in /etc/systemd/system/NAME.service.d/override.conf. Only your changed lines go in it; the rest comes from the original file.

  1. Open the override editor and add your settings.
    systemctl edit httpd
    /etc/systemd/system/httpd.service.d/override.conf
    [Service]
    Restart=always
    RestartSec=5s
    MemoryMax=1G
  2. systemctl edit reloads systemd for you. After editing files by hand, reload yourself and restart the service.
    systemctl daemon-reload
    systemctl restart httpd
  3. Check the result.
    systemctl cat httpd
    systemctl show httpd -p Restart,MemoryMax

Op het werk

Useful hardening lines for your own services: NoNewPrivileges=yes, ProtectSystem=strict, ProtectHome=yes, PrivateTmp=yes. systemd-analyze security NAME rates a service.

11.11

Write a simple service of your own

Any program or script can become a service with a small unit file in /etc/systemd/system/.

  1. Create the unit file.
    /etc/systemd/system/hello.service
    [Unit]
    Description=Hello demo service
    After=network-online.target
     
    [Service]
    ExecStart=/usr/bin/bash -c 'while true; do echo "hello $(date)"; sleep 60; done'
    Restart=on-failure
     
    [Install]
    WantedBy=multi-user.target
  2. Load it, start it and enable it.
    systemctl daemon-reload
    systemctl enable --now hello
  3. See its output in the journal.
    journalctl -u hello -f

11.12

Remember the main points

The most important facts of this chapter, in short.

  • systemd starts and watches everything as units: services, sockets, mounts, timers, targets and more.
  • Unit files live in /usr/lib/systemd/system (from packages) and /etc/systemd/system (your changes win).
  • systemctl start|stop|restart|reload NAME works now; enable|disable works at the next boot; enable --now does both.
  • systemctl status NAME shows state, the main PID and the last log lines.
  • mask makes a unit impossible to start; unmask undoes it.
  • Change a unit with systemctl edit NAME (a drop-in file); systemctl daemon-reload after you edit unit files by hand.
  • Dependencies: Wants= and Requires= say what else must run; After= and Before= set the order.
  • A target (like multi-user.target) is a group of units for a system state.

11.13

Learn the key terms

The words of this chapter in simple English.

TermMeaning
systemdThe first process (PID 1); it starts and manages all units.
UnitOne thing systemd manages, like a service or a mount.
Unit fileThe text file that describes a unit.
ServiceA unit that runs a program (a daemon).
Socket unitA unit that listens on a port and starts a service when needed.
TargetA group of units for one system state.
EnableStart a unit automatically at boot.
MaskBlock a unit so nothing can start it.
Drop-in fileA small file that changes a part of a unit file.
DaemonA program that runs in the background as a service.

11.14

Check what you learned

Think about the answer first, then open it.

1How do you start httpd now and at every boot with one command?
2How do you check if a service starts at boot?
3What is the difference between disable and mask?

disable only stops the automatic start; mask blocks every start, also by hand or by other units.

4Where do you put your own changes to a unit so updates do not remove them?

In a drop-in under /etc/systemd/system/NAME.service.d/, made with systemctl edit NAME.

5What must you run after changing a unit file by hand?
6How do you list all services that failed?
7How do you see which units a target pulls in?
8How do you make a service restart by itself when it crashes?

Add Restart=on-failure in a drop-in (systemctl edit NAME), then restart it.

Oefenen

Alle commando's in dit hoofdstuk