Situatie
systemctl manages systemd (init/system manager) units: services, sockets, timers, targets, mounts. Use it to start/stop services, enable them at boot, inspect status, and control units.
Solutie
Common commands
- Start a service now
sudo systemctl start nginx.service
- Stop a service now
sudo systemctl stop nginx.service
- Restart (stop then start)
sudo systemctl restart nginx.service
- Reload configuration (if supported) without full restart
sudo systemctl reload nginx.service
- Check status (shows active state and logs)
systemctl status nginx.service
- Show recent journal logs for a unit
journalctl -u nginx.service
- Follow logs in real time
journalctl -u nginx.service -f
Enable/disable at boot
- Enable service to start on boot
sudo systemctl enable nginx.service
- Disable service from starting on boot
sudo systemctl disable nginx.service
- Enable and start immediately
sudo systemctl enable --now nginx.service
- Mask a service (prevent any activation)
sudo systemctl mask nginx.service
- Unmask
sudo systemctl unmask nginx.service
Inspect and list units
- List all loaded units
systemctl list-units
- List all unit files (enabled/disabled)
systemctl list-unit-files
- Show unit properties
systemctl show nginx.service
Boot targets (runlevels)
- Show current target
systemctl get-default
- Set default target (e.g., multi-user or graphical)
sudo systemctl set-default multi-user.target
- Switch to a target immediately
sudo systemctl isolate multi-user.target
Power management
- Reboot now
sudo systemctl reboot
- Power off
sudo systemctl poweroff
- Suspend
sudo systemctl suspend
Useful flags and tips
- Unit name can omit the suffix for common types (systemctl status nginx).
- Use –now with enable/disable to act immediately.
- Use –no-pager to prevent paging of output: systemctl status nginx –no-pager
- Check exit status ($?) after commands in scripts; many commands require sudo.
- journalctl shows systemd logs; combine with -u to focus on a unit.
Examples
- Start nginx, check status, follow logs:
sudo systemctl start nginx
systemctl status nginx
journalctl -u nginx -f
- Enable and start a custom service file:
sudo cp myapp.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now myapp.service
- Debug a failing service (show full logs and properties):
systemctl status myapp.service --no-pager
journalctl -u myapp.service --since "1 hour ago"
systemctl show myapp.service
Leave A Comment?