Correct Way of Using Ubuntu systemctl to Control Systemd

Published:18 November 2021 - 5 min. read

Managing a service or group of services in a modern Linux PC can be a drag, that is, if you’re not sure what you’re doing. Lucky for you, Ubuntu Systemctl has you covered to manage services on a Linux PC.

In this tutorial, you will learn how to manage Systemd services on Ubuntu PC by running different systemctl commands.

Ready? Time to dig in!

Prerequisites

This tutorial will be a hands-on demonstration but doesn’t require special tools, so long as you’re on Ubuntu 16 or higher version, you’re good to go. This tutorial uses Ubuntu 20.04.3 LTS version for the demos.

What is Ubuntu Systemctl?

Before running Ubuntu systemctl commands, get to know what Systemctl is first. Systemctl is a command-line utility to control and manage systemd and system services.

In a nutshell, Systemctl is a control panel or service manager for the systemd init system to initialize components after the Linux kernel is booted. Systemctl is a collection of libraries, daemons, and utilities that you can use to manage services.

To effectively manage services, know the different service states as follow:

  • Enabled – indicates a service is configured to start when the system boots.
  • Disabled – indicates a service is configured not to start when the system boots.
  • Active – indicates a service is currently in running state.
  • Inactive – indicates a service is not currently running but may start when some application tries to start that service.

Listing All Services

Now that you know the different states of services, perhaps you want to see a list of all the services on your Ubuntu PC. Listing all services provides a handful of information that prepares you for managing services.

Open your terminal and run the below command to list all the service units (list-units) (active, running, exited, or failed) with the service set as unit type (--type=service).

systemctl list-units --type=service

Pick any of the services from the list you want to manage later in this tutorial and note the UNIT name (e.g., apache2). Hit the spacebar to scroll down, or hit the “Q” key to exit the command.

By the way, here’s a good guide on Linux Shell Scripting.

Listing All Services (active, running, exited, or failed)
Listing All Services (active, running, exited, or failed)

Listing Services with Specific State

Instead of listing all services, perhaps you want to narrow down the list of services. If so, adding the --state parameter will do the trick, as it filters out the services with the specific state you specify.

Run either of the commands below to list all active services (--type=service --state=active), both running and exited.

systemctl list-units --type=service --state=active
Listing Services on "active" State
Listing Services on “active” State

Similarly, to list only exited or running services, change the state value from active to either running or exited, as shown in the below commands:

systemctl --type=service --state=running
systemctl --type=service --state=exited
Listing Services on "running" State
Listing Services on “running” State
Listing Services on "stopped" State
Listing Services on “stopped” State

Listing Enabled Services

Apart from listing active, running, and exited services, you can also list enabled and disabled services by pipelining the grep command.

Run either of the commands below to list (list-unit-files) disabled or enabled services (--type=service).

## Lists all Enabled services
systemctl list-unit-files --type=service | grep enabled
## Lists all Disabled services
systemctl list-unit-files --type=service | grep disabled

Take note of an enabled service, as shown below, as you’ll need it later for disabling a service, but this tutorial uses the acpid.service for demo. The acpid is an extensible daemon that supports the Advanced Configuration and Power Interface (ACPI) events.

Listing Enabled Services
Listing Enabled Services

Listing Service Properties

Aside from listing services, you may want to check a service’s properties. Having insight into service properties is handy for troubleshooting, parameters for the process, or restart behavior.

Run the command below to show a service’s (acpid.service) properties in a key=value format.

systemctl show acpid.service
Showing Service Properties
Showing Service Properties

Now, if you’re only looking for a specific service property, you can pass the -p flag with the property name.

Run the below command to show a service’s (acpid.service) MainPID property.

systemctl show acpid.service -p MainPID
Showing Specific Service Property
Showing Specific Service Property

Managing a Specific Service

Since the basics of listing services are out of the way, it’s time to manage a specific (single) service by running the systemctl stop and start commands.

Apache service (apache2) is used for the following demos, but you may freely manage the service you noted in the “Listing All Services” section. But before you stop or start a service, you first need to verify the service’s status.

Run the below command on a terminal to view the apache2 service’s detailed information like its status.

systemctl status apache2

Below, you can see that the apache2 service is active and running.

Viewing apache2 Service Status
Viewing apache2 Service Status

Entering the sudo password is not needed for viewing service status as you are not changing the service state. But if you start, stop or restart a service, you have to prepend the sudo command and enter the sudo password.

Stopping a Service

Perhaps you want to stop a service that was left hanging and still running in the background. In that case, the systemctl stop command is what you need. Run the systemctl command below to stop the apache2 service.

sudo systemctl stop apache2

If you prefer to stop multiple services simultaneously, specify each service separated by space, like this: sudo systemctl stop sshd apache2. In this command, you’ll stop both sshd and apache2 services in one go.

Now run the systemctl status command as you did previously to verify if the apache2 service stopped.

systemctl status apache2

You can see below that the status of the apache2 service changed to inactive (dead), which indicates the service stopped.

Verifying if apache2 Service is Stopped
Verifying if apache2 Service is Stopped

Starting a Service

Now how do you start a service if you noticed it’s not running at all? You run a command similar to stopping service, which is the systemctl start command. Run the below command to start the apache2 service.

sudo systemctl start apache2

Like stopping multiple services, you can also start multiple services in a single line of command. Replace the stop command with the start command like this: sudo systemctl start sshd apache2

Now run the systemctl status command one more time to verify if the apache2 service is running.

systemctl status apache2

If you see the active (running) status, as shown below, then you’re all set as the service is back in business.

Starting apache2 service
Starting apache2 service

If you prefer to restart the service right off the bat without having to stop it first. If so, go for the systemctl restart command instead, like this: sudo systemctl restart apache2

Enabling or Disabling a Service

Perhaps there’s a particular service you prefer to either start or prevent to start on system boot. If so, running the systemctl enable or disable command is the best approach.

The enable subcommand lets you configure the default startup settings on your system, while the disable subcommand prevents a service from running on system boot.

Run the systemctl command below to disable the service (acpid.service) you noted in the “Listing Enabled Services” section. Enter your sudo password to authorize running the command.

sudo systemctl disable acpid.service
Disabling a Service
Disabling a Service

Now since acpid.service supports OS-directed configuration and Power Management (OSPM); you need to re-enable it. The command to enable service is similar to disabling a service.

Execute the systemctl command below to enable acpid.service. This command registers the service back to default startup settings on your system.

sudo systemctl enable acpid.service
Enabling a Service
Enabling a Service

Conclusion

In this tutorial, you’ve realized how quickly you can stop, start or restart services by running Ubuntu systemctl commands. You’ve also learned that the systemctl command lets you manage not just one but multiple services simultaneously.

With this newfound knowledge, why not automate controlling services in your Ubuntu system?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!