3.3 Service Management with Systemd
Systemd is the init system used by almost all modern major distributions. It manages “Units”, most commonly “Service Units” (.service). It replaces older init systems and manages processes (see [[/en/module-2/5-processes|Process Control]]) more robustly.
Systemctl Commands
Section titled “Systemctl Commands”The main tool for interacting with systemd.
| Action | Command | Explanation |
|---|---|---|
| Start | sudo systemctl start nginx | Start service now. |
| Stop | sudo systemctl stop nginx | Stop service now. |
| Restart | sudo systemctl restart nginx | Stop, then start. |
| Reload | sudo systemctl reload nginx | Reload config without stopping (zero downtime). |
| Enable | sudo systemctl enable nginx | Start automatically on boot. |
| Disable | sudo systemctl disable nginx | Do not start on boot. |
| Status | systemctl status nginx | Check if running, enabled, and view latest logs. |
Cheatsheet: The “Enable and Start” Combo
Section titled “Cheatsheet: The “Enable and Start” Combo”You often want to do both.
Unit Files
Section titled “Unit Files”Service definitions live in /lib/systemd/system/ (defaults) and /etc/systemd/system/ (custom overrides).
Practice Exercises
Section titled “Practice Exercises”- Investigate SSH:
- Check the status of the SSH service:
systemctl status ssh(orsshd). - Is it active? Is it enabled?
- Check the status of the SSH service:
- Manage a Dummy Service:
- Install
apache2ornginxif you can, or pick an existing service likecron. - Stop the service. Verify it’s stopped.
- Start it again.
- Disable it (so it won’t start on boot).
- Enable it again.
- Install
- Logs:
- Use
journalctl -u ssh(orsshd) to view logs specifically for that service.
- Use