Managing systemd services and units using systemctl commands Print

  • 0

The default system and service manager for most Linux distributions now is systemd. It is designed to be backward compatible with SysV init scripts and provides several features such as parallel startup of system services at boot time, on-demand activation of daemons, or dependency-based service control logic. In Red Hat Enterprise Linux 7, systemd replaces Upstart as the default init system.

Along with systemd, a new concept called systemd units was introduced. These units are represented by unit configuration files located in one of the directories listed under

/usr/lib/systemd/system/
/run/systemd/system/
/etc/systemd/system/

And encapsulate information about system services, listening sockets, and other objects relevant to the init system.

The systemd runs as the first process after the kernel boot and is responsible for bringing the Linux host up. It is responsible for starting and managing the services, mounting filesystems, managing hardware, generating the login prompt, and much more. A key benefit over SysV is that systemd starts as many services as possible in parallel, thus speeding up the startup process, bringing up the login screen faster.

Service Units

The target units are service units for service management, which have unit files with a suffix of .service.

 

Managing systemd services

The command to manage systemd units is systemctl

Starting and Stopping Services

To start a systemd service, use the systemctl start command:

# systemctl start name.service

It is not mandatory to include .service suffix. For example, to start the apache server on Ubuntu:

# systemctl start apache2

To stop a running service:

# systemctl stop name.service

So, to stop the apache server on Ubuntu:

# systemctl stop apache2


Restarting and Reloading services

To restart a running service, use the restart command:

# systemctl restart name.service

And where the only reloading configuration file is required, use reload

# systemctl reload name.service


Enabling and Disabling Services

If you want a service to start automatically at system boot, use enable command:

# systemctl enable name.service

To disable a service from starting at system boot, use disable

# systemctl disable name.service

Disabling does not stop a running service but will only come into effect once the system is rebooted.


Viewing service status

To view information about a service, use the status command.

# systemctl status name.service

This displays the service status and the first few lines of the log file. So, while the service is running is, the output of

# systemctl status apache2

output

● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Thu 2021-06-10 03:05:13 PDT; 1min 21s ago
Main PID: 3880 (apache2)
    Tasks: 55 (limit: 4915)
   CGroup: /system.slice/apache2.service
           ├─3880 /usr/sbin/apache2 -k start
           ├─3883 /usr/sbin/apache2 -k start
           └─3884 /usr/sbin/apache2 -k start
Jun 5 03:05:13 master-node systemd[1]: Starting The Apache HTTP Server...
Jun 5 03:05:13 master-node apachectl[3855]: AH00557: apache2: apr_sockaddr_info_get() failed for master-node
Jun 5 03:05:13 master-node apachectl[3855]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress th
Jun 5 03:05:13 master-node systemd[1]: Started The Apache HTTP Server.

 

Let us now see the command to check if a service is active:

# systemctl is-active name.service

So, while the apache2 service is running, the output of systemctl is-active command is :

# systemctl is-active apache2

Output

active

To check if a service is enabled:

# systemctl is-enabled name.service.

 

Viewing System State

All commands you have seen till now are used to manage a single service. When you want an overview of the system state, use the following set of commands:

To see all unit types

# systemctl -t help

 Output

Available unit types:
service
socket
target
device
mount
automount
swap
timer
path
slice
scope

 

To list all installed units, use list-unit-files.

# systemctl list-unit-files

Output

UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
-.mount generated
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
acpid.path enabled
apport-autoreport.path enabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-244.scope transient
session-250.scope transient
accounts-daemon.service enabled
acpid.service disabled
apache-htcacheclean.service disabled
[email protected] disabled
apache2.service enabled
[email protected] disabled
apparmor.service enabled
apport-autoreport.service static
[email protected] static
apport.service generated
apt-daily-upgrade.service static
apt-daily.service static
atd.service enabled
[email protected] enabled
bootlogd.service masked
bootlogs.service masked

Note: The above output is only partial for demonstration purposes only. The actual list is long and may vary.

The output has only two columns Unit File and State. The state will usually be enabled, disabled, static, or masked.

Static: This means the unit cannot be enabled, performs a one-off action, or is a dependency of another unit and cannot be run by itself.

Masked: A unit listed as masked means it is entirely unstartable, as it is linked to /dev/null. This is called masking the unit. This prevents the service from being started, manually or automatically.

 

List all installed services.

The systemctl list-unit-files command with -t or –type service filter shows the state of installed services only.

# systemctl list-unit-files -t service

Output

UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
-.mount generated
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
acpid.path enabled
apport-autoreport.path enabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-244.scope transient
session-250.scope transient
accounts-daemon.service enabled
acpid.service disabled
apache-htcacheclean.service [email protected] disabled
apache2.service enabled
[email protected] disabled
apparmor.service enabled
apport-autoreport.service static
[email protected] static
apport.service generated
apt-daily-upgrade.service static

To see all active service units, use list-units with -t service filter.

# systemctl list-units -t service

Output

UNIT                                    LOAD                       ACTIVE               SUB                 DESCRIPTION

accounts-daemon.service                 loaded                     active               running             Accounts Service
apache2.service                         loaded                     active               running             The Apache HTTP Server
apparmor.service                        loaded                     active               exited              AppArmor initialization
apport.service                          loaded                     active               exited              LSB: automatic crash report generation
atd.service                             loaded                     active               running             Deferred execution scheduler
console-setup.service                   loaded                     active               exited              Set console font and keymap
containerd.service                      loaded                     active               running             containerd container runtime
cron.service                            loaded                     active               running             Regular background program processing daemon
dbus.service                            loaded                     active               running             D-Bus System Message Bus
docker.service                          loaded                     active               running             Docker Application Container Engine
ebtables.service                        loaded                     active               exited              ebtables ruleset management
fail2ban.service                        loaded                     active               running             Fail2Ban Service
[email protected]                      loaded                     active               running             Getty on tty1
grub-common.service                     loaded                     active               exited              LSB: Record successful boot for GRUB
[email protected]                       loaded                     active               exited              ifup for eth0
irqbalance.service                      loaded                     active               running             irqbalance daemon
keyboard-setup.service                  loaded                     active               exited              Set the console keyboard layout


LOAD = Reflects whether the unit definition was loaded correctly.
ACTIVE = The high-level unit activation state, i.e., generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.


Was this answer helpful?