systemctl command is your primary tool for interacting with systemd, letting you start, stop, enable, disable, and inspect any service on your system.
Basic Service Operations
Use these commands to control whether a service is running at any given moment. Changes made withstart, stop, and restart are immediate but not persistent across reboots.
reload command sends a signal to the service to re-read its configuration file without fully restarting the process. Not all services support reload — check the service’s documentation if you’re unsure.
Enable and Disable at Boot
Enabling a service tells systemd to start it automatically on boot. This is separate from starting it right now.--now flag combines enabling and starting in a single command, which is the most convenient way to bring a new service online and ensure it survives a reboot.
Listing Services
Inspect what services are present on your system and which ones are currently active.list-units shows loaded units and their current state. list-unit-files shows all installed unit files regardless of whether they are currently loaded, which is useful for seeing disabled services.
Viewing Service Logs
systemd routes all service output to the journal, which you query withjournalctl.
Combine flags for more precise queries. For example,
journalctl -u nginx -p err --since "today" shows only error-level messages from nginx since midnight.Creating a Custom Service Unit File
When you deploy an application that doesn’t come with a systemd unit file, you can write one yourself. Unit files live in/etc/systemd/system/ for system-level services.
Write the unit file
Create
/etc/systemd/system/myapp.service with the following content:After=network.targetensures the network is up before your app starts.Restart=on-failuretells systemd to restart the process if it exits with a non-zero code.StandardOutput=journalsends all stdout/stderr output to the systemd journal.
Reload and enable the service
After writing the file, tell systemd to pick up the new unit and then enable and start it:Run
daemon-reload any time you create or modify a unit file — systemd does not detect changes automatically.System Targets (Runlevels)
systemd uses targets as the modern equivalent of SysV runlevels. The default target determines which services start at boot.| Target | Equivalent Runlevel | Description |
|---|---|---|
poweroff.target | 0 | Shut down the system |
rescue.target | 1 | Single-user rescue mode |
multi-user.target | 3 | Multi-user, no GUI |
graphical.target | 5 | Multi-user with GUI |
reboot.target | 6 | Reboot |