Operating Systems

What is systemctl?

A command-line utility in systemd-based Linux systems that manages system services and units, allowing administrators to start, stop, enable, disable, and monitor services and their status.

Overview

systemctl is the primary control interface for the systemd system and service manager in modern Linux distributions. It replaces older service management tools like service and chkconfig, providing a unified and more powerful way to manage system services, sockets, timers, and other units. Understanding systemctl is essential for Linux system administrators and anyone preparing for Linux certification exams.

What is systemd and systemctl?

systemd is a modern init system and service manager that has become the default in most major Linux distributions including Red Hat, CentOS, Ubuntu, Debian, and SUSE. The systemctl command is the user interface to systemd, allowing administrators to manage services and system behavior. Unlike older init systems that used sequential shell scripts, systemd provides parallel service startup, on-demand activation, and advanced dependency management.

Core Functionality

Service Management

The primary function of systemctl is to manage services—background programs that provide functionality to the operating system. Common operations include:

  • Start a service: systemctl start service-name
  • Stop a service: systemctl stop service-name
  • Restart a service: systemctl restart service-name
  • Reload configuration: systemctl reload service-name (reloads without stopping)
  • Check service status: systemctl status service-name

Enabling and Disabling Services

Services can be configured to start automatically at boot time or remain disabled. The enable and disable options manage this behavior:

  • systemctl enable service-name — Creates symbolic links so the service starts at boot
  • systemctl disable service-name — Removes the links, preventing automatic startup
  • systemctl is-enabled service-name — Checks if a service is enabled

Querying System State

systemctl provides numerous commands to view the current state of services and the system:

  • systemctl list-units — Lists all loaded units
  • systemctl list-unit-files — Lists all available unit files with their enablement status
  • systemctl list-dependencies service-name — Shows dependency relationships
  • systemctl show service-name — Displays detailed unit properties
  • systemctl is-active service-name — Checks if a service is currently running

Understanding Units

In systemd terminology, a unit is any resource that the system knows how to manage. While services are the most common units, systemd also manages:

  • Services (.service): Background processes like web servers or databases
  • Sockets (.socket): Network or IPC sockets for inter-process communication
  • Timers (.timer): Scheduled jobs (similar to cron)
  • Mounts (.mount): Filesystem mount points
  • Targets (.target): Groups of units for different system states (like runlevels)
  • Slices (.slice): Resource control groups for process hierarchies

Unit Files and Configuration

Each unit is defined by a configuration file, typically located in /etc/systemd/system/ or /usr/lib/systemd/system/. These files contain directives that define how the service should behave. Common unit file sections include:

  • [Unit]: Metadata like Description, dependencies (Requires, Wants, Before, After)
  • [Service]: Service-specific options including Type, ExecStart, ExecStop, Restart behavior
  • [Install]: Installation information like WantedBy (which targets include this service)

For example, a basic SSH service unit file might contain:

[Unit]
Description=OpenSSH server daemon
After=network.target

[Service]
Type=simple
ExecStart=/usr/sbin/sshd -D
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

Common systemctl Commands

Basic Service Operations

systemctl start sshd — Starts the SSH service immediately

systemctl status httpd — Shows detailed status including whether it's running, enabled, and recent log entries

systemctl restart mariadb — Stops and then starts the MariaDB service

Boot Configuration

systemctl enable nginx — Enables nginx to start at boot

systemctl disable postfix — Prevents postfix from starting automatically

systemctl mask service-name — Completely disables a service (prevents even manual start)

systemctl unmask service-name — Removes the mask

System-Level Operations

systemctl reboot — Reboots the system

systemctl poweroff — Shuts down the system

systemctl suspend — Enters suspend mode

systemctl get-default — Shows the default target (equivalent to runlevel)

systemctl set-default graphical.target — Sets the default target

Filtering and Querying

systemctl list-units --type=service — Lists only service units

systemctl list-units --state=failed — Shows failed units

systemctl list-units --failed — Shorthand for failed units

systemctl list-unit-files --state=enabled — Shows all enabled services

Understanding Service States

A service can exist in various states:

  • active (running): The service is currently executing
  • active (exited): The service completed successfully and is not running (normal for one-shot services)
  • active (waiting): The service is running but waiting for events
  • inactive (dead): The service is not running
  • failed: The service failed to start or crashed
  • deactivating: The service is in the process of stopping
  • reloading: The service is reloading its configuration

Advanced Features

Dependency Management

systemd handles service dependencies intelligently. For example, a web application might require the database service to start first. These dependencies are defined in unit files and systemctl respects them, preventing race conditions and startup errors.

Service Restart Policies

systemctl can automatically restart failed services. The Restart= directive in a unit file defines when restart should occur (on-failure, always, etc.), and RestartSec= specifies the delay between restart attempts.

Resource Limits and Control

systemctl manages cgroup integration for resource control, allowing administrators to limit CPU, memory, and I/O usage per service. Commands like systemctl set-property service-name MemoryMax=500M can dynamically adjust these limits.

Practical Examples

Example 1: Troubleshooting a Failed Service

An administrator notices a service isn't responding. They would run:

systemctl status postgresql
systemctl restart postgresql
systemctl is-active postgresql

Example 2: Persistent Service Configuration

To ensure a monitoring service survives reboots:

systemctl enable prometheus
systemctl start prometheus
systemctl is-enabled prometheus

Example 3: Finding Failed Services

After a system update, an administrator checks for problems:

systemctl list-units --failed

Best Practices

  • Enable services rather than using startup scripts: systemctl provides better dependency handling and consistency
  • Check service status after making changes: Always verify that services are running as expected
  • Use reload instead of restart when possible: Reloading preserves active connections and is less disruptive
  • Review unit file dependencies: Ensure services start in the correct order to prevent startup failures
  • Monitor failed services: Regularly check for failed services that may indicate configuration or system problems
  • Use journalctl with systemctl: When investigating service problems, use journalctl -u service-name to view detailed logs
  • Document custom units: Create clear descriptions and comments in custom unit files for maintainability

Comparison with Legacy Tools

systemctl supersedes several older commands:

  • service — Old command for starting/stopping services
  • chkconfig — Old tool for managing boot services
  • init scripts — Direct shell scripts in /etc/init.d/

While some distributions still support these for compatibility, systemctl is the modern standard and preferred approach.

Integration with System Logging

systemctl integrates with journald, the systemd logging system. The journalctl command can be combined with systemctl operations to view service-specific logs: journalctl -u httpd -n 50 shows the last 50 log entries for the httpd service, providing detailed troubleshooting information.

Studying for CompTIA (Operating Systems)?

ExamWizardz turns the official objectives into a guided study plan — with practice tests, real PBQs, and a readiness score. Join the waitlist to be first in when CompTIA A+ launches.

Related terms