Systemd service commands let you start, stop, restart, enable, and inspect background services on Linux distributions that use systemd (most modern distros like Ubuntu, Debian, Fedora, and Arch). The primary tool is systemctl, and this article explains the essential commands, how to read their output, and how to troubleshoot common issues.
What Is Systemd and a Service Unit?
Systemd is the init system and service manager for most Linux distributions. It is the first process (PID 1) that the kernel starts, and it brings up the rest of the system. A service unit is a configuration file that tells systemd how to manage a specific daemon or process. These files live in:
/etc/systemd/system/– administrator-created or modified units/lib/systemd/system/or/usr/lib/systemd/system/– package-installed units/run/systemd/system/– runtime-generated units
Service files end in .service and contain sections like [Unit], [Service], and [Install]. For example, the SSH daemon is managed by ssh.service (or sshd.service depending on distro).
Essential systemctl Commands
All service management goes through systemctl. The most common operations are:
systemctl start– start a service nowsystemctl stop– stop a service nowsystemctl restart– stop and start againsystemctl reload– ask the service to reload its config (if supported)systemctl enable– start the service automatically at bootsystemctl disable– remove the auto-start symlinksystemctl status– show current state, recent logs, and process infosystemctl is-active– printactive,inactive, orfailedsystemctl is-enabled– printenabled,disabled, orstatic
Most commands accept a .service suffix, but it is optional. systemctl start nginx and systemctl start nginx.service are the same.
Checking Status and Logs
systemctl status is the first command to run when something is wrong. It shows:
- Loaded state (whether the unit file is valid)
- Active state (running, exited, failed, etc.)
- Main process ID and memory usage
- The last few log lines from the journal
For full logs, use journalctl:
journalctl -u– all logs for that servicejournalctl -u– follow new log lines-f journalctl -u– recent entries--since "1 hour ago" journalctl -p err -u– only errors
If a service fails, the status output usually includes the exit code and the last log lines. Combined with journalctl, that is enough to diagnose most problems.
Creating and Modifying Service Files
To create a custom service, write a unit file in /etc/systemd/system/. A minimal example:
[Unit] Description=My custom daemon After=network.target [Service] ExecStart=/usr/local/bin/my-daemon Restart=on-failure User=myuser [Install] WantedBy=multi-user.target
After creating or editing a file, run systemctl daemon-reload to make systemd re-read the files. Then you can start and enable it. If you are modifying a package-provided unit, do not edit the original in /lib/systemd/system; instead create an override directory:
systemctl edit
This opens a drop-in file in /etc/systemd/system/. Changes there take precedence over the original without breaking package updates.
Enabling vs. Starting: What’s the Difference?
A common misunderstanding is that enable starts a service. It does not. enable creates symlinks so the service starts when the system boots into the target that wants it. start launches it immediately. You usually want both:
systemctl enable --now
This enables and starts in one command. Conversely, systemctl disable --now stops and disables it.
Some services are static – they cannot be enabled because they are only started by other units. Others are masked – they are completely disabled, even manually. To unmask, use systemctl unmask .
Common Problems and Troubleshooting
When a service fails, follow these steps:
- Run
systemctl status– read the error line. - Check
journalctl -ufor the actual error message.-n 50 - Verify the executable path in
ExecStartexists and is executable. - Check permissions – if the service runs as a specific user, that user must have access to all files and directories.
- If you changed a unit file, run
systemctl daemon-reloadand thensystemctl restart.
Common error messages include:
Failed to start: Unit not found– the service name is wrong or the unit file is missing.Failed to start: Access denied– usually a permissions problem on the executable or a directory.Failed with result 'exit-code'– the process ran and exited with a non-zero code. The log will show the code.Failed with result 'timeout'– the service took too long to start or stop. IncreaseTimeoutStartSecorTimeoutStopSecin the unit file.
Systemd vs. SysVinit and Other Tools
Older Linux systems used SysVinit, where services were managed with service and chkconfig. Systemd replaced that with systemctl and adds features like parallel startup, socket activation, and dependency tracking. Other init systems include OpenRC (Gentoo) and runit (some minimal distros). If you are on a systemd distro, service and chkconfig are often still available as compatibility wrappers that translate to systemctl, but you should use systemctl directly for full control.
Frequently Asked Questions
How do I list all running services?
Use systemctl list-units --type=service --state=running. This shows only active services. To see all installed service units, use systemctl list-unit-files --type=service.
What does “enabled” vs “static” mean in systemctl status?
Enabled means the service has a symlink in a .wants directory, so it starts at boot. Static means the unit file has no [Install] section, so it cannot be enabled – it is only started by other units or manually. It is not a problem; many system services are static.
Why does my service start but then exit immediately?
Usually the program itself exits because it is not a daemon, it failed to bind to a port, or it needs a config file that is missing. Check the logs with journalctl -u . If the program is supposed to run in the foreground, ensure ExecStart points to it directly, not through a shell script that backgrounds it.
How do I make a service restart automatically on failure?
Add Restart=on-failure to the [Service] section. You can also set RestartSec=5 to wait 5 seconds before retrying. Then run systemctl daemon-reload and restart the service.
What is the difference between systemctl restart and systemctl reload?
Restart stops the process and starts it again, which is heavier but always works. Reload sends a signal (usually SIGHUP) asking the process to re-read its configuration without stopping. Reload is faster and avoids downtime, but the service must support it. If it does not, reload will fail.
Can I run a service as a specific user?
Yes. In the [Service] section, set User=username and optionally Group=groupname. The service will run with those permissions. Make sure the user has access to all files and directories the service needs.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Canon Printer Not Responding? Fixes That Work
- 50MP Selfie Phones in India: Worth Comparing
- API Performance Monitoring: Why It Matters & What to Track
- Affordable Desktop Peripherals: Smart Picks
- Techno Fascism: Contested Term in Tech & Politics
- Upgrade Firmware Cisco Devices: Steps & Risks