Systemd is the essential service manager for the majority of Linux distributions. To use the service manager, an essential command is systemctl. Therefore, if you are a newbie sysadmin for servers on the base of Linux, learning functions and the syntax behind systemctl is one of the primary steps – to which this article is dedicated.

Listing Linux Services Using systemctl

What is Systemd Essentials?

Systemd is the basic set of tools for managing the fundamental components of the majority of popular Linux distributions. Systemd is responsible for initializing and managing system services, devices, and other aspects of the system’s boot process and runtime environment.

One of the key functions of systemd is its performing the role of an init (initialization) system. When the system starts, init is one of the primary processes to start, and it accompanies the initialization of all remaining processes until the system is turned off.

However, systemd should not be regarded as the only and the default init system for Linux. There are a bunch of alternatives. Moreover, systemd is often criticized and misliked by users for being too complicated and thus regarded as not really adhering to fundamental principles of Unix systems as a concept. This has gone so far that there is, for instance, even an option to specifically unselect distributions with systemd on Distro Watch.

Nevertheless, lots of users are loyal to Systemd, and learning its basics will be a helpful capacity for any system administrator.

Exploring the Linux systemctl Command

The Linux systemctl command is one of the essential commands that are responsible for managing the systemd init system. The command allows to switch on and off different services and acquire info about their condition and dependencies.

Efficient Management of Systemd Services Using systemctl

Now, let’s have a look at different uses of systemctl for systemd services.

Prerequisites for Service Management

Before you can use systemctl for systemd services, make sure you have access to your VPS server as well as root privileges for it.

Navigating Systemd Service Management

 

Checking Service Status via Linux systemctl

To check the contemporary status of a particular service, the essential command is:

 

systemctl status <nameoftheservice>

 

If you want to check something like ufw firewall, type:

 

systemctl status ufw

 

What you’ll see will include the service status, its PID as well as some log information.

Viewing Service Properties with Linux systemctl

Service properties are responsible in many ways for a service’s operation.

To see the properties of a given service:

 

systemctl show <nameoftheservice>

 

This command will give you info about all the properties of a given service.

If you’re interested in one particular property, add -p and the required property. For example to visualize the SliceName \property of the sshd service:

 

systemctl show -p SliceName sshd

 

 

Linux Services with systemctl

Controlling Service Start and Stop using Linux systemctl

A further application of systemctl is changing the status of a particular service. In this case – starting it or stopping it.

To stop a service

systemctl stop <nameoftheservice>

 

To stop sshd, type:

systemctl stop sshd

 

In a similar way, to start a service:

 

systemctl start <nameoftheservice>

 

To start sshd, type:

 

systemctl start sshd

 

To restart a given service:

 

systemctl restart <nameoftheservice>
systemctl restart sshd

 

 

Reloading a Service (Refreshing Configuration)

If you want to reload the service’s configuration, but you don’t know to start it over, you can also benefit from the “reload” option:

 

systemctl reload

 

systemctl reload sshd

 

 

Trying to Reload a Service, Falling Back to Restart if Necessary

Some services don’t support reloading. In case you try to reload them, you’ll get a message about the impossibility of doing that, and you’ll have to restart the service anyway. If you don’t know if a service reloads or not, and still want to get through this with one single command, you can use the “reload-or-restart” option:

 

systemctl reload-or-restart <nameoftheservice>

 

The command will either reload the service if it supports reloading or restart it if not.

 

systemctl reload-or-restart sshd

 

Enabling a Service using Linux systemctl

If you want your service to start at once with your system, you have to enable it. Do it with this command:

 

systemctl enable <nameoftheservice>

 

systemctl enable sshd

 

If you want to enable a service and start it as well (it hasn’t been started yet) you can add the –now affix:

 

systemctl enable <nameoftheservice> --now

 

Disabling a Service using Linux systemctl

If in turn, you don’t want your service to start together with the OS, you can disable it in a similar way:

 

systemctl disable <nameoftheservice>

 

systemctl disable sshd

Similarly, you can kill two birds with one stone and disable and stop the service with a single command with the help of the –now switch:

 

systemctl disable <nameoftheservice> --now

 

Masking a Service via Linux systemctl

After disabling a service, you can still start it. If you want to make a process unstartable, you can mask it. This way it won’t be able to be started until unmasked:

 

systemctl mask <nameoftheservice>

 

Keep in mind that you’ll also need to make sure there is no .service file belonging to the service in question at

/etc/systemd/system/

. To do it:

Rename the file by appending “.old” at the end of its current name:

mv /etc/systemd/system/sshd.service /etc/systemd/system/sshd.service.old

Mask the service:

 

systemctl mask sshd

 

Unmasking a Service through Linux systemctl

A service masked can be eventually unmasked:

 

systemctl unmask <nameoftheservice>

 

systemctl unmask sshd

 

Delving into Systemd Unit Management

Another considerable part of systemd administering is systemd unit management.

Enumerating Units using Linux systemctl

Systemd units are in fact the files belonging to services (the .service files). By entering the command

systemctl

or

systemctl list-units

you can get the list of all the units that are loaded to the system.

The information will be presented in the form of columns.

  • UNIT: The unit’s name.
  • LOAD: The configuration of the file: loaded, error, not found, masked, or bad-setting.
  • ACTIVE: The status of the unit. Can be active, infective, or failed.
  • SUB: “Substates” – more detailed info on the status of units. Can be running, stop, exited, etc. For more information about the possible states, apply systemctl –state=help.

Keep in mind that the default commands:

systemctl

and

systemctl list-units

will only show the loaded services. To display the remaining services, you need to append the –all switch:

systemctl list-units --all.

To filter the services and see only ones in a certain state or of a certain type, apply –state and –type switches respectively:

systemctl list-units --state=inactive --

 

Revealing Dependencies via Linux systemctl

With systemctl you can also see a service’s dependencies:

systemctl list-dependencies <nameoftheservice>

Example:

 

systemctl list-dependencies sshd

Displaying Unit Files through Linux systemctl

You can see the list of files currently involved in the operation of systemd, which can serve different purposes. To do it:

systemctl cat <unit name>
systemctl cat sshd

 

Modifying Unit Files using Linux systemctl

Unit files can be edited to change certain parameters of a service:

systemctl edit --full <nameoftheservice>
systemctl edit --full sshd

The unit will open in the text editor you use.

If you want to create a new file snippet in lieu of editing the old file, just don’t add –full:

systemctl edit <nameoftheservice>

As a result, you’ll get the

override.conf

file that will be active instead of the source file.

For the new setting to come into force, reload the service.

Working with Targets in Systemd

Targets are groups of units that are launched at the same time to enter a particular mode.
There are the following targets in systemd:

poweroff.target:

Used when switching the system off.

rescue.target:

Single-user mode target.

multi-user.target:

Target for launching the multi-user mode (standard, with no graphic interface): Multi-user mode with networking and a display manager.

reboot.target:

A target for rebooting.

To see the active target:

systemctl get-default

To see all available targets:

systemctl list-units --type target --all

To set a particular target as default:

systemctl set-default <target_name>.target

To activate a particular target at once:

systemctl isolate <target_name>.target

Executing systemctl Commands on a Remote Host

Above in the article we’ve gone through the essential syntax using

systemctl

on a local host. To work with systemctl commands on a remote host, you’ll need to add the -H or –host options as well as your username and your hostname.
To run the command sytemctl status sshd, you’ll, for instance, have to enter:

systemctl status sshd -H root@remotehost
root@remotehost password:

Final Thoughts

Systemd is a service manager that is widely used on various Linux distros. To administrate it in an efficient manner, having the knowledge of the syntax of the systemctl command is essential. In the article above we’ve covered the main operations that can be done with this command. We hope that the information was helpful, thank you for your attention.