How to Use Systemctl Command in Systemd Linux System

Linux TLDR
Last Updated:
Reading time: 6 minutes

Most Linux distributions, such as Debian, Ubuntu, Fedora, Redhat, Manjaro, or OpenSUSE, use systemd as their init system; if unsure, you can check whether you are using systemd or not.

Despite all the criticism surrounding systemd, it still holds the number one position on the init system list, and many reputable or new Linux distributions favor it over other init systems.

Many people surprisingly think of systemd as a daemon, the first process that is started by the kernel once the system is booted. However, the truth is, it’s a software suite packaged with daemons (the systemd init service) and the systemctl command, used to interact with the systemd init service.

The systemctl command is a very useful and important command that every Linux user must be aware of. To put its definition in layman’s terms, it can be described as a command that helps to interact with systemd init services (also known as units) in your system by providing options like listing, starting, stopping, restarting, reloading, etc.

So in this tutorial, I will walk you through all the essentials you need to learn to use the systemctl command:

Tutorial Details

DescriptionManaging Systemd Services and Units
Difficulty LevelModerate
Root or Sudo PrivilegesYes
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitessystemctl
Internet RequiredNo

Syntax of Systemctl Command

The basic syntax of the systemctl command will look like the one below:

$ systemctl <command> <service_name>

Whereas,

  • β€œcommandβ€œ: this parameter is used to describe the action, such as β€œstopβ€œ, β€œstartβ€œ, β€œenableβ€œ, etc., to be performed for the corresponding service.
  • β€œservice_nameβ€œ: this is where you will mention the name of the service you would like to work with.

To manage services, you have multiple commands to use with the systemctl command that are described in detail below:

How to Use Systemctl Command on Systemd Linux System

Here, we’re going to see some practical examples of the systemctl command, covering basic to advanced commands, starting with…

List Unit Files

In systemd, the unit files are plain text configuration files that are used to define and manage systemd services.

You can list all of the unit files by executing the following command:

$ systemctl list-unit-files

List All Unit Files That Are Enabled

When you only check the unit file by following the previous method, you will get a list of all unit files, but to identify which one of them is currently being used, you need to execute the following command with the help of the grep command:

$ systemctl list-unit-files | grep enabled

Or you can execute the following command to get the list of running services in the tree hierarchy.

$ systemctl status

List All Unit Files That Failed To Start

Sometimes, another running process or a configuration error interrupts the systemd services. You can list these interruptions using the following command and then investigate the cause.

$ systemctl --failed

List All the Active Sockets

The active sockets are inter-process communication channels established on a systemd-based system, which you can list using the following command:

$ systemctl list-sockets

Start a Service

The systemd services that are already running are either part of your core system or configured by some program during installation. However, in some cases, programs do not activate their services by default, rather than wanting users to manually start the service when required.

So, to start a service, you need to use the β€œstart” flag with the systemctl command followed by the service’s name:

$ sudo systemctl start <service_name>

For example, the following is a command to start the Nginx service:

$ sudo systemctl start nginx

Stop a Service

When you plan to modify certain configuration settings of the running service, such as setting firewall restrictions in the Nginx configuration, you need to stop the service beforehand, and for that purpose, you use the β€œstop” flag:

$ sudo systemctl stop <service_name>

For example, the following is a command to stop the Nginx service:

$ sudo systemctl stop nginx

Restart a Service

If you make changes to any active service configuration file and forget to stop the service beforehand, or if some active service is not working correctly, you can restart the service using the β€œrestart” flag.

$ sudo systemctl restart <service_name>

For example, the following is a command to restart the Nginx service:

$ sudo systemctl restart nginx

Reload a Service

When you make minor changes in the service configuration file that do not affect the entire functioning of the application, instead of restarting it, you can reload the service without restarting the entire service (getting zero downtime), and you can do that using the β€œreload” flag.

$ sudo systemctl reload <service_name>

For example, the following is a command to reload the Nginx service:

$ sudo systemctl reload nginx

Check the Status of a Service

It’s always recommended to check the service status before making any modifications, like starting, stopping, restarting, or reloading. Even if you encounter issues with the service, you can check its status to identify the cause.

So, to check the status of a service, you need to use the β€œstatus” flag followed by the service name:

$ systemctl status <service_name>

For example, the following is a command to check the status of the Nginx service:

$ systemctl status nginx

Check the Service is Set to Autostart

Core system services and some application services are set to autostart whenever you restart your system. One good example is Snapd, which Snap uses to monitor all Snap packages in your system.

To determine whether your desired service will auto-start with system boot or not, you can use the β€œis-enabled” flag followed by the service name.

$ systemctl is-enabled <service_name>

For example, the following command will check whether the Nginx service is set to enabled (meaning autostart) or disabled (meaning not autostart):

$ systemctl is-enabled nginx

Enable a Service

To autostart any desired service each time the system is booted, you can set the service to an enabled state where the service will start automatically when you boot your system, and for that purpose, you can use the β€œenable” flag.

$ sudo systemctl enable <service_name>

For example, the following is a command to enable the Nginx service:

$ sudo systemctl enable nginx

Disable a Service

If you have previously enabled a service that uses ports to allow anyone on the same network to connect to your system, and you realize that you do not often use that service and it must be stopped from autostarting, you can use the β€œdisable” flag for that purpose.

$ sudo systemctl disable <service_name>

For example, the following is a command to disable the Nginx service:

$ sudo systemctl disable nginx

Mask a Service

Masking a service is a way to prevent it from being auto-started by any manual or automatic intervention, adding one more strict layer to halt the service.

Therefore, you can mask a service by using the β€œmask” flag and the service name:

$ sudo systemctl mask <service_name>

For example, the following is a command to mask the Nginx service:

$ sudo systemctl mask nginx

Unmask a Service

Unmasking, as the name suggests, means if you realize you accidentally masked the wrong service or think the masked service needs to start, then you can unmask it using the β€œunmask” flag.

$ sudo systemctl unmask <service_name>

For example, the following is a command to unmask the Nginx service:

$ sudo systemctl unmask nginx

Set the Default Target

The default target (target is referred to as runlevels in other init systems) means changing the default operational mode that the system will enter after booting up. In other words, by setting a default target, you can switch between text or GUI mode interfaces upon boot.

To find out the default target unit in use, you can use the following command:

$ systemctl get-default

To change the boot target to text mode, you can use the following command:

$ sudo systemctl set-default multi-user.target

To change the boot target to the GUI mode, you can use the following command:

$ sudo systemctl set-default graphical.target

The following are some common yet important targets:

TargetDescription
emergency.targetThis target starts an emergency shell on the console that can be used when the system is under maintenance or repair.
halt.targetThis target sends shutdown signals.
multi-user.targetThis target starts the system with networking enabled, suitable for servers and non-graphical environments.
reboot.targetThis target sends reboot signals.
rescue.targetThis target is useful for system recovery or troubleshooting when the system is in an unresponsive state and unable to boot in multi-user mode.
poweroff.targetThis target sends shutdown signals.

Reloading Systemd

To ensure the systemd daemon scans new or changed services after making alterations to your systemd unit file or creating a new systemd unit file for your desired application, reloading the systemd daemon is required, and you can accomplish this by using the following command:

$ systemctl daemon-reload

Here comes the end of this article; it covers all the important things one Linux user should know about the systemctl command, and I would also suggest bookmarking this page for future reference.

If you have further questions or queries related to the topic, then do let me know in the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.