هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
أهداف الامتحان في هذا الفصل
- تشغيل الخدمات وإيقافها وجعلها تعمل عند الإقلاع
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.
| Type | Example | Manages |
|---|---|---|
.service | sshd.service | A program that runs (daemon) |
.socket | cockpit.socket | A port or socket; starts the service on the first connection |
.mount | home.mount | A mounted file system |
.target | multi-user.target | A group of units (like a runlevel) |
.timer | fstrim.timer | A time plan that starts a service |
.path, .slice, .device, .swap | Other types |
Find the unit files
Unit files live in three places. A file in /etc wins over the same name in /usr/lib.
| Folder | Who writes it | Priority |
|---|---|---|
/usr/lib/systemd/system/ | Packages (do not edit) | Lowest |
/run/systemd/system/ | Created at run time | Middle |
/etc/systemd/system/ | You, the admin | Highest |
انتبه
Never edit files in /usr/lib/systemd/system/ — a package update overwrites them. Use systemctl edit (see below).
Read a service unit
A service unit has three sections: [Unit] (description and order), [Service] (how to run it) and [Install] (what enable does).
[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| Line | Meaning |
|---|---|
ExecStart | The command that starts the service |
Restart=on-failure | Start it again when it crashes |
After= | Start after these units (order only) |
Wants= / Requires= | Also start these units (soft / hard dependency) |
WantedBy=multi-user.target | enable links the service into this target |
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.
Understand targets
A target is a group of units that brings the system into a state. They replace the old runlevels.
| Target | State | Old runlevel |
|---|---|---|
graphical.target | Multi-user with a desktop | 5 |
multi-user.target | Multi-user, text only (servers) | 3 |
rescue.target | Minimal system, single user, root login | 1 |
emergency.target | Even less: root file system read-only | — |
systemctl get-default
systemctl list-units -t target
systemctl list-dependencies multi-user.target | headBoot targets are covered in the boot chapter.
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| Command | Now | At boot |
|---|---|---|
start / stop | yes | no |
enable / disable | no | yes |
enable --now / disable --now | yes | yes |
reload | re-read the config without stopping | — |
نصيحة للامتحان
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.
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| Word | Meaning |
|---|---|
loaded | The unit file was read |
enabled / disabled | Starts at boot / does not |
static | Cannot be enabled itself; another unit starts it |
masked | Blocked: cannot be started at all |
active (running) | Runs now |
active (exited) | Ran once and finished (OK for one-shot units) |
inactive (dead) | Not running |
failed | Crashed or could not start |
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.
Understand dependencies
Units can need other units. Order (After, Before) and need (Wants, Requires) are separate settings.
| Setting | Meaning |
|---|---|
Requires=B | Start B too; if B fails or stops, stop me too |
Wants=B | Start B too; I do not care if it fails |
Requisite=B | B must already be running, or I fail |
Conflicts=B | Never run together with B |
After=B / Before=B | Only the order of start and stop |
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.
- 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 - systemctl edit reloads systemd for you. After editing files by hand, reload yourself and restart the service.
- Check the result.
systemctl edit --full NAMEcopies the whole file to/etc/systemd/system/(harder to update later).systemctl revert NAMEremoves your changes.
في العمل
Useful hardening lines for your own services: NoNewPrivileges=yes, ProtectSystem=strict, ProtectHome=yes, PrivateTmp=yes. systemd-analyze security NAME rates a service.
Write a simple service of your own
Any program or script can become a service with a small unit file in /etc/systemd/system/.
- 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 - Load it, start it and enable it.
- See its output in the journal.
journalctl -u hello -f
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 NAMEworks now;enable|disableworks at the next boot;enable --nowdoes both.systemctl status NAMEshows state, the main PID and the last log lines.maskmakes a unit impossible to start;unmaskundoes it.- Change a unit with
systemctl edit NAME(a drop-in file);systemctl daemon-reloadafter you edit unit files by hand. - Dependencies:
Wants=andRequires=say what else must run;After=andBefore=set the order. - A target (like
multi-user.target) is a group of units for a system state.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| systemd | The first process (PID 1); it starts and manages all units. |
| Unit | One thing systemd manages, like a service or a mount. |
| Unit file | The text file that describes a unit. |
| Service | A unit that runs a program (a daemon). |
| Socket unit | A unit that listens on a port and starts a service when needed. |
| Target | A group of units for one system state. |
| Enable | Start a unit automatically at boot. |
| Mask | Block a unit so nothing can start it. |
| Drop-in file | A small file that changes a part of a unit file. |
| Daemon | A program that runs in the background as a service. |
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.