systemctl
Overview
systemctl is the primary interface to systemd: start/stop units, enable on boot, inspect status, and list dependencies. Most modern distros (including Ubuntu) manage services this way.
Syntax
systemctl [options] command [unit...]Units end in suffixes: .service, .socket, .timer, .mount, .target, …
Common Commands
| Command | Description |
|---|---|
status UNIT |
State, PIDs, recent logs |
start / stop / restart |
Lifecycle |
reload |
Reload config if supported |
enable / disable |
Boot linkage |
is-active / is-enabled |
Script-friendly checks |
mask / unmask |
Prevent start entirely |
daemon-reload |
Reload unit files after edit |
list-units |
Active units |
list-unit-files |
Installed unit files |
cat UNIT |
Show unit text |
edit UNIT |
Drop-in override |
reboot / poweroff |
System actions (also shutdown) |
Key Use Cases
- Operate application services
- Enable services at boot
- Debug failed units
- Override vendor unit settings safely
Safety
mask makes a unit impossible to start (symlink to /dev/null) — easy to forget. Prefer stop + disable unless you must block activation. Do not restart critical remote access (sshd) without a recovery path.
Examples with Explanations
Status and logs peek
systemctl status nginx
systemctl status nginx --no-pager -lStart / enable
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl enable --now nginx # enable + startRestart / reload
sudo systemctl reload nginx # graceful if available
sudo systemctl restart nginxFailed units
systemctl --failed
systemctl list-units --state=failedScript checks
systemctl is-active --quiet nginx && echo running
systemctl is-enabled nginxAfter editing a unit file
sudo systemctl daemon-reload
sudo systemctl restart myapp.serviceDrop-in override
sudo systemctl edit nginx
# opens editor for /etc/systemd/system/nginx.service.d/override.confList timers
systemctl list-timers --allDependencies
systemctl list-dependencies nginx.serviceUnderstanding Output
status shows load state (loaded/masked), active state (running/failed/dead), main PID, cgroup, and a journal snippet. Exit codes from is-active / is-enabled are designed for shell scripts.
Common Usage Patterns
Follow service logs (via journalctl)
journalctl -u nginx -fReset failed marker after fix
sudo systemctl reset-failed myapp.serviceNotes & Pitfalls
- User services:
systemctl --user(lingering may be needed for background).
restart= stop+start;try-restartonly if running.
- Unit names can be abbreviated when unique (
systemctl status nginx).
Additional Resources
man systemctl
man systemd.service