Skip to main content
Commands

Using systemctl Command

The systemctl command is a must know for Linux systems with systemd init system. Here are some practical examples.

β€” Sagar Sharma

Warp Terminal

Every mainstream Linux distribution including Ubuntu, Fedora, openSUSE, and RedHat uses systemd as their init system.

In simple terms, the init system is used to manage services in Linux and systemctl is a tool used to interact with the systemd.

This makes it essential for users to learn how to use the systemctl command in order to manage services in Linux (the most distros).

So in this tutorial, I will walk you through some common usage of the systemctl command.

systemctl command

Before I jump to the examples part, let's have a quick look at the syntax of using the systemctl command:

systemctl <command> <service_name>

Here,

  • <command>: is an action you want to perform for service (start, stop, restart, etc).
  • <service_name>: is where you enter the name of the service to perform actions.

In case you are curious, here a widely used options and descriptions of the systemctl command:

CommandDescription
systemctl start [service]Start a service.
systemctl stop [service]Stop a service.
systemctl enable [service]Enable a service to start automatically at system boot.
systemctl disable [service]Disable a service from starting automatically at system boot.
systemctl status [service]View the status of a service.
systemctl restart [service]Restart a service.
systemctl reload [service]Reload a service's configuration without restarting it.
systemctl mask [service]Prevent a service from being started.
systemctl unmask [service]Allow a previously masked service to be started.
systemctl set-default [target]Change the default system target (runlevel).
systemctl list-unit-filesList all installed unit files and their states.
systemctl list-dependencies [unit]List the dependencies of a specific unit.
systemctl list-socketsList all active sockets.
systemctl list-jobsList all active systemd jobs.
systemctl list-unitsShow the status of all loaded and active systemd units.

Now, let's have a look at the example of each option mentioned in the above table.

1. Start service

You are recommended to start the service after installing a package or after making some changes to the configuration file.

To start the service, all you have to do is use the start flag with the systemctl command as shown here:

sudo systemctl start <service_name>

For example, if I want to start the apache2 service, then I will be using the following command:

sudo systemctl start apache2

Yep, starting a service won't show any output (unless the service does not exist).

2. Stop service

If you want to make any changes to the configuration file you are recommended to stop the service.

To stop the service, you use the stop flag with the systemctl command as shown here:

sudo systemctl stop <service_name>

For your reference, here, I stopped the apache2 service (which I started recently):

sudo systemctl stop apache2

Stopping service too doesn't show any output unless the service does not exist in the first place.

3. Enable service

Enabling service referes to start the service automatically at the system boot. This is helpful in cases where you have configured a web server and you don't want to start everything manually.

To enable the service, you use the enable flag as shown here:

sudo systemctl enable <service_name>

For example, if I want to start the apache2 service automatically at the system boot, then, I will be using the following:

sudo systemctl enable apache2

4. Disable service

If there's a service that is configured to be started automatically at the system boot and you no longer want to do that, you can disable this behavior.

To do so, simply use the disable flag as shown here:

sudo systemctl disable <service_name>

Here's how I disabled apache2 service:

sudo systemctl disable apache2

If you notice the last line, it removed a reference symbolic link which would be responsible for starting the service at the system boot.

5. Check the status of the service

Checking the status of the service is regarded as the first step in troubleshooting the service and to check the status, all you have to do is use the status flag as shown:

systemctl status <service_name>

For example, here, I checked for the status of the apache2 service:

systemctl status apache2

Earlier, the apache2 service was disabled by me, it shows the status disabled but there are various status indicators used by systemd:

StatusDescription
active (running)Service is actively running in the background.
active (exited)The service was meant to be executed one time or periodically, and it exited upon completion.
active (waiting)The service is running but is waiting to be triggered by a specific condition or event.
inactiveService is not currently running.
enabledService will be automatically started at system boot time.
disabledService is disabled and won't be started at system boot.
staticThis service can't be managed using systemd or the systemctl command; you need to manage it manually.
maskedThe service is masked and can't be started (needs to be unmasked to make it work).
aliasThe service name is an alias, and the service is a symlink pointing to another unit file.
linkedThe service or unit file is symbolically linked to another unit file.

To learn more about checking the status of service, I'd recommend checking our detailed guide on how to check the status of service using the systemctl command:

Use Systemctl Status Command to Check Service Status
Learn how to check and understand the status of a systemd service using the systemctl command.

6. Restart a service

There are multiple reasons why one would want to restart the service such it is not responding or you just want to apply the new configuration.

To restart the service, all you have to do is use the restart flag as shown here:

sudo systemctl restart <service_name>

Let's say I want to restart the apache2 service, then I'm required to use the following command:

sudo systemctl restart apache2

Once you restart the service, the stopped service will be active and should reflect while checking the status.

7. Reload the service

Reloading the service is beneficial in the time when you made changes to the configuration and you don't want to restart the entire service to take effect from the changes.

To reload the service, all you have to do is use the reload flag as shown here:

sudo systemctl reload <service_name>

Recently, I made a few changes to the Apache configuration file and now I want to apply the changes but without restarting the entire server so here's how I restarted apache2 service:

sudo systemctl reload apache2

8. Mask service

Think of masking as an advanced version of stopping the service where you can still start the service (after stopping it) whereas the masked service can not be started at all (unless you unmask it).

So if you want to mask the service, then you can use the mask flag as shown here:

sudo systemctl mask <service_name>

For example, here, I masked the apache2 service:

sudo systemctl mask apache2

If you notice carefully, the output suggests that it created a symbolic link to /dev/null which makes it impossible to start the service (until unmasked).

9. Unmask the service

If the service was masked and now you want to make it work, there's only one way and that is to unmask the service itself.

To unmask the service, use the unmask flag as shown here:

sudo systemctl unmask <service_name>

Here's how I unmasked the apache2 service:

sudo systemctl unmask apache2

10. Set the default target

You can use the systemctl command to set the default target (or runlevel) that the system will boot into.

Such as you can specify whether to boot directly into GUI or TTY.

For example, if you want your system to boot into GUI, you set your default target to graphical.target:

sudo systemctl set-default graphical.target

But if you want to boot into the TTY with multi-user support, then you set your default target to multi-user.target:

sudo systemctl set-default multi-user.target

11. List unit files

For those who don't know, unit files in systemd are nothing but plain text configuration file that is used to define and manage system services and units.

To list unit files, all you have to do is execute the following command:

systemctl list-unit-files

It shows the status of every unit file and to learn what it is pointing to, you can refer to the earlier section where I explained every status indicator.

12. List the dependency of a specific unit file

Each unit file has its own set of dependencies and to list them all at once, you can use the list-dependencies flag as shown here:

systemctl list-dependencies <unit-file>

For example, if I want to list dependencies of the nix-daemon.socket unit file, then I will be using the following:

systemctl list-dependencies nix-daemon.socket

13. List all active sockets

By listing the active sockets, you can have insight into the network services and sockets managed by systemd.

To list all the active sockets, use the following command:

systemctl list-sockets

You can see 3 columns here, lets breakdown them down:

  • LISTEN: Lists sockets that are actively listening to incoming network connections.
  • UNIT: This column specifies the units are responsible for controlling the socket and its associated service.
  • ACTIVATES: It indicates the service or unit that will be activated when incoming network connections are received on that specific socket.

Sounds confusing? Let's take an example from the first entry:

LISTEN                                    UNIT                             ACTIVATES                   
/dev/rfkill                               systemd-rfkill.socket            systemd-rfkill.service

In the above example, the /dev/rfkill socket will be managed by the systemd-rfkill.socket unit when the connection is made to that socket.

14. List all the active systemd jobs

To list all the active systemd jobs, all you have to do is execute the following command:

sudo systemctl list-jobs

In my case, there we no active jobs at the moment:

15. Show the status of active and loaded unit files

If you want to check the status of active and located unit files, you can simply execute the following command:

systemctl list-units

Pretty cool. Right?

Next: Learn journalctl commands

journalctl is a tool that allows you to interact and analyze journal logs created by your Linux system. Learn to use it.

How to Use journalctl Command to Analyze Logs in Linux
Beginner’s guide to using journalctl commands for viewing, filtering and analyzing journal logs in Linux.

I hope you will find this guide helpful.

Sagar Sharma