Services and daemons form the backbone of Linux system functionality by running background tasks and providing ongoing services required for system operation and user applications. Managing these services effectively is essential for maintaining system stability, security, and performance.
Linux systems primarily use the systemd init system for service management, providing powerful tools like systemctl to start, stop, enable, disable, and monitor services and daemons.
What are Services and Daemons?
Daemon: A background process that runs continuously or on-demand without direct user interaction, often providing essential system functions (e.g., sshd for SSH access).
Service: A broader term referring to programs that perform specific functions, including daemons and other long-running processes.
By convention, many daemons have names ending with “d” (e.g., crond, httpd).
Systemd and Service Management
systemd is the modern init system that has replaced traditional SysV init scripts. Running as PID 1, it orchestrates system startup, launches services, and manages system shutdown. Additionally, systemd handles service dependencies, parallelizes service startup, and tracks process groups to ensure efficient and reliable system management.
Common systemctl Commands
Managing Legacy Services
Older distributions or legacy systems may use service commands:
sudo service sshd start
sudo service sshd stop
sudo service sshd statusOn systemd systems, service often redirects to systemctl.
Creating and Modifying Custom Services
Create systemd service unit files under /etc/systemd/system/.
Basic unit file structure:
[Unit]
Description=My Custom Service
[Service]
ExecStart=/usr/local/bin/my_script.sh
[Install]
WantedBy=multi-user.targetAfter creating/editing, reload systemd daemon and enable/start service:
sudo systemctl daemon-reload
sudo systemctl enable myservice
sudo systemctl start myserviceMonitoring and Troubleshooting
1. Use systemctl status and journalctl for detailed logs.
2. Check dependencies with systemctl list-dependencies myservice.
3. Masking a service prevents it from being started manually or automatically:
sudo systemctl mask myservice