systemctl

systemctl is a command-line utility used to interact with systemd, the init system and service manager used by many Linux distributions. systemctl provides a comprehensive interface for managing services, inspecting the state of the system, and controlling system behavior.

Overview of systemctl

Purpose

systemctl is used to:

  • Manage system services (start, stop, restart, enable, disable).

  • Query and change system states.

  • Control system boot process.

  • Inspect logs and system status.

Basic Usage

The general syntax for systemctl commands is:

systemctl [options] <command> [name]
  • [options]: Various options to control the behavior of systemctl.

  • <command>: The specific action to perform (e.g., start, stop, status).

  • [name]: The name of the service, unit, or target to act upon.

Common systemctl Commands

  1. Starting and Stopping Services

    • Start a service:

      systemctl start <service_name>

      Example:

      systemctl start apache2
    • Stop a service:

      systemctl stop <service_name>

      Example:

      systemctl stop apache2
    • Restart a service:

      systemctl restart <service_name>

      Example:

      systemctl restart apache2
    • Reload a service (if the service supports it):

      systemctl reload <service_name>

      Example:

      systemctl reload apache2
  2. Enabling and Disabling Services

    • Enable a service (start at boot):

      systemctl enable <service_name>

      Example:

      systemctl enable apache2
    • Disable a service (do not start at boot):

      systemctl disable <service_name>

      Example:

      systemctl disable apache2
    • Check if a service is enabled:

      systemctl is-enabled <service_name>

      Example:

      systemctl is-enabled apache2
  3. Checking Service Status

    • Get the status of a service:

      systemctl status <service_name>

      Example:

      systemctl status apache2
  4. Listing Services and Units

    • List all services:

      systemctl list-units --type=service
    • List all units (including services, sockets, targets, etc.):

      systemctl list-units
    • List failed units:

      systemctl --failed
  5. Managing System States

    • Reboot the system:

      systemctl reboot
    • Power off the system:

      systemctl poweroff
    • Suspend the system:

      systemctl suspend
    • Hibernate the system:

      systemctl hibernate
  6. Masking and Unmasking Services

    • Mask a service (prevent it from starting):

      systemctl mask <service_name>

      Example:

      systemctl mask apache2
    • Unmask a service (allow it to start):

      systemctl unmask <service_name>

      Example:

      systemctl unmask apache2

Advanced Usage

  1. Viewing Logs with journalctl

    • View logs for a specific service:

      journalctl -u <service_name>

      Example:

      journalctl -u apache2
  2. Editing Unit Files

    • Edit a service unit file:

      systemctl edit <service_name>
    • Reload systemd manager configuration after editing unit files:

      systemctl daemon-reload

Conclusion

systemctl is an essential tool for managing and controlling services and system states on a Linux system running systemd. It provides powerful capabilities for administrators to maintain and troubleshoot system services effectively.

Last updated