Linux Systemd and Service Management
systemd is a widely used initialization and service management system on modern Linux distributions. It starts system services, manages dependencies, handles system states, and provides tools for monitoring and troubleshooting services.
What is systemd?
systemd is responsible for starting important services during boot and managing them while the system is running. It uses units to represent services, sockets, timers, mounts, and other system resources.
Boot → systemd → Dependencies → Services → Running System
What is systemctl?
systemctl is the primary command used to communicate with systemd. It can start, stop, restart, enable, disable, and inspect system services.
systemctl status nginx
sudo systemctl start nginx
sudo systemctl stop nginx
Starting a Service
The systemctl start command starts a service immediately. Starting a service does not necessarily mean it will automatically start after the next reboot.
sudo systemctl start nginx
Stopping a Service
The stop command terminates a running service when the service supports normal shutdown.
sudo systemctl stop nginx
Restarting and Reloading Services
restart stops and starts a service again, while reload asks a service to reload its configuration without a full restart when supported.
sudo systemctl restart nginx
sudo systemctl reload nginx
Checking Service Status
The status command provides information about whether a service is running, recent logs, its process ID, and recent systemd events.
systemctl status nginx
Enabling Services at Boot
The enable command configures a service to start automatically during the appropriate boot target. The disable command removes that configuration.
sudo systemctl enable nginx
sudo systemctl disable nginx
Enable and Start Together
The --now option can be used with enable or disable to apply the runtime state immediately as well as changing the boot configuration.
sudo systemctl enable --now nginx
sudo systemctl disable --now nginx
Listing Services
systemctl can list active services and other systemd units.
systemctl list-units --type=service
systemctl list-unit-files --type=service
Viewing Logs with journalctl
journalctl is used to query logs collected by the systemd journal. It is especially useful when a service fails to start or unexpectedly stops.
journalctl -u nginx
journalctl -u nginx --since today
journalctl -u nginx -n 50
Systemd Command Table
| Command | Purpose | Example |
|---|---|---|
| systemctl start | Start a service | sudo systemctl start nginx |
| systemctl stop | Stop a service | sudo systemctl stop nginx |
| systemctl restart | Restart a service | sudo systemctl restart nginx |
| systemctl status | Check service status | systemctl status nginx |
| systemctl enable | Start service at boot | sudo systemctl enable nginx |
| systemctl disable | Disable automatic startup | sudo systemctl disable nginx |
| journalctl | View systemd logs | journalctl -u nginx |
Checking Failed Services
systemd can identify units that have entered a failed state. This is useful when troubleshooting boot or application problems.
systemctl --failed
Example Service Troubleshooting Scenario
Service Fails → systemctl status → journalctl -u service → Check Configuration → Fix Problem → Restart Service
Systemd Units
systemd manages more than traditional services. Different unit types represent different system resources and tasks.
- Service units manage background services
- Socket units manage sockets
- Timer units schedule tasks
- Mount units manage mount points
- Target units group related units
Common Service Management Problems
- Service fails during startup
- Incorrect configuration
- Missing dependencies
- Port already in use
- Permission problems
- Service disabled at boot
Common Mistakes to Avoid
- Restarting services without checking logs
- Enabling unnecessary services at boot
- Editing service files incorrectly
- Ignoring dependency failures
- Running privileged commands without understanding their effect
Advanced systemd Concepts
- Unit files
- Service dependencies
- Targets
- Timers
- Socket activation
- Resource limits
- Custom systemd services
Practice Exercises
- Check the status of a service
- Start and stop a test service
- Enable a service at boot
- View service logs with journalctl
- Find failed systemd units
- Restart a service after configuration changes
Conclusion
systemd provides a central way to manage services and many other system resources on modern Linux systems. Learning systemctl and journalctl is essential for Linux administration, service deployment, and troubleshooting.