Quick Summary: systemd is the init system and service manager used by virtually all modern Linux distributions. It manages the entire boot process, system services, logging, and more. The primary command for interacting with systemd is systemctl, which starts, stops, enables, and monitors services. This guide covers everything from basic service management to creating custom unit files.
What Is systemd?
systemd is the first process (PID 1) that starts when your Linux system boots. It replaces the older SysVinit and Upstart init systems. systemd manages service startup order, handles dependencies between services, monitors running services, and provides centralized logging through journald.
Essential systemctl Commands
| Command | Purpose |
|---|---|
systemctl start nginx | Start a service immediately |
systemctl stop nginx | Stop a running service |
systemctl restart nginx | Stop then start (brief downtime) |
systemctl reload nginx | Reload config without stopping |
systemctl enable nginx | Start automatically on boot |
systemctl disable nginx | Do not start on boot |
systemctl enable --now nginx | Enable AND start immediately |
systemctl status nginx | Show service status and recent logs |
systemctl is-active nginx | Check if running (for scripts) |
systemctl is-enabled nginx | Check if enabled on boot |
systemctl list-units --type=service | List all loaded services |
systemctl list-units --failed | List failed services |
Understanding Unit Files
Every service managed by systemd has a unit file that defines how it behaves. Unit files are typically stored in /etc/systemd/system/ (custom) or /usr/lib/systemd/system/ (package-installed).
Unit File Structure
| Section | Purpose | Key Directives |
|---|---|---|
[Unit] | Metadata and dependencies | Description, After, Requires, Wants |
[Service] | How the service runs | ExecStart, Type, Restart, User |
[Install] | When to start the service | WantedBy (usually multi-user.target) |
Creating a Custom Service
- Create the unit file at
/etc/systemd/system/myapp.service - Define the [Unit] section with Description and After directives
- Define the [Service] section with Type, User, WorkingDirectory, ExecStart, and Restart
- Define the [Install] section with WantedBy=multi-user.target
- Reload systemd:
sudo systemctl daemon-reload - Enable and start:
sudo systemctl enable --now myapp
Service Types
| Type | Behavior | Use Case |
|---|---|---|
| simple | ExecStart process IS the service | Most applications (default) |
| forking | Process forks and parent exits | Traditional daemons (Apache) |
| oneshot | Runs once then exits | Setup scripts, cleanup tasks |
| notify | Service signals readiness | Applications using sd_notify |
| idle | Runs after other jobs finish | Low-priority background tasks |
Restart Policies
| Directive | Behavior |
|---|---|
Restart=always | Always restart when the process exits |
Restart=on-failure | Restart only on non-zero exit codes |
Restart=on-abnormal | Restart on signals, timeouts, watchdog |
RestartSec=5 | Wait 5 seconds before restart |
StartLimitIntervalSec=300 | Rate limit: max restarts per interval |
StartLimitBurst=5 | Maximum 5 restarts per interval |
Logging with journald
journalctl -u nginxβ View logs for a specific servicejournalctl -u nginx --since "1 hour ago"β Time-filtered logsjournalctl -u nginx -fβ Follow logs in real timejournalctl -p errβ Show only error messagesjournalctl --disk-usageβ Check journal disk usagejournalctl --vacuum-size=500Mβ Limit journal to 500MB
Dependencies and Ordering
| Directive | Meaning |
|---|---|
After=network.target | Start after network is up |
Requires=postgresql.service | Hard dependency: fails if PostgreSQL fails |
Wants=redis.service | Soft dependency: starts Redis but continues if it fails |
Before=nginx.service | Start before NGINX |
Frequently Asked Questions
What is the difference between enable and start?
systemctl start starts a service immediately but it will not survive a reboot. systemctl enable configures the service to start automatically on boot but does not start it now. Use systemctl enable --now to do both at once.
How do I find why a service failed?
Run systemctl status servicename for a quick overview, then journalctl -u servicename -n 50 for detailed logs. Look for error messages, permission denied errors, or missing files/directories.
What does daemon-reload do?
systemctl daemon-reload tells systemd to re-read all unit files from disk. You must run this after creating or modifying any unit file before the changes take effect. It does not restart any services.
How do I make a service restart automatically after crashes?
Add Restart=on-failure (or Restart=always) and RestartSec=5 to the [Service] section. This tells systemd to automatically restart the service 5 seconds after it exits with a non-zero status code.