How to Use systemctl Command in Linux: Syntax and Examples
Use the
systemctl command to control system services on Linux. It manages services by starting, stopping, enabling, or checking their status with simple commands like systemctl start service or systemctl status service.Syntax
The basic syntax of systemctl is:
systemctl [command] [service_name]commandis the action you want to perform, likestart,stop,restart,status, orenable.service_nameis the name of the service you want to manage, usually ending with.service.
bash
systemctl [command] [service_name]
Example
This example shows how to check the status of the ssh service, start it if it is stopped, and then enable it to start automatically on boot.
bash
systemctl status ssh.service sudo systemctl start ssh.service sudo systemctl enable ssh.service
Output
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-06-14 10:00:00 UTC; 5min ago
Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /lib/systemd/system/ssh.service.
Common Pitfalls
Common mistakes include:
- Forgetting to use
sudowhen runningsystemctlcommands that change service states. - Using incorrect service names or omitting the
.servicesuffix when required. - Trying to start a service that is masked or disabled without enabling it first.
Always check the service status before and after commands to confirm changes.
bash
sudo systemctl start apache2 # Wrong: missing sudo systemctl start apache2 # Right: with sudo sudo systemctl start apache2
Quick Reference
| Command | Description |
|---|---|
| systemctl start [service] | Start a service immediately |
| systemctl stop [service] | Stop a running service |
| systemctl restart [service] | Restart a service |
| systemctl status [service] | Show current status of a service |
| systemctl enable [service] | Enable service to start at boot |
| systemctl disable [service] | Disable service from starting at boot |
| systemctl reload [service] | Reload service configuration without restarting |
| systemctl is-active [service] | Check if service is active |
| systemctl list-units --type=service | List all active services |
Key Takeaways
Use
systemctl with commands like start, stop, status, and enable to manage Linux services.Always run
systemctl commands with sudo when changing service states.Check service status before and after to confirm your commands worked.
Use the exact service name, usually ending with
.service.Use
systemctl list-units --type=service to see all active services.