Operations 8 min read

Mastering systemctl: Essential Commands for Linux Service Management

This guide provides a comprehensive overview of the systemctl command, covering its basic operations like starting, stopping, restarting, reloading, and checking service status, as well as common options, practical examples, and advanced usage such as user services, remote execution, custom configurations, and pre/post execution hooks.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering systemctl: Essential Commands for Linux Service Management

Introduction

systemctl

is a command‑line tool for interacting with the systemd init system, allowing users to start, stop, restart, reload, and query services on modern Linux distributions. Because systemd is the default init system on many Linux platforms, mastering systemctl is essential for effective system administration.

Basic Usage

1. Start a Service

Use the start subcommand: sudo systemctl start servicename Example – start Apache:

sudo systemctl start apache2

2. Stop a Service

Use the stop subcommand: sudo systemctl stop servicename Example – stop Apache:

sudo systemctl stop apache2

3. Restart a Service

Use the restart subcommand: sudo systemctl restart servicename Example – restart Apache:

sudo systemctl restart apache2

4. Reload Configuration

If a service’s configuration file changes, reload it without a full restart:

sudo systemctl reload servicename

5. Check Service Status

Query the current state of a service: sudo systemctl status servicename Example – view Apache status:

sudo systemctl status apache2

Common Options

-a

: Show all units, including inactive ones. -l: Display full log output. --no-pager: Disable paging of command output. --type=TYPE: Filter units by type, e.g., --type=service or --type=target.

Example Commands

1. Enable and Disable Services

Enable a service to start at boot: sudo systemctl enable servicename Disable a service from starting automatically:

sudo systemctl disable servicename

2. List All Services

Show every service unit on the system:

sudo systemctl list-units --type=service

3. Find a Specific Service

Filter the list for a keyword, such as ssh:

sudo systemctl list-units --type=service | grep ssh

Advanced Usage

1. Run a Service in a User Session

Execute a service under a specific user’s environment with the --user flag:

# Start a user‑level service
systemctl --user start servicename

2. Operate on a Remote Host

Use -H to target a different machine:

# Start a service on a remote host
systemctl -H user@remotehost start servicename

3. Use a Custom Configuration File

Specify an alternative configuration with --configuration:

# Start a service using a custom config file
systemctl --configuration=/path/to/custom.conf start servicename

4. Pre‑ and Post‑Start Hooks

Define commands to run before or after a service starts via ExecStartPre and ExecStartPost in the unit file:

[Service]
ExecStartPre=/bin/command1
ExecStartPost=/bin/command2

In this example, command1 runs before the service starts, and command2 runs after it has started.

Conclusion

The systemctl utility is a powerful tool for managing services on Linux systems. By understanding its basic commands, common options, and advanced features, administrators can efficiently control service lifecycles, customize execution environments, and automate pre‑ and post‑service actions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

linuxcommand-lineService Managementsystemdsystemctl
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.