funwithlinux guide

Essential Systemd Commands Every Admin Should Know

In the landscape of modern Linux systems, **systemd** has emerged as the de facto init system, replacing legacy systems like SysVinit and Upstart. As a system and service manager, systemd is responsible for bootstrapping the user space, managing daemons, handling power management, and much more. For system administrators, mastering systemd is non-negotiable—its commands and utilities simplify routine tasks like starting/stopping services, debugging failures, and configuring system behavior. This blog post compiles the most essential systemd commands every admin should know. Whether you’re troubleshooting a misbehaving service, optimizing boot times, or configuring system defaults, these commands will streamline your workflow and deepen your understanding of how Linux systems operate under the hood.

Table of Contents

1. Understanding systemd: A Quick Primer

Before diving into commands, let’s clarify what systemd is. At its core, systemd is a system manager that initializes the Linux kernel and user space during boot. It uses units (configuration files) to manage resources like services, sockets, devices, and mounts. The most common unit type is a service unit (.service), which defines how a daemon (e.g., nginx, sshd) should start, stop, or reload.

Systemd’s key advantages include parallelized service startup (faster boot times), centralized logging via journald, and robust dependency management between services.

2. Basic Service Management

Services are the workhorses of Linux systems. Controlling them is foundational for admins. Here are the critical commands to manage services with systemctl (systemd’s primary command-line tool).

2.1 Starting, Stopping, and Restarting Services

Use systemctl [action] [service] to control a service’s state:

Command SyntaxDescription
systemctl start <service>Starts a service immediately (does not persist across reboots).
systemctl stop <service>Stops a running service immediately.
systemctl restart <service>Stops and restarts a service (use for major config changes).
systemctl reload <service>Reloads a service’s configuration without stopping it (for minor changes).
systemctl try-restart <service>Restarts only if the service is running (avoids errors if stopped).

Examples:

# Start the Nginx web server  
sudo systemctl start nginx  

# Reload Nginx after editing /etc/nginx/nginx.conf  
sudo systemctl reload nginx  

# Restart SSH service (sshd)  
sudo systemctl restart sshd  

2.2 Enabling and Disabling Services

To ensure a service starts automatically at boot (or prevent it from doing so), use:

Command SyntaxDescription
systemctl enable <service>Enables a service to start at boot (persistent).
systemctl disable <service>Disables a service from starting at boot (persistent).
systemctl is-enabled <service>Checks if a service is enabled (returns enabled, disabled, or masked).
systemctl enable --now <service>Enables AND starts a service in one step.

Examples:

# Enable Nginx to start at boot  
sudo systemctl enable nginx  

# Disable SSH from starting at boot (but leave it running)  
sudo systemctl disable sshd  

# Check if Docker is enabled  
systemctl is-enabled docker  
# Output: enabled  

3. Checking Service Status

To diagnose issues, you’ll often need to check a service’s current state. Use systemctl status <service> for a detailed report:

sudo systemctl status nginx  

Sample Output:

● nginx.service - A high performance web server and a reverse proxy server  
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)  
     Active: active (running) since Wed 2024-05-20 14:30:00 UTC; 2h 15min ago  
       Docs: man:nginx(8)  
   Main PID: 1234 (nginx)  
      Tasks: 2 (limit: 4915)  
     Memory: 3.5M  
     CGroup: /system.slice/nginx.service  
             ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;  
             └─1235 nginx: worker process  

Key details:

  • Loaded: Path to the service file and whether it’s enabled.
  • Active: Current state (e.g., active (running), failed).
  • Main PID: Process ID of the service’s main process.
  • Recent logs: The last few lines of the service’s logs.

4. Viewing Logs with journalctl

Systemd centralizes logs via journald, a logging daemon. The journalctl command lets you query these logs, making it indispensable for debugging.

Basic Log Viewing

# View all logs (paged with less)  
journalctl  

# View logs for a specific service  
journalctl -u nginx  

# View logs since the system booted  
journalctl -b  

Advanced Log Filtering

OptionDescription
-f”Follow” real-time logs (like tail -f).
-n <number>Show the last N log entries (e.g., -n 50 for 50 lines).
-since <time>Filter logs since a time (e.g., "-since 2024-05-20 14:00:00" or "-since 1h").
-until <time>Filter logs until a time (e.g., "-until 2024-05-20 16:00:00").
-p <priority>Filter by log priority (e.g., err for errors, warn for warnings).
--no-pagerPrint logs directly (no pagination).

Examples:

# View real-time Nginx errors  
journalctl -u nginx -f -p err  

# View SSH logs from the last hour  
journalctl -u sshd --since "1 hour ago"  

# View critical system logs since boot  
journalctl -b -p crit  

5. Managing Targets (Runlevels)

Systemd replaces traditional SysVinit runlevels with targets—groups of units that define a system state (e.g., “graphical interface” or “multi-user command line”).

Common Targets

TargetPurposeEquivalent Runlevel
multi-user.targetMulti-user command-line interface (no GUI).Runlevel 3
graphical.targetMulti-user interface with GUI.Runlevel 5
rescue.targetSingle-user mode for troubleshooting.Runlevel 1
poweroff.targetShutdown the system.Runlevel 0

Key Target Commands

Command SyntaxDescription
systemctl get-defaultShow the default target (boot target).
systemctl set-default <target>Set the default target (persists across reboots).
systemctl isolate <target>Switch to a target immediately (e.g., from GUI to command line).

Examples:

# Check current default target  
systemctl get-default  
# Output: graphical.target  

# Set default target to multi-user (no GUI on boot)  
sudo systemctl set-default multi-user.target  

# Switch to rescue mode (single-user)  
sudo systemctl isolate rescue.target  

6. System Information and Control

Systemd provides utilities to query and modify system properties like hostname, time, and locale.

hostnamectl: Manage Hostname

# View current hostname  
hostnamectl  

# Set a new hostname (persistent)  
sudo hostnamectl set-hostname "web-server-01"  

timedatectl: Manage Time and Date

# View current time, timezone, and NTP status  
timedatectl  

# Set timezone to UTC  
sudo timedatectl set-timezone UTC  

# Enable NTP (network time synchronization)  
sudo timedatectl set-ntp on  

localectl: Manage Locale

# View current locale settings  
localectl  

# Set system locale to en_US.UTF-8  
sudo localectl set-locale LANG=en_US.UTF-8  

systemctl is-system-running: Check System Health

# Check if the system is running normally  
systemctl is-system-running  
# Output: running (or degraded, maintenance, etc.)  

7. Service Configuration and Management

Service behavior is defined in .service files (e.g., /lib/systemd/system/nginx.service). Systemd lets you view, edit, and reload these configurations.

View a Service File

# Show the full content of a service file  
systemctl cat nginx  

Edit a Service File Safely

Avoid editing the original service file (in /lib/systemd/system/). Instead, use drop-in files to override settings:

# Create/modify a drop-in file for nginx  
sudo systemctl edit nginx  

This opens a temporary file in /etc/systemd/system/nginx.service.d/override.conf, where you can override specific settings (e.g., ExecStart).

Reload Systemd After Changes

After modifying service files or drop-ins, reload systemd to apply changes:

sudo systemctl daemon-reload  

8. Advanced Commands

These commands handle edge cases and advanced service management.

Mask/Unmask Services

  • systemctl mask <service>: Prevent a service from starting (even manually) by linking it to /dev/null.
  • systemctl unmask <service>: Re-enable a masked service.

Example:

# Mask Apache to prevent accidental startup (use instead of disable for strict blocking)  
sudo systemctl mask apache2  

List Service Dependencies

# Show dependencies for Nginx (what Nginx depends on)  
systemctl list-dependencies nginx  

# Show reverse dependencies (what depends on Nginx)  
systemctl list-dependencies --reverse nginx  

Kill a Service

# Force-stop a misbehaving service (sends SIGKILL)  
sudo systemctl kill -s SIGKILL nginx  

9. Troubleshooting Common Issues

Service Fails to Start

  1. Check status for errors: systemctl status <service>.
  2. View detailed logs: journalctl -u <service> -b (logs since boot).
  3. Validate configuration files (e.g., nginx -t for Nginx).
  4. Check dependencies: systemctl list-dependencies <service>.

System Boots to Emergency Mode

  • Emergency mode indicates critical errors (e.g., corrupted filesystem).
  • Check logs with journalctl -b for clues.
  • Verify /etc/fstab for incorrect mount entries.

Logs Not Showing Up in journalctl

  • Ensure systemd-journald is running: systemctl status systemd-journald.
  • If logs are persistent (stored on disk), check /var/log/journal/ permissions.

Conclusion

Systemd is a cornerstone of modern Linux administration, and mastering its commands is essential for efficiently managing services, troubleshooting issues, and configuring systems. From basic service control with systemctl to advanced logging with journalctl, the commands covered here will empower you to maintain stable, performant Linux environments.

Practice these commands in a test environment to build familiarity—you’ll soon find them indispensable in your daily workflow.

References