Managing services with systemctl on your VPS
Your VPS runs programs that always need to be there: the SSH server, a web server, a database. These programs are called services. On Debian, Ubuntu, AlmaLinux and Rocky Linux, systemd starts and monitors them, and you control systemd with the systemctl command. These are the commands you'll need most often.
The status of a service
systemctl status nginx
You can leave out the .service extension. Read the output from top to bottom:
- The dot in front of the name is green if the service is running, white if it has stopped and red if it has failed.
- Loaded tells you whether the service starts at boot (
enabled) or not (disabled). - Active tells you whether it is running right now (
active (running)), has stopped (inactive (dead)) or has failed (failed), and since when. - At the bottom you'll find the last lines from the service's log. Often that already tells you why something is going wrong.
Starting, stopping and reloading
| Command | What it does |
|---|---|
sudo systemctl start nginx | Start now |
sudo systemctl stop nginx | Stop now |
sudo systemctl restart nginx | Stop and start again |
sudo systemctl reload nginx | Reread the configuration without stopping |
The difference between restart and reload matters. With a restart, the service stops briefly, and if there's an error in the new configuration, it won't start again and your website is down. With a reload, the service keeps running; if there's an error, nginx keeps the old configuration. So always test the configuration first (sudo nginx -t, sudo sshd -t) and then use reload. Not every service supports reload; sudo systemctl reload-or-restart picks the right one for you.
At boot: enable and disable
Starting a service and "starting it at boot" are two different things. If you only start a service with start, it will be gone again after the next reboot, for example after a kernel update.
| Command | What it does |
|---|---|
sudo systemctl enable --now nginx | Start now and from now on at boot. Use this for a new service |
sudo systemctl disable --now nginx | Stop now and no longer start at boot |
systemctl is-enabled nginx | Shows whether the service starts at boot |
What's wrong? And what's running?
systemctl --failed
This shows all failed services. The list should be empty. Check it after every reboot and every upgrade. Once you've fixed the problem, clear the message with sudo systemctl reset-failed.
systemctl list-units --type=service --state=runningshows all running services. If you don't recognise one, find out what it does. A service you don't need is an unnecessary way in.systemctl list-timersshows systemd's scheduled tasks, with when they last ran and when they'll run next. This lets you see, for example, whether the timers for automatic updates or certificate renewal are running.
The logs of a service
Want to see more than the few lines in systemctl status? Use journalctl:
sudo journalctl -u nginx --since "1 hour ago" sudo journalctl -u nginx -p err -b
The first shows everything from nginx in the past hour, the second only the errors since the last reboot. You'll find more commands in Reading logs with journalctl and docker logs.
Changing the settings of a service
Never edit a package's unit file (in /usr/lib/systemd/system/), because an update will overwrite it. Use sudo systemctl edit nginx instead: it creates a small file containing only your changes and makes systemd reread it. You can see what ultimately applies with systemctl cat nginx.
sudo systemctl reboot, or with Restart in the VPS screen. Force off is like pulling the plug: services don't get a chance to stop cleanly and a database can get corrupted. See The VPS screen: on and off, graphs, system and log.On AlmaLinux and Rocky Linux, systemctl works exactly the same way, but some services have different names: the SSH service is called sshd instead of ssh, Apache is called httpd and the firewall is firewalld.
Want to learn more? Take the free course VPS and Linux server management, lesson 6.1 Managing services with systemd or lesson 6.2 Reading logs with journalctl.
