Master Systemd: Enable, Start, Stop, and Configure Linux Services
This guide explains how to enable a systemd service to start at boot, manually start and stop services, interpret status output, read and edit unit files, understand the [Unit], [Service], and [Install] sections, configure dependencies, startup types, restart policies, and targets, and finally reload and restart services after configuration changes.
1. Enable service at boot
For software that supports systemd, installation adds a unit file to /usr/lib/systemd/system. To enable the service to start on boot, run sudo systemctl enable httpd.service. This creates a symbolic link in /etc/systemd/system that points to the original unit file.
2. Start a service
Enabling a service does not start it immediately; to run it now, execute sudo systemctl start httpd.service. If the start fails, check the service status with sudo systemctl status httpd.service. The status output includes fields such as Loaded (location and enablement), Active (running state), Main PID, Status (application‑provided state), and CGroup (process tree).
3. Stop a service
To stop a running service, use sudo systemctl stop httpd.service. If the service does not respond, you can force termination with sudo systemctl kill httpd.service. Restarting is done via sudo systemctl restart httpd.service.
4. Read a unit file
Unit files reside in /usr/lib/systemd/system or /etc/systemd/system. Use systemctl cat sshd.service to view the file. A typical unit file is divided into sections: [Unit], [Service], and [Install], each containing key‑value pairs.
5. [Unit] section – ordering and dependencies
The Description and Documentation fields provide a brief description and docs. After and Before define start order, while Wants and Requires express weak and strong dependencies respectively. Weak dependencies allow the dependent service to start even if the required service fails, whereas strong dependencies cause the dependent service to stop if the required one fails.
6. [Service] section – start behavior
6.1 Exec commands : ExecStart defines the command to launch the service; optional EnvironmentFile can supply key=value pairs. Additional commands include ExecReload, ExecStop, ExecStartPre, ExecStartPost, ExecStopPost. A leading hyphen (e.g., EnvironmentFile=-/etc/sysconfig/sshd) suppresses errors if the file is missing.
6.2 Startup type : The Type field can be simple (default), forking, oneshot, dbus, notify, or idle, each affecting how systemd treats the process lifecycle.
6.3 Restart behavior : KillMode controls which processes are terminated (options: control-group, process, mixed, none). Restart defines when to restart (e.g., on-failure, always, etc.), and RestartSec sets the delay before a restart.
7. [Install] section – enabling the unit
The WantedBy (or RequiredBy) field specifies the target that pulls the unit in. Enabling a service creates a symlink under /etc/systemd/system/<target>.wants/, linking the unit to the chosen target (commonly multi-user.target).
8. Targets
Targets group units. The default boot target is obtained with systemctl get-default (usually multi-user.target). Targets have no Exec commands; they only declare ordering ( After), requirements ( Requires), conflicts, and whether they can be isolated ( AllowIsolate=yes). Use systemctl list-dependencies to view a target’s units and systemctl isolate to switch to a different target.
9. Reloading after changes
After editing unit files, reload the daemon with sudo systemctl daemon-reload and restart the affected service, e.g., sudo systemctl restart foobar.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
