If you're managing Linux servers, encountering systemctl is inevitable. This utility has become the de facto standard for controlling system processes across most modern distributions. Let's explore how to leverage this tool effectively and avoid common pitfalls when working with system services.
When you power on a Linux machine, the first process to launch carries process ID 1—that's systemd
. It orchestrates the entire system: from initializing core services to managing network connections and mount points.
Unlike its predecessors (such as SysV Init
), systemd employs parallel execution for compatible components. Imagine: instead of waiting for each service to start sequentially, the system launches multiple independent processes simultaneously. The result—your server becomes operational significantly faster.
Systemctl
serves as your control panel for systemd
. All commands flow through it: from simple "start the web server" to complex operations involving dependencies and resource constraints.
The first skill to master—checking what's happening with a service right now:
$ systemctl status apache2
The command reveals:
Need just a quick "running/not running" check?
$ systemctl is-active apache2
You'll get a concise response: active or inactive.
Launch a service immediately:
$ sudo systemctl start apache2
Halt a running process:
$ sudo systemctl stop apache2
Complete restart (terminating all connections):
$ sudo systemctl restart apache2
Graceful configuration reload without dropping connections:
$ sudo systemctl reload apache2
Critical Point: These actions are temporary. After server reboot, everything reverts to autostart configurations.
To launch a service alongside the system:
$ sudo systemctl enable apache2
Systemd
creates necessary links in startup directories.
Remove from autostart:
$ sudo systemctl disable apache2
Check current autostart status:
$ systemctl is-enabled apache2
Pro Tip: Combined command enables autostart and launches the service immediately:
$ sudo systemctl enable --now apache2
Systemd operates with the concept of "units"—an abstraction for any managed object. Services represent just one type of unit.
All currently active units:
$ systemctl list-units
Absolutely all units, including disabled ones:
$ systemctl list-units --all
Services only:
$ systemctl list-units --type=service
Display problematic units:
$ systemctl list-units --state=failed
Complete list of unit files in the system:
$ systemctl list-unit-files
Contents of a specific unit:
$ systemctl cat apache2.service
Discover the physical file location:
$ systemctl show -p FragmentPath apache2.service
Systemctl provides access to extensive information about each unit.
All service parameters:
$ systemctl show apache2
The output will be lengthy. Filter for specific parameters:
$ systemctl show apache2 -p Requires
What's required to launch this service:
$ systemctl list-dependencies apache2
Reverse relationship—which services depend on this one:
$ systemctl list-dependencies --reverse apache2
Sometimes you need to absolutely prevent a service from starting—even if someone attempts manual activation.
Complete blockade:
$ sudo systemctl mask apache2
Systemd creates a symbolic link to /dev/null
, rendering the unit inaccessible.
Removing the block:
$ sudo systemctl unmask apache2
When This Matters: During migration to a different web server (say, from Apache to Nginx), you can mask the old service to eliminate accidental launches.
Utilize the built-in mechanism:
$ sudo systemctl edit apache2
An editor opens for creating a drop-in file. Your settings persist in /etc/systemd/system/apache2.service.d/override.conf
and survive package updates.
Editing the complete unit file:
$ sudo systemctl edit --full apache2
After any modifications, always run:
$ sudo systemctl daemon-reload
System files (installed by packages):
/lib/systemd/system/
— don't touch these directly.
Custom settings (highest priority):
/etc/systemd/system/
— your changes belong here.
Temporary units:
/run/systemd/system/
A target defines the set of active services for a specific operational mode:
poweroff.target
— Shutdownrescue.target
— Recovery mode (single-user)multi-user.target
— Console multi-user modegraphical.target
— Graphical interfacereboot.target
— RebootWhich target is active by default:
$ systemctl get-default
Set a different default target:
$ sudo systemctl set-default multi-user.target
Switch immediately:
$ sudo systemctl isolate graphical.target
Shutdown:
$ sudo systemctl poweroff
Restart:
$ sudo systemctl reboot
Sleep mode (RAM):
$ sudo systemctl suspend
Hibernate (disk):
$ sudo systemctl hibernate
Combined mode:
$ sudo systemctl hybrid-sleep
Systemd maintains detailed startup statistics.
Overall boot duration:
$ systemd-analyze
Which services slow down boot:
$ systemd-analyze blame
Displays a sorted list with initialization time for each service.
Boot critical path:
$ systemd-analyze critical-chain
Visualizes the dependency chain that determined total startup time.
Optimization Practice: Identify services with long initialization times. Verify whether unnecessary ones can be disabled or configured for delayed start via socket activation.
Systemd centrally collects logs from all components. Access via journalctl
:
Entire journal:
$ journalctl
Logs for a specific service:
$ journalctl -u apache2
Real-time mode (tail -f
equivalent):
$ journalctl -u apache2 -f
Last 100 entries:
$ journalctl -u apache2 -n 100
Since current boot:
$ journalctl -b
For a defined period:
$ journalctl --since "2025-01-15" --until "2025-01-20"
$ journalctl --since "30 minutes ago"
Errors only:
$ journalctl -p err
Delete older than 2 weeks:
$ sudo journalctl --vacuum-time=14d
Keep maximum 1 GB:
$ sudo journalctl --vacuum-size=1G
Create /etc/systemd/system/myservice.service
:
[Unit]
Description=My Custom Application Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=appuser
Group=appuser
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/start
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5s
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Section Breakdown:
[Unit] — General information
[Service] — Execution parameters
[Install] — System integration
ExecStart
is the main one.sd_notify
.$ sudo systemctl daemon-reload
$ sudo systemctl enable myservice
$ sudo systemctl start myservice
$ systemctl status myservice
Examine details:
$ systemctl status myservice
Dive into logs:
$ journalctl -u myservice -n 200 --no-pager
Verify unit file syntax:
$ systemd-analyze verify /etc/systemd/system/myservice.service
Dependency tree:
$ systemctl list-dependencies myservice --all
Search for circular dependencies:
$ systemctl show -p After,Before,Requires,Wants myservice
Verify whether the service user can execute the binary:
$ sudo -u appuser /opt/myapp/bin/start
CPU time constraint:
$ sudo systemctl set-property myservice CPUQuota=30%
Memory limit:
$ sudo systemctl set-property myservice MemoryMax=512M
Resource consumption monitoring:
$ systemd-cgtop
Service starts only upon first socket access. Create /etc/systemd/system/myapp.socket
:
[Unit]
Description=My App Socket
[Socket]
ListenStream=9000
Accept=no
[Install]
WantedBy=sockets.target
The service remains inactive until a connection arrives on port 9000.
Create /etc/systemd/system/backup.timer
:
[Unit]
Description=Daily Backup Timer
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
Unit=backup.service
[Install]
WantedBy=timers.target
Activation:
$ sudo systemctl enable backup.timer
$ sudo systemctl start backup.timer
View all timers:
$ systemctl list-timers --all
/lib/systemd/system
— use systemctl
edit.systemctl
status before production deployment.User/Group
settings.Systemctl represents the Swiss Army knife of Linux administration. Its capabilities extend far beyond simple "start/stop" operations. Proficient systemd usage enables building reliable, fast-booting systems with predictable service behavior.
The transition to systemd once sparked controversy in the community, but today it's an industry standard. Parallel booting, intelligent dependency management, integrated logging, and resource control—all of this makes systemd indispensable for modern infrastructure.
Invest time in mastering systemctl—it pays dividends manifold during the first serious incident or optimization task you encounter.