Linux systemctl
Command – Control Systemd Services
The systemctl
command is an essential tool for managing systemd services and other system components in modern Linux distributions. It is used to start, stop, restart, enable, disable, and monitor system services, making it a fundamental tool for system administrators.
Syntax of systemctl
systemctl [OPTIONS] COMMAND [SERVICE]
OPTIONS
→ Flags that modify the behavior ofsystemctl
.COMMAND
→ The action to be performed (e.g., start, stop, status).SERVICE
→ The name of the service you want to control (e.g.,nginx
,apache2
).
Common systemctl
Commands
1. Start a Service
To start a service immediately:
sudo systemctl start service_name
Example:
sudo systemctl start nginx
This will start the nginx web server.
2. Stop a Service
To stop a running service:
sudo systemctl stop service_name
Example:
sudo systemctl stop nginx
This will stop the nginx service.
3. Restart a Service
To restart a service (stops and then starts the service again):
sudo systemctl restart service_name
Example:
sudo systemctl restart nginx
This will restart the nginx service.
4. Reload a Service
If a service supports reloading its configuration without stopping, use:
sudo systemctl reload service_name
Example:
sudo systemctl reload nginx
This reloads the nginx service's configuration.
5. Check the Status of a Service
To check the current status of a service (running, stopped, etc.):
sudo systemctl status service_name
Example:
sudo systemctl status nginx
This shows the status of the nginx service, including whether it is active, inactive, or failed.
6. Enable a Service
To enable a service to start automatically at boot time:
sudo systemctl enable service_name
Example:
sudo systemctl enable nginx
This will ensure that nginx starts automatically when the system boots.
7. Disable a Service
To disable a service from starting at boot time:
sudo systemctl disable service_name
Example:
sudo systemctl disable nginx
This prevents nginx from starting automatically at boot time.
8. Mask a Service
To prevent a service from being started manually or automatically:
sudo systemctl mask service_name
Example:
sudo systemctl mask nginx
This will create a symbolic link to /dev/null
and make it impossible to start the nginx service, even if it's enabled.
9. Unmask a Service
To remove the mask from a service and allow it to be started again:
sudo systemctl unmask service_name
Example:
sudo systemctl unmask nginx
This will allow nginx to be started again after being masked.
10. View All Services
To list all active and inactive services:
sudo systemctl list-units --type=service
This will display all services, including their current state (active, inactive, etc.).
11. View All Failed Services
To list all services that have failed:
sudo systemctl --failed
This will display any services that have failed to start or encountered an issue.
12. View Logs for a Service
To view logs for a specific service using journalctl:
sudo journalctl -u service_name
Example:
sudo journalctl -u nginx
This will show the logs for the nginx service.
Useful Options for systemctl
1. Show the Status of Systemd
To view general information about the systemd system and its status:
sudo systemctl status
2. Start a Service in the Background (--quiet
)
To suppress output when starting a service, use the --quiet
option:
sudo systemctl start nginx --quiet
3. View Service Dependency Tree (list-dependencies
)
To see the dependency tree for a specific service:
sudo systemctl list-dependencies service_name
Example:
sudo systemctl list-dependencies nginx
This shows the other services or units that the nginx service depends on.
4. Set Default Target (Runlevel) (set-default
)
To set the default target (similar to runlevels in older Linux systems):
sudo systemctl set-default target_name
Example:
sudo systemctl set-default graphical.target
This sets the default target to the graphical environment (GUI).
Examples
1. Start a Service
sudo systemctl start apache2
This starts the apache2 web server.
2. Enable a Service to Start on Boot
sudo systemctl enable apache2
This enables apache2 to start automatically at boot.
3. Check the Status of a Service
sudo systemctl status apache2
This shows the current status of the apache2 service.
4. Restart a Service
sudo systemctl restart apache2
This restarts the apache2 service.
5. View Logs of a Service
sudo journalctl -u apache2
This shows the logs for the apache2 service.
Conclusion
The systemctl
command is a powerful tool for managing systemd services in Linux. It allows you to control services, check their status, and enable or disable them from starting automatically at boot. Whether you're restarting a service, viewing logs, or managing the startup behavior, systemctl
is essential for system administrators.
Would you like additional details or SEO optimization? 🚀