systemctl Service Manager

systemctl cheatsheet — manage systemd services. systemctl start/stop/restart/status, systemctl enable, systemctl list-units, daemon-reload. Control every service on Linux.

8 min read

What it is

systemctl is the central command-line utility for controlling the systemd system and service manager, used for starting, stopping, enabling, disabling, and querying the status of system services and units.

Installation

systemctl is part of the systemd suite and is pre-installed on most modern Linux distributions. If you are on a system that uses systemd, you do not need to install systemctl separately.

Core Concepts

  • Units: The fundamental objects managed by systemd. Units can represent services, sockets, devices, mount points, targets (groups of units), etc. The most common types are .service units.
  • Targets: Analogous to runlevels in older init systems. Targets are special units that represent a system state and can activate a set of other units. Examples include multi-user.target (text-based multi-user mode) and graphical.target (graphical desktop mode).
  • Unit Files: Configuration files that describe how a unit should be managed. These are typically located in /etc/systemd/system/ (for system administrator overrides/additions), /usr/lib/systemd/system/ (for distribution-provided units), and /run/systemd/system/ (for runtime-generated units).
  • Dependencies: Units can declare dependencies on other units, ensuring they are started or stopped in a specific order.
  • State: Units have various states, such as active (running), inactive (stopped), enabled (configured to start on boot), disabled (configured not to start on boot), failed, etc.

Commands / Usage

Managing Services

Start a service:

sudo systemctl start nginx.service

Starts the nginx service immediately.

Stop a service:

sudo systemctl stop nginx.service

Stops the nginx service immediately.

Restart a service:

sudo systemctl restart nginx.service

Stops and then starts the nginx service.

Reload a service’s configuration:

sudo systemctl reload nginx.service

Tells the nginx service to reload its configuration without a full restart (if supported by the service).

Check the status of a service:

systemctl status nginx.service

Displays detailed information about the nginx service, including its current state, recent log messages, and process information.

Enable a service to start on boot:

sudo systemctl enable nginx.service

Creates symbolic links so that the nginx service will be started automatically when the system boots into its default target.

Disable a service from starting on boot:

sudo systemctl disable nginx.service

Removes the symbolic links that would have started nginx on boot.

Check if a service is enabled:

systemctl is-enabled nginx.service

Outputs enabled or disabled (and potentially other states like static) for the specified service.

Get the main type of a unit:

systemctl show -p MainType nginx.service

Shows the main type of the unit (e.g., simple, forking, notify, dbus).

Managing Units and Targets

List all active units:

systemctl list-units

Shows all units that are currently loaded and active.

List all loaded units (active and inactive):

systemctl list-unit-files

Shows all unit files and their enabled/disabled state.

List all units (loaded and unloaded):

systemctl list-unit-files --all

Shows all unit files, regardless of whether they are currently loaded.

List all failed units:

systemctl --failed

Shows any units that have recently failed.

List all installed unit files:

systemctl list-unit-files

Lists all unit files on the system and their current state (enabled, disabled, static).

Show detailed information about a unit:

systemctl status apache2.service

(Same as status for services, but works for any unit type).

Show specific properties of a unit:

systemctl show sshd.service

Displays all properties of the sshd.service unit.

Show a specific property of a unit:

systemctl show docker.service -p PID

Shows only the PID property for the docker.service unit.

Show the dependency tree of a unit:

systemctl list-dependencies graphical.target

Lists all units that graphical.target depends on, recursively.

Isolate to a specific target (e.g., boot into multi-user mode):

sudo systemctl isolate multi-user.target

Switches the system to the specified target, stopping all units not required by that target and starting all units that are.

Switch to a different target:

sudo systemctl default graphical.target

Sets the default target to graphical.target so it will be activated on the next boot.

Start a target (and all its dependencies):

sudo systemctl start graphical.target

Activates the graphical.target and all units it depends on.

Stop a target:

sudo systemctl stop multi-user.target

Deactivates the multi-user.target and all units that depend on it.

Managing System State

Reboot the system:

sudo systemctl reboot

Reboots the system.

Halt (power off) the system:

sudo systemctl poweroff

Shuts down and powers off the system.

Halt the system (alternative):

sudo systemctl halt

Shuts down the system, but does not necessarily power it off.

Sleep (suspend) the system:

sudo systemctl suspend

Suspends the system to RAM.

Hibernate the system:

sudo systemctl hibernate

Hibernates the system by saving its state to disk and powering off.

Hybrid-sleep the system:

sudo systemctl hybrid-sleep

Suspends the system to RAM and also hibernates it.

Managing Unit Files

Reload systemd manager configuration:

sudo systemctl daemon-reload

Reloads all unit files, after you’ve created or modified them. This is essential for systemd to recognize changes.

Re-enable unit files:

sudo systemctl reenable nginx.service

Re-enables a unit file, useful if it was somehow disabled without being explicitly told to.

Mask a unit (prevent it from being started manually or by dependencies):

sudo systemctl mask nginx.service

Creates a symbolic link from the unit file to /dev/null, effectively preventing it from being started.

Unmask a unit:

sudo systemctl unmask nginx.service

Removes the mask created by systemctl mask.

Querying System Information

Show system boot messages (like dmesg):

journalctl -k

Displays kernel ring buffer messages.

Show all logged messages:

journalctl

Displays all messages from the system journal.

Show logs for a specific service:

journalctl -u nginx.service

Displays log messages generated by the nginx service.

Follow logs for a specific service in real-time:

journalctl -f -u nginx.service

Continuously displays new log messages from the nginx service as they arrive.

Show logs since a specific time:

journalctl --since "2023-10-27 10:00:00"

Shows logs from the specified date and time onwards.

Show logs for the current boot:

journalctl -b

Displays log messages from the current boot session.

Show logs for the previous boot:

journalctl -b -1

Displays log messages from the previous boot session.

Show system information:

systemctl

A general overview of the systemd manager state.

Show systemd version:

systemctl --version

Displays the version of systemd.

Common Patterns

Start, enable, and check status of a new service:

sudo systemctl start myapp.service && sudo systemctl enable myapp.service && systemctl status myapp.service

A common workflow for deploying a new application service.

View logs for a service that just failed:

sudo systemctl status failing-service.service
sudo journalctl -u failing-service.service -n 50

First, check the status for a quick overview, then dive into the last 50 lines of its logs.

Temporarily stop a service but keep it enabled:

sudo systemctl stop unwanted.service

The service will restart on the next boot.

Completely disable and stop a service:

sudo systemctl stop unwanted.service && sudo systemctl disable unwanted.service

Ensures the service is not running and will not start on boot.

Restart a service after configuration change:

sudo systemctl restart myapp.service && sudo systemctl status myapp.service

Apply configuration changes and verify the service is running.

Check if a service is running (useful in scripts):

systemctl is-active myapp.service

Returns active if running, inactive if stopped.

Check if a service is enabled (useful in scripts):

systemctl is-enabled myapp.service

Returns enabled if enabled, disabled if not.

View all services and their states:

systemctl list-units --type=service

Filters the list to show only service units.

View all enabled services:

systemctl list-unit-files --type=service | grep enabled

Shows only services that are configured to start on boot.

Gotchas

  • daemon-reload is crucial: If you create, delete, or modify unit files (like .service files), you must run sudo systemctl daemon-reload for systemd to recognize the changes. Forgetting this is a common mistake.
  • sudo is often required: Most systemctl commands that modify the system state (start, stop, enable, disable, reboot) require superuser privileges.
  • Services might not have .service suffix: While .service is common, systemctl can manage other unit types. You can often omit the .service suffix for services, and systemd will infer it (e.g., systemctl status nginx instead of systemctl status nginx.service). However, being explicit is clearer.
  • journalctl vs syslog: journalctl is systemd’s unified logging system. It captures logs from various sources, including traditional syslog, kernel messages, and systemd-native services. If you’re used to rsyslog or syslog-ng, familiarize yourself with journalctl for systemd systems.
  • Masked units cannot be started: A masked unit cannot be started manually, automatically by dependencies, or even by daemon-reload if it’s masked. You must unmask it first.
  • Targets are not services: While you can start and stop targets like multi-user.target, their primary function is to define a state and activate a set of dependent units. Isolating to a target is more powerful than just starting it.
  • Static units: Some units are marked as static in their unit files. These cannot be enabled or disabled directly; they are typically started as dependencies of other units.
  • systemctl start --now <unit>: For commands like enable and disable, you can use --now to also start or stop the unit immediately, respectively. For example, sudo systemctl enable --now nginx.service enables the service and starts it if it’s not already running.