Managing services in Linux depends on the system’s init system. Most modern distributions use systemd, while older ones might still use SysV init.
Contents
1. Using systemd
(Most Modern Distributions)
Most Linux systems (Ubuntu 16.04+, Debian 8+, CentOS 7+, etc.) use systemd
.
Start a Service
sudo systemctl start <service-name>
# Example:
sudo systemctl start nginx
Stop a Service
sudo systemctl stop <service-name>
# Example:
sudo systemctl stop sshd
Restart a Service
sudo systemctl restart <service-name> # Full restart
sudo systemctl reload <service-name> # Reload config (if supported)
# Example:
sudo systemctl restart apache2
Reload Configuration (Without Restart)
sudo systemctl reload <service-name> # Only reload config files
# Example:
sudo systemctl reload nginx
Check Service Status
sudo systemctl status <service-name>
# Example:
sudo systemctl status mysql
Enable/Disable Service at Boot
sudo systemctl enable <service-name> # Start automatically at boot
sudo systemctl disable <service-name> # Disable auto-start
# Example:
sudo systemctl enable docker
Restart vs. Reload
restart
: Stops and starts the service (downtime).reload
: Applies new configuration without stopping the service (no downtime).
2. Using Legacy service
Command (SysVinit)
Older systems (or compatibility mode) may use service
.
Start/Stop/Restart
sudo service <service-name> start
sudo service <service-name> stop
sudo service <service-name> restart
# Example:
sudo service mysql restart
3. Using /etc/init.d
Scripts (Legacy Systems)
For very old systems or custom scripts:
sudo /etc/init.d/<service-name> start
sudo /etc/init.d/<service-name> stop
# Example:
sudo /etc/init.d/nginx restart
Common Services
Replace <service-name>
with:
nginx
/apache2
(web servers)mysql
/postgresql
(databases)ssh
/sshd
(SSH server)docker
(container service)ufw
(firewall)
Key Tips
- List All Services:
systemctl list-unit-files --type=service
- Check If
systemd
Is Used:
ps -p 1 -o comm= # Returns "systemd" if running
- View Logs:
journalctl -u <service-name> # systemd logs
Troubleshooting
- Permission Denied: Use
sudo
. - Service Not Found: Check the exact service name with
systemctl list-unit-files
. - Failed to Start: Check logs with
journalctl -u <service-name>
.
Example Workflow
- Restart Nginx after config changes:
sudo systemctl reload nginx # Or restart if reload isn't supported
- Disable MySQL auto-start:
sudo systemctl disable mysql
- Stop a service temporarily:
sudo systemctl stop apache2