Introduction / Why This Is Needed
systemctl is the primary utility for managing systemd, the init system and service manager used in the vast majority of modern Linux distributions (Ubuntu, CentOS, Debian, Fedora, etc.). With it, you can control the lifecycle of background processes (services), such as web servers, databases, network daemons, and many other system components.
By the end of this guide, you will confidently manage services: start, stop, configure their autostart, reload configurations, and diagnose issues. These skills are an essential foundation for any system administrator and developer working with servers.
Prerequisites / Preparation
- Access to a Linux terminal with
systemdinstalled. You can verify its presence with:
The command should returnps -p 1 -o comm=systemd. - Superuser privileges (root or sudo) to manage most system services. For managing user services, root privileges may not be required.
- Basic understanding of the command line and service naming conventions (they usually have a
.serviceextension, e.g.,nginx.serviceorsshd.service).
Step 1: Checking Service Status
Before taking any action, it's useful to know the current state of a service.
sudo systemctl status <service_name>.service
- Example:
sudo systemctl status nginx.service - What it does: Displays detailed information: whether the unit is active, if it started successfully, the process PID, last start time, and the latest log entries for that service.
- Short forms: For a simple status output (active/inactive), use
systemctl is-active <service_name>orsystemctl is-enabled <service_name>(shows if autostart is enabled).
Step 2: Basic Management (Start, Stop)
These commands manage the service in the current session, without persisting the state after a reboot.
# Start the service immediately
sudo systemctl start <service_name>.service
# Stop the service
sudo systemctl stop <service_name>.service
- Example:
sudo systemctl start apache2.service - Important: If the service is already running, the
startcommand does nothing. If it's stopped,stopalso has no effect. For a forceful restart, see the next step.
Step 3: Configuring Autostart (enable/disable)
To have a service start automatically on every system boot, you must "enable" it.
# Enable the service to start at boot
sudo systemctl enable <service_name>.service
# Disable the service from starting at boot
sudo systemctl disable <service_name>.service
- What happens:
enablecreates symbolic links in the/etc/systemd/system/(or/lib/systemd/system/) directory, instructing systemd to start this service when reaching a specific target level, typicallymulti-user.targetorgraphical.target. - Useful command:
systemctl is-enabled <service_name>.servicewill show if autostart is configured (enabled/disabled/static/masked).
Step 4: Restarting and Reloading Configuration
Often, after modifying a service's configuration file (e.g., /etc/nginx/nginx.conf), you need to apply changes without a full stop.
# Full restart (stop and start again)
sudo systemctl restart <service_name>.service
# Reload configuration (without stopping, if the service supports it)
sudo systemctl reload <service_name>.service
- Critical difference:
restartguarantees the service starts fresh with a clean environment but causes a brief downtime.reloadis a more "graceful" operation, but not all services support it. Ifreloadis unavailable, systemd will report it. - Alternative:
sudo systemctl try-reload-or-restart <service_name>.servicewill attemptreload, and if that fails, it will performrestart.
Step 5: Viewing Service Logs
Diagnostics start with logs. journalctl is the tool for working with the systemd journal.
# Show all logs for a specific service
sudo journalctl -u <service_name>.service
# Show logs from the current system boot (after the last reboot)
sudo journalctl -u <service_name>.service -b
# Follow logs in real-time (like tail -f)
sudo journalctl -u <service_name>.service -f
- Filters: Add
--since "2024-01-01 10:00:00"to filter by time or-p errto show only error messages. - Helpful: The combination of
systemctl statusandjournalctlallows you to quickly understand why a service fails to start or behaves unstably.
Step 6: Additional Useful Operations
Isolating a Target (System Reboot to a Runlevel)
Switching systemd to a different target level, similar to changing runlevels in SysVinit.
# Switch to multi-user mode without a graphical interface (like runlevel 3)
sudo systemctl isolate multi-user.target
# Switch back to graphical mode (like runlevel 5)
sudo systemctl isolate graphical.target
Masking/Unmasking a Service
An extreme measure that prevents any start of the service, even manually or as a dependency.
# Completely block the service (creates a symlink to /dev/null)
sudo systemctl mask <service_name>.service
# Unblock the service
sudo systemctl unmask <service_name>.service
- Caution: Use
maskcarefully. You can mask a critical service (likesshd), after which console access may be required to unmask it.
Reloading the Daemon (Daemon-Reload)
After modifying a unit file (the .service file itself, e.g., in /etc/systemd/system/), you need to inform systemd of the changes.
sudo systemctl daemon-reload
- When: Always after manually editing or creating a new service file, but before restarting it (
restart).
Verifying the Result
- Check the status:
sudo systemctl status <service_name>.service. Ensure theActive:line showsactive (running)for running services orinactive (dead)for stopped ones. - Check autostart:
systemctl is-enabled <service_name>.serviceshould returnenabled. - Test functionality: For a web server, make an HTTP request (
curl http://localhost); for SSH, try to connect. This is the most reliable way to confirm the service is working correctly, not just "started".
Common Issues
| Problem / Symptom | Likely Cause | Solution |
|---|---|---|
Failed to start <service>.service | Error in the service's configuration file or dependencies. | 1. Run sudo systemctl status <service>.service and look for the Result: line. 2. Check logs: sudo journalctl -u <service>.service -b -p err. 3. Validate configuration syntax (if a -t flag exists, e.g., nginx -t). |
Unit <service>.service not found. | Service not installed, name misspelled, or unit file missing. | 1. Verify the name is correct. 2. Search for the file: sudo find / -name "*<part_of_name>*.service" 2>/dev/null. 3. If the service isn't installed—install the corresponding package (e.g., sudo apt install nginx). |
Permission denied on start/stop | Running without superuser privileges (sudo). | All system service management operations require sudo. Use sudo systemctl ... or log in as root. |
| Service starts but doesn't work (port not listening, process unresponsive) | Problem within the service itself (code error, lack of resources, port conflict). | 1. Check if the process is running: ps aux | grep <process_name>. 2. Check if the port is listening: sudo ss -tulpn | grep :<port>. 3. Examine the application's own logs (not just journald) in /var/log/ or as specified in its config. |
masked service won't start, even after enable | The service was blocked (mask). | Unmask it: sudo systemctl unmask <service>.service, then sudo systemctl enable --now <service>.service. |