How to Enable a Service on Linux Using systemctl
To enable a service on Linux so it starts automatically at boot, use the
sudo systemctl enable service-name command. Replace service-name with the actual service you want to enable.Syntax
The basic syntax to enable a service on Linux is:
systemctl: The command to control system services.enable: The action to set the service to start at boot.service-name: The name of the service you want to enable.
bash
sudo systemctl enable service-name
Example
This example shows how to enable the nginx web server service to start automatically when the system boots.
bash
sudo systemctl enable nginx
Output
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
Common Pitfalls
Common mistakes when enabling services include:
- Using the wrong service name. Check the exact service name with
systemctl list-unit-files --type=service. - Not running the command with
sudo, which is required for permission. - Confusing
enablewithstart.enablesets the service to start at boot, but does not start it immediately.
bash
sudo systemctl start nginx # Starts service immediately but does not enable at boot sudo systemctl enable nginx # Enables service at boot but does not start it immediately
Quick Reference
| Command | Description |
|---|---|
| sudo systemctl enable service-name | Enable service to start at boot |
| sudo systemctl disable service-name | Disable service from starting at boot |
| sudo systemctl start service-name | Start service immediately |
| sudo systemctl stop service-name | Stop service immediately |
| sudo systemctl status service-name | Check service status |
Key Takeaways
Use
sudo systemctl enable service-name to make a service start automatically at boot.Always run
systemctl commands with sudo for proper permissions.Check the exact service name before enabling to avoid errors.
enable sets the service to start at boot but does not start it immediately.Use
systemctl start service-name to start a service right away.