How To Use Systemctl to Manage Systemd Services and Units

How To Use Systemctl to Manage Systemd Services and Units. In this post, we will introduce systemd, its advantages then show you how to manage systemd services and units using systemctl.

Well let’s start with How To Use Systemctl to Manage Systemd Services and Units.

What is Systemd in Linux?

Image source: Zdnet

Systemed in Linux is a system and service manager designed particularly for the Linux kernel. Proposed in 2010, Systemd replaced the widely used sysvinit system to become the first process. Aadditionally, it was executed in user space during the Linux start up process.

Kernel started the sysvinit as the first process to boot up any Linux or Unix computer. After successfully booting the system, it continually runs and waits for appropriate commands like ‘shutdown.’ all in all, it shuts down the Linux system. Simply put, Sysvinit’s process effortlessly shuts down all the tasks in the system.

Basically, it remained the most suitable choice to bring up and shut down the Linux based system for many years. However, with time, it became slow and inflexible for modern computers.

Although both sysvinit and systemd have their advantages, developers decided to replace sysvinit with systemd. Besides, it was atrocious when developers began horsing around with the crucial Linux subsystems. But, if the developers do not like a particular software application, desktop environment, or command, they shift their attention toward its alternatives.

Moreover, since subsystems have deep hooks in the kernel, every management script, and software package dependencies, it is pretty effortless to replace the older version.

In short, things change. Computers are getting more complex and changing the software to meet up with the system requirement is very essential.

Managing Services With Systemd

Some useful systemd utilities are as follows:

  • systemctl – Controls the systemd system and services.
  • hostnamectl – Controls the hostname.
  • timedatectl – Sets the time and date.
  • systemadm – Acts as a front end for the systemctl command.
  • systemd-cgls – Shows cgroup contents.
  • localectl- Configures the system’s local and keyword layout.
  • journalctl – Manages the journal and systemd’s own logging system.

Advantages of Systemd

Since 2014, several Linux distributions are relying on the Systemd system and service manager. It provides them with the following advantages:

  • Systemd daemons make the process of supervising and controlling processes effortless. Besides, it also makes the execution of parallelized jobs seamless.
  • Provides systemctl command and cgroups to make life easier.
  • The systemctl utility provides the administrator with more detailed error messages, including runtime errors and startup errors.
  • cGroups, also known as ‘Control Groups,’ enables you to group the processes into the hierarchy for effortless management.
  • As seen, it is a straightforward process to check process attributes such as function and property. For example, in the Systemd framework, child processes become “children” and are organized in the appropriate “parent” collection to show inheritance.

Please continue reading How To Use Systemctl to Manage Systemd Services and Units.

How To Use Systemctl to Manage Systemd Services and Units

Image source: 2daygeek

Systemtd and Systemctl Basics

Before starting, it is important to check whether the systemd is installed on your system. You can check it with the following command:

				
					systemctl --version
				
			

If the systemd package is installed, you should see its version in the following output:

				
					systemd 237
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid

				
			

Next, check the path of the systemd and systemctl binary with the following command:

				
					whereis systemd 
				
			

You should see the following output:

				
					systemd: /usr/lib/systemd /bin/systemd /etc/systemd /lib/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz

				
			

In order to check the path of the systemctl utility, run the following command:

				
					whereis systemctl
				
			

Following output should appear:

				
					systemctl: /bin/systemctl /usr/share/man/man1/systemctl.1.gz
				
			

Next, it is also important to know whether the systemd is running or not.

				
					ps -eaf | grep [s]ystemd
				
			

And then you get the following output:

				
					root       291     1  0 Oct30 ?        00:00:04 /lib/systemd/systemd-journald
root       345     1  0 Oct30 ?        00:00:09 /lib/systemd/systemd-udevd
systemd+   944     1  0 Oct30 ?        00:00:29 /lib/systemd/systemd-resolved
systemd+   945     1  0 Oct30 ?        00:00:00 /lib/systemd/systemd-timesyncd
message+  1255     1  0 Oct30 ?        00:00:13 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only

				
			

Manage Units using Systemctl

Unit files are the primary object in systemd. In turn, that system knows how to operate on and manage. It is a plain text ini-style file that encodes information about a service, a socket, a device, a mount point, an automount point, and a swap file. In this section, we will show you how to manage Units using the systemctl command.

To list all unit types, run the following command:

				
					systemctl -t help
				
			

You will get the following list:

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

				
			

After that, to get a list of all installed units, run the following command:

				
					systemctl list-unit-files
				
			

You should see the following output:

				
					UNIT FILE                                  STATE          
proc-sys-fs-binfmt_misc.automount          static         
-.mount                                    generated      
boot-efi.mount                             generated      
Data.mount                                 generated      
dev-hugepages.mount                        static         
dev-mqueue.mount                           static         
proc-fs-nfsd.mount                         static         
proc-sys-fs-binfmt_misc.mount              static         
run-rpc_pipefs.mount                       static         

				
			

Following, to list all running units, use the following command:

				
					systemctl list-units
				
			

You should see the following output:

				
					  UNIT                                                                                  LOAD   ACTIVE     SUB       DESCRIPTION              
  proc-sys-fs-binfmt_misc.automount                                                     loaded active     running   Arbitrary Executable File 
  dev-fuse.device                                                                       loaded activating tentative /dev/fuse                
  sys-devices-pci0000:00-0000:00:01.0-backlight-amdgpu_bl0.device                       loaded active     plugged   /sys/devices/pci0000:00/00
  sys-devices-pci0000:00-0000:00:01.1-sound-card0.device                                loaded active     plugged   /sys/devices/pci0000:00/00
  sys-devices-pci0000:00-0000:00:02.4-0000:14:00.0-net-enp20s0.device                   loaded active     plugged   RTL810xE PCI Express Fast 
  sys-devices-pci0000:00-0000:00:02.5-0000:16:00.0-net-wlp22s0.device                   loaded active     plugged   QCA9377 802.11ac Wireless 
  sys-devices-pci0000:00-0000:00:09.2-sound-card1.device                                loaded active     plugged   /sys/devices/pci0000:00/00
  sys-devices-pci0000:00-0000:00:10.0-usb2-2\x2d1-2\x2d1:1.0-bluetooth-hci0.device      loaded active     plugged   /sys/devices/pci0000:00/00
  sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active     plugged   TOSHIBA_MQ01ABD100 ESP 
  sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active     plugged   TOSHIBA_MQ01ABD100 Micr
  sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda3.device loaded active     plugged   TOSHIBA_MQ01ABD100 OS  
  sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda4.device loaded active     plugged   TOSHIBA_MQ01ABD100 4   
  sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-sda5.device loaded active     plugged   TOSHIBA_MQ01ABD100 5   
  sys-devices-pci0000:00-0000:00:11.0-ata1-host0-target0:0:0-0:0:0:0-block-sda.device   loaded active     plugged   TOSHIBA_MQ01ABD100       
  sys-devices-platform-serial8250-tty-ttyS0.device                                      loaded active     plugged   /sys/devices/platform/seri
  sys-devices-platform-serial8250-tty-ttyS1.device                                      loaded active     plugged   /sys/devices/platform/seri
  sys-devices-platform-serial8250-tty-ttyS10.device                                     loaded active     plugged   /sys/devices/platform/seri

				
			

Then, to list all failed units, run the following command:

				
					systemctl --failed
				
			

Result is the following output:

				
					  UNIT                                            LOAD   ACTIVE SUB    DESCRIPTION                                                   
● isc-dhcp-server.service                         loaded failed failed ISC DHCP IPv4 server                                          
● isc-dhcp-server6.service                        loaded failed failed ISC DHCP IPv6 server                                          
● mariadb.service                                 loaded failed failed MariaDB 10.1.48 database server                               
● nginx.service                                   loaded failed failed A high performance web server and a reverse proxy server      
● systemd-backlight@backlight:acpi_video0.service loaded failed failed Load/Save Screen Backlight Brightness of backlight:acpi_video0

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

5 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

				
			

If you want to list only service unit files, run the following command:

				
					systemctl list-unit-files -t service
				
			

At this point you get the following output:

				
					UNIT FILE                                  STATE          
accounts-daemon.service                    enabled        
acpid.service                              disabled       
alsa-restore.service                       static         
alsa-state.service                         static         
alsa-utils.service                         masked         
anacron.service                            enabled        
anydesk.service                            enabled        
apache-htcacheclean.service                disabled       
apache-htcacheclean@.service               disabled       
apache2.service                            enabled        
apache2@.service                           disabled       
apparmor.service                           enabled        
apport-autoreport.service                  static         
apport-forward@.service                    static         
apport.service                             generated      

				
			

The output has the following columns:

  • UNIT: The systemd service unit name
  • LOAD: Shows whether the unit definition was properly read and loaded
  • ACTIVE: Describes if the unit is active.
  • SUB: Low-level activation state of the unit, giving more detailed information about the unit. This will vary by unit type.
  • DESCRIPTION: The service unit’s description.

If you want to display the content of specific unit file, run the following comand:

				
					systemctl cat nginx.service
				
			

Now, this is the following output:

				
					# /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target
				
			

To see a unit’s dependency tree, run the following output:

				
					systemctl list-dependencies nginx.service
				
			

Get a list of all dependent service units in the following output:

				
					nginx.service
● ├─system.slice
● └─sysinit.target
●   ├─apparmor.service
●   ├─blk-availability.service
●   ├─dev-hugepages.mount
●   ├─dev-mqueue.mount
●   ├─keyboard-setup.service
●   ├─kmod-static-nodes.service
●   ├─lvm2-lvmetad.socket
●   ├─lvm2-lvmpolld.socket
●   ├─lvm2-monitor.service
●   ├─plymouth-read-write.service
●   ├─plymouth-start.service
●   ├─proc-sys-fs-binfmt_misc.automount
●   ├─setvtrgb.service
●   ├─sys-fs-fuse-connections.mount
●   ├─sys-kernel-config.mount

				
			

Check the properties of the specific unit file, run the following command:

				
					systemctl show nginx.service
				
			

You should see the following output:

				
					Type=forking
Restart=no
PIDFile=/run/nginx.pid
NotifyAccess=none
RestartUSec=100ms
TimeoutStartUSec=1min 30s
TimeoutStopUSec=5s
RuntimeMaxUSec=infinity
WatchdogUSec=0
WatchdogTimestampMonotonic=0
PermissionsStartOnly=no
RootDirectoryStartOnly=no
RemainAfterExit=no
GuessMainPID=yes
MainPID=0
ControlPID=0
FileDescriptorStoreMax=0
NFileDescriptorStore=0
StatusErrno=0
Result=exit-code
UID=[not set]
GID=[not set]

				
			

In order to edit the specific unit file, run the following command:

				
					systemctl edit --full nginx.service
				
			

This command will allow you to edit the modify the Nginx service file.

Manage Services Using Systemctl

Secondly, the main purpose of the systemd is to manage services and daemons for the server at any point while the system is running. Basic syntex to manage services is shown below:

				
					systemctl [action] service-name
				
			

Start the Nginx service, run the following command:

				
					systemctl start nginx
				
			

Stop the Nginx service, run the following command:

				
					systemctl stop nginx
				
			

Then to restart the Nginx service, run the following command:

				
					systemctl restart nginx
				
			

Reload the Nginx service, run the following command:

				
					systemctl reload nginx
				
			

Well and to start the Nginx service at system reboot, run the following command:

				
					systemctl enable nginx
				
			

Check the status of the Nginx service, run the following command:

				
					systemctl status nginx
				
			

Also check the active or in-active status of the Nginx service using the following command:

				
					systemctl is-active nginx
				
			

Now to check whether the service is in a enabled state, run the following command:

				
					systemctl is-enabled  nginx
				
			

To kill the Nginx service, run the following command:

				
					systemctl kill nginx
				
			

Alternatively, to mask the Nginx service and make it impossible to start, run the following command:

				
					systemctl mask nginx
				
			

You should see the following output:

				
					Created symlink /etc/systemd/system/nginx.service → /dev/null.
				
			

To unmask it again, run the following command:

				
					systemctl unmask nginx
				
			

You will get the following output:

				
					Removed /etc/systemd/system/nginx.service.
				
			

Manage Mount Points using Systemctl

Evidently, using systemd, you can also mount, unmount, remount, reload system mount points and also check the status of mount points. Forthwith, to get a list of all system mount points, run the following command:

				
					systemctl list-unit-files --type=mount
				
			

You should see the following list:

				
					UNIT FILE                                 STATE    
-.mount                                   generated
boot-efi.mount                            generated
Data.mount                                generated
dev-hugepages.mount                       static   
dev-mqueue.mount                          static   
proc-fs-nfsd.mount                        static   
proc-sys-fs-binfmt_misc.mount             static   
run-rpc_pipefs.mount                      static   

				
			

From the above list, start ad stop the Data.mount using the following command:

				
					systemctl start Data.mount
systemctl stop Data.mount
				
			

Hence, in order to check the status of the Data.mount, run the following command:

				
					systemctl status Data.mount
				
			

You should see the status of the mount in the following output:

				
					● Data.mount - /Data
   Loaded: loaded (/etc/fstab; generated)
   Active: active (mounted) since Sun 2022-10-30 09:37:24 IST; 2 days ago
    Where: /Data
     What: /dev/sda5
     Docs: man:fstab(5)
           man:systemd-fstab-generator(8)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/Data.mount

Oct 30 09:37:24 vyompc systemd[1]: Mounting /Data...
Oct 30 09:37:24 vyompc systemd[1]: Mounted /Data.

				
			

To reload the Data.mount, run the following command:

				
					systemctl reload Data.mount
				
			

To mask and unmask Data.mount, run the following command:

				
					systemctl mask Data.mount
systemctl unmask Data.mount
				
			

Manage Sockets using Systemctl

Indeed, the basic syntax to manage sockets using the systemctl command is shown below:

				
					systemctl [action] socket-name
				
			

To get a list of all system sockets, run the following command:

				
					systemctl list-unit-files --type=socket
				
			

You should see all sockets in the following output:

				
					UNIT FILE                       STATE   
acpid.socket                    enabled 
apport-forward.socket           enabled 
avahi-daemon.socket             enabled 
cups.socket                     enabled 
dbus.socket                     static  
dm-event.socket                 enabled 
docker.socket                   enabled 
epmd.socket                     disabled
lvm2-lvmetad.socket             enabled 
lvm2-lvmpolld.socket            enabled 
rpcbind.socket                  enabled 
saned.socket                    disabled
snapd.socket                    enabled 
ssh.socket                      disabled

				
			

From the above list, start the docker.socket using the following command:

				
					systemctl start docker.socket
				
			

Likewise, to stop the docker.socket, run the following command:

				
					systemctl stop docker.socket
				
			

To check the status of the docker.socket, run the following command:

				
					systemctl status docker.socket
				
			

If you want to check the active status, run the following command:

				
					systemctl is-active docker.socket
				
			

Later, to mask and unmask docker.sockets, run the following command:

				
					systemctl mask docker.socket
systemctl unmask docker.socket
				
			

Thank you for reading How To Use Systemctl to Manage Systemd Services and Units. We shall conclude.

How To Use Systemctl to Manage Systemd Services and Units Conclusion

In this guide, we explained systemd units in more detail and how to use the systemctl command to explore and manage units. We also show you how to manage services, mounts and sockets using the systemctl command. The systemctl utility is very useful for system administrator for service and system state management.

In order to read more of our content about Linux click over here.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x