Checking Service Status In Detail
The systemctl status command provides detailed information about a service, including it’s current state (active, inactive, failed), recent log entries, and reasons for failure if the service is not running.
Example Command:
sudo systemctl status apache2
#or httpd service
sudo systemctl status httpd
Output:
iamyaash@pi5:~ $ sudo systemctl status firewalld.service
β firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/lib/systemd/system/firewalld.service; enabled; preset: enabled)
Active: active (running) since Sun 2025-06-29 20:30:48 IST; 1s ago
Docs: man:firewalld(1)
Main PID: 7956 (firewalld)
Tasks: 2 (limit: 9585)
CPU: 279ms
CGroup: /system.slice/firewalld.service
ββ7956 /usr/bin/python3 /usr/sbin/firewalld --nofork --nopid
Jun 29 20:30:48 pi5 systemd[1]: Starting firewalld.service - firewalld - dynamic firewall daemon...
Jun 29 20:30:48 pi5 systemd[1]: Started firewalld.service - firewalld - dynamic firewall daemon.
The output includes:
- Loaded: Whether the service is loaded and
enabled/disabled. - Active: Current status(active, inactive, failed).
- Main PID: The main process ID.
- Tasks, Memory, CPU: Resource usage (if available).
- Logs: Recent log entries for troubleshooting.
Starting, Stopping, Restarting, And Reloading Services
This is relatively simple, and it’s the bare minimum requirement to use systemctl to manage systemd daemon.
We can control the operation of a service using start, stop, restart, and reload.
sudo systemctl start firewalld # start the service
sudo systemctl stop firewalld # stop the service
sudo systemctl restart firewalld # stop and start the service
sudo systemctl reload firewalld # reload configuration without stopping
Use
reloadfor configuration changes that do not require a full restart.
Enabling And Disabling Services At Boot
The term enable and disable in systemd services indicates, that a service can start automatically at boot(enable), or to prevent automatic startup(disable).
sudo systemctl enable firewalld # get started at boot
sudo systemctl disable firewalld # won't start at boot
Masking And Unmasking Services
Masking a service prevents it from being start, even manually. Unmasking removes this restriction.
sudo systemctl mask firewalld # prevent all starts
sudo systemctl unmask firewalld # allow starts again
Masking creates a symbolic link in
/etc/systemd/systemthat overrides the service file.
Understanding And Working With systemd Targets
systemd uses “targets” to group services and define the system state (eg: multi-user, graphical, rescue).
Purpose:They act as synchronization points during boot, shutdown, and state changes, allowing systemd to manage dependencies and ensure services start in the correct order.
File Format: Target units ends with .target and are defined in /usr/lib/systemd/system or /etc/systemd/system.
You can switch between targets using systemctl isolate.
Common targets:
multi-user.target: Text-based multi-user mode.graphical.target: Graphical user interface mode.rescue.target: Single-user rescue mode.poweroff.target: For shutting down or rebooting the system.
For compatibility,
systemdprovidesrunlevel*.targetunits that map to traditional SysV runlevels.
Advanced Usage and Features
- Creating Custom Targets: You can define your own target files to group custom services or define new system states.
- Dependencies: Targets can depend on other targets or units using
Wants=,Requires=,Before=, andAfter=directives in unit files. - Multiple Active Targets: Unlike traditional runlevels, multiple targets can be active at the same time.
- Viewing Active Targets: Use
systemctl list-units --type=targetto see all active and loaded targets. - Changing the Default Target: Use
systemctl set-default <target>to change the default boot target. - Isolating a Target: Use
systemctl isolate <target>to switch to a different target at runtime (eg: from graphical to rescue mode) - Nested Targets: Some targets inherit all services from another target and add more (eg:
graphical.targetincludes all frommulti-user.targetplus a display manager).
Essential Commands
- List all targets:
systemctl list-unit --type=target
- Get current default target
systemctl get-default
- Set default target
sudo systemctl set-default multi-user.target
- Isolate a target (switch state)
sudo systemctl isolate rescue.target
- View dependencies of a target
systemctl show -p Wants --value multi-user.target
systemctl show -p Requires --value multi-user.target
You can easily create a custom target file by duplicating the existing target and edit dependencies as needed.
