Common Commands
List all service units that are configured to start automatically at boot.
systemctl list-unit-files --type=service --state=enabledList all currently running services.
systemctl list-units --type=service --state=runningCheck whether a service is enabled at boot and whether it is currently running.
systemctl is-enabled nginx.servicesystemctl is-active nginx.serviceStart, stop, restart, enable, and check the status of a service.
systemctl start nginxsystemctl stop nginxsystemctl restart nginxsystemctl enable nginxsystemctl status nginxCreate a Custom Spring Boot Service
cat <<'EOF' >/etc/systemd/system/hello.service[Unit]Description=Spring Boot HelloWorldAfter=syslog.targetAfter=network.target
[Service]User=usernameType=simpleExecStart=/usr/bin/java -jar /root/hello.jarRestart=alwaysStandardOutput=syslogStandardError=syslogSyslogIdentifier=helloworld
[Install]WantedBy=multi-user.targetEOFDefault Locations of systemd Unit Files
(See man systemd.unit for more details.)
Table 1. Load path when running in system mode (--system).
┌────────────────────┬─────────────────────────────┐│ Path │ Description │├────────────────────┼─────────────────────────────┤│ /etc/systemd/system│ Local configuration │├────────────────────┼─────────────────────────────┤│ /run/systemd/system│ Runtime units │├────────────────────┼─────────────────────────────┤│ /lib/systemd/system│ Units of installed packages │└────────────────────┴─────────────────────────────┘NOTE
/etc/systemd/systemcontains locally created or customized unit files./lib/systemd/system(or/usr/lib/systemd/systemon many modern distributions) contains unit files installed by software packages./run/systemd/systemis used for runtime-generated or temporary unit files.
Reload systemd After Modifying a Unit
systemctl daemon-reloadWhenever you create or modify a unit file, you should reload the systemd manager.
Reloading the configuration does not restart the service automatically, so you will usually need to run:
systemctl restart hello.serviceVerify the Syntax of a Unit File
systemd-analyze verify /etc/systemd/system/hello.serviceList Failed Units
systemctl --failedList only failed services.
systemctl --failed --type=serviceView Service Logs
journalctl -u hello.servicejournalctl -u hello.service -bjournalctl -u hello.service -fjournalctl -u hello.service --since "30 minutes ago"Clear the Failed State
If a service still shows as failed after fixing the issue:
systemctl reset-failed hello.serviceThen start the service again:
systemctl start hello.serviceUse Drop-in Overrides Instead of Editing Package Files
Avoid modifying package-managed unit files directly, such as:
/lib/systemd/system/nginx.serviceYour changes may be overwritten during package upgrades.
Instead, create a drop-in override:
systemctl edit nginx.serviceExample:
[Service]Restart=on-failureRestartSec=5sView the merged configuration:
systemctl cat nginx.serviceRemove all local overrides and restore the original configuration:
systemctl revert nginx.serviceFind Which Package Installed a Unit File
dpkg-query -S /lib/systemd/system/* | sort -uExample output:
dpkg-query: no path found matching pattern /lib/systemd/system/ecs_mq.servicedpkg-query: no path found matching pattern /lib/systemd/system/SplunkForwarder.servicedpkg-query: no path found matching pattern /lib/systemd/system/system-systemd\x2dcryptsetup.sliceaccountsservice: /lib/systemd/system/accounts-daemon.serviceapparmor: /lib/systemd/system/apparmor.serviceapt: /lib/systemd/system/apt-daily.serviceapt: /lib/systemd/system/apt-daily.timerapt: /lib/systemd/system/apt-daily-upgrade.serviceapt: /lib/systemd/system/apt-daily-upgrade.timerat: /lib/systemd/system/atd.serviceauditd: /lib/systemd/system/auditd.servicebase-files: /lib/systemd/system/motd-news.servicebase-files: /lib/systemd/system/motd-news.timerNOTEIf
dpkg-querycannot find a matching package, it simply means that no package recorded ownership of that path in the dpkg database. The unit file may have been created manually, installed by a third-party installer, generated by a package installation script, or generated dynamically by a systemd generator. Installing a package manually withdpkg -idoes not normally preventdpkg-query -Sfrom identifying files that belong to that package.