Manage Systemd Services Using systemctl [With Examples]

5 min


All the major Linux distributions such as Ubuntu, Fedora, etc uses systemd init system today for managing and controlling various services while the system is running. In this guide, we explain the systemd commands which you can use to manage systemd services using systemctl.

Contents

  • Service management concept
  • List of Unit types
  • Manage Systemd Services Using systemctl 
  • List Services
  • Show Service Status
  • Start a service
  • Stop a service
  • Restart and reload a service
  • Enable a service
  • Disable a service

Service management concept

Systemd is the init system and service manager of the modern Linux systems. Init system is the first process that starts when you power on your system and keeps on running until it is shutdown.

The main purpose of systemd as init system is to initialize various system components just after the Linux kernel is booted at the beginning. Along with that, when the system is running it also manages various services and daemons such as ssh daemon, network manager, etc.

The systemd works on the unit files. There are different types of unit files based on the purpose and resources. For example, the services have unit files with .service extensions, whereas device unit files have .device extensions. The systemctl command of systemd is used to manage the unit files. 

Important Unit files and description

Unitsextensionsdescriptions
Service unit.serviceA system service.
Target unit.targetA group of systemd units.
Automount unit.automountA file system automount point.
Device unit.deviceA device file recognized by the kernel.
Mount unit.mountA file system mount point.
Path unit.pathA file or directory in a file system.
Scope unit.scopeAn externally created process.
Slice unit.sliceA group of hierarchically organized units that manage system processes.
Snapshot unit.snapshotA saved state of the systemd manager.
Socket unit.socketAn inter-process communication socket.
Swap unit.swapA swap device or a swap file.
Timer unit.timerA systemd timer.

Where is the unit files stored?

The unit files are stored in two places ideally in a Linux system. The files created at run time, boot time are stored in /run/systemd/system/. The unit files you create manually (e.g. systemctl enable command) is stored at /etc/systemd/system/.

The /etc/systemd/system/ path takes precedance over the run time unit files present at /run/systemd/system/.

There is another path where systemd keeps the system’s copy of the service unit files – /lib/systemd/system.

Examples

run systemd system - unit files
run systemd system – unit files
etc systemd system - user enabled unit files
etc systemd system – user enabled unit files

Manage Systemd Services Using systemctl 

First, let’s take a look at how you can list down the services, check their status, etc. This is required if you want to understand the state of your system for investigation purposes. 

To list out all systemd active units, you can use the below command.

systemctl list-units 
systemctl list-units (1)
systemctl list-units (1)
systemctl list-units (2)
systemctl list-units (2)

This command gives a large output with the below headings. Scroll using arrow keys (up, down, left, and right) to view the entire output. 

  • UNIT – Name of the systemd unit
  • Load – Reflects whether systemd parsed the config file of the unit and it is loaded in memory
  • ACTIVE – Unit state (high-level status). 
  • SUB – Unit state (low-level status)
  • DESCRIPTION – Description of the unit.
SEE ALSO:   A Guide to systemd journal Maintenance [With Examples]

For example, the ACTIVE value can be active where the SUB level can be many such as running, listening, dead, and active.

More example commands:

systemctl list-units --all
systemctl list-units --all --state=inactive

List Services

To list all units of type service, use the below command. 

systemctl list-units --type=service
list loaded services
list loaded services

You can combine additional parameters as well. For example, if you want to find out the services that are running, use the below command.

systemctl list-units --type=service --state=running
list all running services
list all running services

The other combinations of state values which you can use are –

  • active
  • inactive
  • running
  • dead
  • exited
  • plugged
  • tentative
  • listening
  • waiting
more examples of state values
more examples of state values

Additionally, you can list all unit files using the below commands. 

systemctl list-unit-files

Managing unit files are a different topic which I would cover in the next article.

Show Service Status

To find out the status of a service, use the below command. For this guide, I have used the NetworkManager service which controls the network management of a system. You can replace the NetworkManager with your own service name. All the following commands require sudo privilege. If you do not know the service name, check the above commands to display all service units, then filter out using the grep command.

systemctl status NetworkManager.service

Note that, you can omit the .service at the end from the service name. The systemd is clever enough to understand the command!

status of a service
status of a service

In the above example, you can see all information is provided in the command. It also has the CGroup value to identify the user groups as well – which is needed in a server environment.

Some more example commands:

systemctl is-active NetworkManager.service
systemctl is-enabled NetworkManager.service
systemctl is-failed NetworkManager.service

Start a service

Starting a systemd service is easy. Run below command with start and service name. The command starts the service.

sudo systemctl start NetworkManager.service

Stop a service

Using the stop parameter you can stop a service. For example:

sudo systemctl stop NetworkManager.service

Be cautious while stopping a service. Make sure you know what are you doing.

Restart and reload a service

The systemd provides options to configure a service in such a way that it can restart or reloads without restarts. The restart parameter restart the services and the reload uses configuration files

sudo systemctl restart NetworkManager.service
sudo systemctl reload NetworkManager.service
restart a service
restart a service

Enable and disable a service

If you created a new service or installed any applications which work via services, then starting them using the start command will not enable it again in the next boot. So, if you want the systemd to start a service when the system boos you need to use the enable command. The same for disabling as well when you want to disable a service at boot time.

sudo systemctl enable NetworkManager.service

The enable command creates a symlink from /etc/systemd/system to the target locations.

To disable the service, use below command.

sudo systemctl disable NetworkManager.service

The systemd services and their associated commands have many additional options as well. The systemd is a robust and important component of modern Linux systems. I hope this guide helps you to troubleshoot your desktop or servers when required in Linux. There are many additional features that systemd provides – such as target files, modifying the service unit files, etc – which I will cover in the next set of articles. All the articles are tagged with systemd for easy browsing. 


We bring the latest tech, software news and stuff that matters. Stay in touch via Telegram, Twitter, YouTube, and Facebook and never miss an update!

Join our Telegram channel and stay informed on the move.

Also Read


Like it? Share with your friends!

Arindam

Creator of debugpoint.com. All time Linux user and open-source supporter. Connect with me via Telegram, Twitter, or send us an email.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x