Managing Services In Linux – systemd & sysvinit systems

Managing services in Linux is one of the important tasks of an admin. Learn what services are, how to start, stop and disable a systemctl and sysvint service. Also learn how to list Linux services.

UNDERSTANDING THE SUBJECT MATTER

What is A Systemd System and A Sysvinit sytem?

A systemd Linux system is a Linux operating system that runs on systemd (uses systemd as its service manager). For example, almost all the modern Linux systems are systemd systems. The likes of RHEL 7, RHEL 8, CentOS 7, CentOS 8, etc are all systemd systems.

Similarly, a sysvinit Linux system is a Linux operating system that runs on sysvinit (uses sysvinit as its service manager). For example, the old Linux systems are sysvinit systems. The likes of RHEL 6, CentOs 6, etc are sysvinit systems.

Going forward, I explicitly explained what systemd is in one of the articles on this site. I also talked about systemd’s runtargets extensively. It would be better you click on the link to understand what these are if you don’t, so you can really grab the subject matter.

There are basically two important systemd’s runtargets. They are the “graphical.target” and “multi-user.target. As we go on, you will also understand that you can get to create your own target as a (unit file) as an administrator if you need to.

This, I also mentioned in the article that the default runtarget the system will boot with will be the first process/service that will start. Hence will have PID = 1, and other processes/services will depend on it.

In other words, the default runtarget is the parent process/service to other processes/services. So, it is safe to say that a runtarget or simply a target is a collection of services.

Every service belongs to a particular runtarget, which basically means that every service is assigned to a target and the target takes care of starting the services.

systemd, unlike sysvinit doesn’t take care of starting services only, it also takes care of other activities/functionalities like mount, automount, sockets, devices, etc. So, systemd basically starts up and supervises almost the entire system.

How Does Systemd Work?

Moving forward, systemd uses unit files for its management. Unit files are configuration files or scripts that define how units function.

Units are the objects that systemd knows how to manage. These objects (units) have names and are of different types. Unit types are “.service”, “.target”, “.device”, “.socket”, “.mount”, .”automount”, “.scope”, “.snapshot”, “.timer”, “.slice”, “.swap”, and “.path”

Unit files are the replacement of the old sysvinit script. Unlike the sysvinit long bash script that is difficult to comprehend, unit files are short, about 17 lines and easy to interpret and comprehend.

Systemd’s unit files are found in different paths but majorly in two different paths. The first which is the “/usr/lib/systemd/system” directory. Let’s take a look at this directory.

[root@HQEBPRD ~]# cd /usr/lib/systemd/system

[root@HQEBPRD system]# ls

default.target.wants                     mdmonitor.service                      selinux-autorelabel.target         systemd-update-utmp-runlevel.service
dev-hugepages.mount                      mdmon@.service                         serial-getty@.service              systemd-update-utmp.service
dev-mqueue.mount                         messagebus.service                     shutdown.target                    systemd-user-sessions.service
dm-event.service                         microcode.service                      sigpwr.target                      systemd-vconsole-setup.service
dm-event.socket                          mlocate-updatedb.service               sleep.target                       systemd-volatile-root.service
dnf-makecache.service                    mlocate-updatedb.timer                 slices.target                      system-update-cleanup.service
dnf-makecache.timer                      ModemManager.service                   smartcard.target                   system-update-pre.target
dnsmasq.service                          multipathd.service                     smartd.service                     system-update.target
dracut-cmdline.service                   multipathd.socket                      sockets.target                     system-update.target.wants
dracut-initqueue.service                 multi-user.target                      sockets.target.wants               tcsd.service
dracut-mount.service                     multi-user.target.wants                sound.target                       teamd@.service
dracut-pre-mount.service                 ndctl-monitor.service                  sound.target.wants                 timedatex.service
dracut-pre-pivot.service                 netcf-transaction.service              speech-dispatcherd.service         timers.target
dracut-pre-trigger.service               NetworkManager-dispatcher.service      spice-vdagentd.service             timers.target.wants
dracut-pre-udev.service                  NetworkManager.service                 spice-vdagentd.socket              time-sync.target
dracut-shutdown.service                  NetworkManager-wait-online.service     sshd-keygen@.service               tmp.mount
ebtables.service                         network-online.target                  sshd-keygen.target                 tuned.service
emergency.service                        network-pre.target                     sshd.service                       udisks2.service
emergency.target                         network.target                         

you can see different unit files with their unit names and types in this directory. Services files, target files, socket files, etc. All these files are systemd’s default unit files.

When a software is installed, the software unit files are installed in this directory. I guess you don’t want to tamper with the files in here. It is advisable not to edit the files in this directory, instead, you can override the files if you need to make changes which take us to the second path where systemd’s unit files are found.

The second path where systemd’s unit files are found is “/etc/systemd/system”. Let’s take a look at this directory.

[root@HQEBPRD system]# cd /etc/systemd/system

[root@HQEBPRD system]# ls

basic.target.wants                          nfs-idmapd.service.requires
bluetooth.target.wants                      nfs-mountd.service.requires
dbus-org.bluez.service                      nfs-server.service.requires
dbus-org.fedoraproject.FirewallD1.service   printer.target.wants
dbus-org.freedesktop.Avahi.service          remote-fs.target.wants
dbus-org.freedesktop.ModemManager1.service  rpc-gssd.service.requires
dbus-org.freedesktop.nm-dispatcher.service  rpc-statd-notify.service.requires
dbus-org.freedesktop.timedate1.service      rpc-statd.service.requires
default.target                              sockets.target.wants
display-manager.service                     sysinit.target.wants
getty.target.wants                          syslog.service
graphical.target.wants                      systemd-timedated.service
multi-user.target.wants                     timers.target.wants
network-online.target.wants                 vmtoolsd.service.requires
nfs-blkmap.service.requires
[root@HQEBPRD system]#

you can see the different unit files in this directory as well. Instead of tampering with systemd’s default unit files directory, you can override the changes in that directory by defining your own unit file in this directory. This location is the location where an administrator can modify or write their unit files.

Whatever unit files you define here will have a higher priority over the unit files in the /usr/lib/systemd/system” directory, as such will override any function/behavior of the system.

As I said earlier, a unit file defines how the unit/object functions. For example, a mount unit file defines how a mount point functions (i.e, how the mount points will be mounted, either automatically or not, e.t.c)

Similarly, a service unit file defines how the service functions (i.e, how the service starts, stops, etc).

Let’s take a look at one of the service’s units file in the “/usr/lib/systemd/system” directory that we can easily relate with.

[root@HQEBPRD ~]# cd /usr/lib/systemd/system

[root@HQEBPRD system]# ls -al *.service

-rw-r--r--. 1 root root  729 Jun 18  2019 accounts-daemon.service
-rw-r--r--. 1 root root  502 May 27  2019 alsa-restore.service
-rw-r--r--. 1 root root  465 May 27  2019 alsa-state.service
-rw-r--r--. 1 root root  722 Nov  8  2018 anaconda-direct.service
-rw-r--r--. 1 root root  240 Nov  8  2018 anaconda-nm-config.service
-rw-r--r--. 1 root root  660 Nov  8  2018 anaconda-noshell.service
-rw-r--r--. 1 root root  585 Nov  8  2018 anaconda-pre.service
-rw-r--r--. 1 root root  442 Nov  8  2018 anaconda.service
-rw-r--r--. 1 root root  532 Nov  8  2018 anaconda-shell@.service
-rw-r--r--. 1 root root  583 Nov  8  2018 anaconda-sshd.service
-rw-r--r--. 1 root root  501 Nov  8  2018 anaconda-tmux@.service
-rw-r--r--. 1 root root  275 Sep 26  2018 arp-ethers.service
-rw-r--r--. 1 root root  222 Aug 12  2018 atd.service
-rw-r--r--. 1 root root 1512 Jul 25  2019 auditd.service
-rw-r--r--. 1 root root  247 Jul 25  2019 sshd-keygen@.service
-rw-r--r--. 1 root root  456 Jul 25  2019 sshd.service
[root@HQEBPRD system]# cat sshd.service

[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target

[Service]
Type=notify
EnvironmentFile=-/etc/crypto-policies/back-ends/opensshserver.config
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

you can see that this confirms what we have been saying, this unit file is not even up to 17 lines and it is easily understood and interpreted.

The file contains everything the sshd service needs to function with. How it should start, stop restart, etc. You should also know that unit files have templates. An example of a service unit file template is sshd.service file we just looked at above.

You will need to follow a unit file template to be able to successfully create a unit file. The last line, “WantedBy=multi-user.target also defines the target that will start the service which is multi-user.target.

Also, there is a file called “wants file”. When a service is enabled, a symbolic link that is associated with a target will automatically be created in a wants file, meaning that the service, of course, is wanted by a specific target and it has to be defined in a want file.

For instance, in the last line of the sshd unit file above, if the sshd service is enabled, a symbolic link will be created in the multi-user.target.wants file.

You can get to see the wants file in the directory, “/usr/lib/systemd/system and “/etc/systemd/system”.

Let’s look at the wants file of the multi-user target.

[root@HQEBPRD ~]# cd /etc/systemd/system/multi-user.target.wants
[root@HQEBPRD multi-user.target.wants]# ls

atd.service           firewalld.service       libvirtd.service        remote-fs.target   sssd.service
auditd.service        irqbalance.service      mcelog.service          rhsmcertd.service  tuned.service
avahi-daemon.service  kdump.service           mdmonitor.service       rpcbind.service    vdo.service
crond.service         ksm.service             ModemManager.service    rsyslog.service    vmtoolsd.service
cups.path             ksmtuned.service        NetworkManager.service  smartd.service
dnf-makecache.timer   libstoragemgmt.service  nfs-client.target       sshd.service

you can see the multi-user target services that are started automatically at system boot-up.

One important systemd utility is “systemctl”. From the word systemctl (system control), it is used to control the systemd service manager.

Masking and Unmasking a Service In Linux

Masking a service is ciesing a service from starting whether or whether not a user tries to start the service.

It is similar to disabling a service, but this time around, the service will be prevented from being activated when any user on the system, even with the right privileges tries to start the masked service.

Masking a service can be imperative when you need to totally deactivate a service that can disrupt the system so that any other user on the system will not be able to start the service.

When a service is masked, no user on the system, including the root user can start the service unless the service is unmasked. see examples in the “ACTION TIME” section below.

Having understood the subject matter, let’s roll to how services can be managed in a systemd Linux systems in the “ACTION TIME” section

ACTION TIME

How To Start A Service In Linux With Examples

To start services on a systemd system, use the command,

systemctl start <service-name>.

For example, to start the sshd service, use the command,

[root@HQEBPRD ~]# systemctl start sshd

To start services on a sysvinit system, use the command,

service <service-name> start

For example, to start the sshd service, use the command,

# service sshd start

How To View The Status Of A Service In Linux With Examples

To view the status of services on a systemd system, use the command,

systemctl status <service-name>

For example, to view the sshd service’s status, use the command,

[root@HQEBPRD ~]# systemctl status sshd

● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-03-22 15:23:00 WAT; 8h ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 1207 (sshd)
    Tasks: 1 (limit: 11337)
   Memory: 8.7M
   CGroup: /system.slice/sshd.service
           └─1207 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc ->

Mar 22 15:22:59 HQEBPRD systemd[1]: Starting OpenSSH server daemon...
Mar 22 15:23:00 HQEBPRD sshd[1207]: Server listening on 0.0.0.0 port 2019.
Mar 22 15:23:00 HQEBPRD sshd[1207]: Server listening on :: port 2019.
Mar 22 15:23:00 HQEBPRD systemd[1]: Started OpenSSH server daemon.
Mar 22 15:30:28 HQEBPRD sshd[3176]: Accepted password for root from 192.168.170.1 port 49696 ssh2
Mar 22 15:30:29 HQEBPRD sshd[3176]: pam_unix(sshd:session): session opened for user root by (uid=0)
Mar 22 21:51:47 HQEBPRD sshd[6132]: Accepted password for root from 192.168.170.1 port 50649 ssh2
Mar 22 21:51:47 HQEBPRD sshd[6132]: pam_unix(sshd:session): session opened for user root by (uid=0)
managing services in Linux

you can see that the service is loaded and enabled, and you can also see the log messages of the service. The “systemctl status <service>” command will show a lot of important information about a service in a glance.

To view the status of services on a sysvinit system, use the command,

 service <service-name> start 

For example, to view the sshd status, use the command,

# service sshd status

How To Reload a Service In Linux With Examples

To reload a service on a systemd system, use the command,

systemctl reload <service-name>

For example, to reload the sshd service, use the command,

[root@HQEBPRD ~]# systemctl reload sshd

If there are changes made in the configuration file while the service is started, the service will not recognize the new changes made because the service was not initialized with the changes. The service must be restarted or reloaded so that it can be initialized with the new changes.

The reload option will make the new changes take effect without changing the process ID. However, if the restart option is used, the process ID will change.


How To Stop A Service In Linux With Examples

To stop the services on a systemd system, use the command,

systemctl stop <service-name>

For example, to stop the sshd services, use the command,

[root@HQEBPRD ~]# systemctl stop sshd

To stop the services on a sysvinit system, use the command,

service <service-name> stop

For example, to stop the sshd service, use the command,

# service sshd stop

How To Restart A Service In Linux With Examples

To restart a service on a systemd system, use the command,

systemctl restart <service-name>

For example, to restart the sshd service , use the command,

[root@HQEBPRD ~]# systemctl restart sshd

To restart a service on a sysvinit system, use the command,

service <service-name> restart

For example, to restart the sshd service, use the command,

# service sshd restart

How To Make A Service Start Automatically On System Boot / How To Make A service Persitent With Examples

To make a service start at system startup on a systemd system, use the command,

systemctl enable <service-name>

For example, to make the sshd service start at system start up, use the command,

[root@HQEBPRD ~]# systemctl enable sshd

Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.
[root@HQEBPRD ~]#

you can see that the multi-user.target.wants file is modified by enabling a service.


How To Make A Service Not To Start Automatically On System Boot With Examples

To make a service not to start on system boot or not persistent on a systemd system, use the command,

systemctl disable <service-name>

For example, to make the sshd service not to start on system boot, use the command,

[root@HQEBPRD ~]# systemctl disable sshd

Removed /etc/systemd/system/multi-user.target.wants/sshd.service.
[root@HQEBPRD ~]#

you can see that when a service is disabled, the multiuser.target.wants file is modified


How To List All The Available Systemd Units and Unit Files

To list all the available systemd units and unit files, use the command,

[root@HQEBPRD ~]# systemctl list-unit-files

UNIT FILE                                                                 STATE
proc-sys-fs-binfmt_misc.automount                                         static
-.mount                                                                   generated
boot.mount                                                                generated
dev-hugepages.mount                                                       static
dev-mqueue.mount                                                          static
proc-fs-nfsd.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
tmp.mount                                                                 disabled
var-lib-machines.mount                                                    static
var-lib-nfs-rpc_pipefs.mount                                              static
cups.path                                                                 enabled
ostree-finalize-staged.path                                               disabled
systemd-ask-password-console.path                                         static

...........

How To List All The Systemd Running Units and Unit Files

To list all the available running units and unit files, uses the command,

[root@HQEBPRD ~]# systemctl list-units

UNIT                                                                                                     LOAD   ACTIVE SUB       DESCRIPTION
proc-sys-fs-binfmt_misc.automount                                                                        loaded active waiting   Arbitrary Executable File Formats File>
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device                      loaded active plugged   VMware_Virtual_S 1
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device                      loaded active plugged   VMware_Virtual_S 2
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda.device                           loaded active plugged   VMware_Virtual_S
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb-sdb1.device                      loaded active plugged   VMware_Virtual_S 1
sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb.device                           loaded active plugged   VMware_Virtual_S
sys-devices-pci0000:00-0000:00:11.0-0000:02:00.0-usb2-2\x2d2-2\x2d2.1-2\x2d2.1:1.0-bluetooth-hci0.device loaded active plugged   /sys/devices/pci0000:00/0000:00:11.0/0>
sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device                                        loaded active plugged   82545EM Gigabit Ethernet Controller (C>
sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device                                      loaded active plugged   ES1371/ES1373 / Creative Labs CT2518 (>
sys-devices-pci0000:00-0000:00:11.0-0000:02:05.0-ata4-host4-target4:0:0-4:0:0:0-block-sr0.device         loaded active plugged   VMware_Virtual_SATA_CDRW_Drive RHEL-8->
sys-devices-platform-serial8250-tty-ttyS1.device                                                         loaded active plugged   /sys/devices/platform/serial8250/tty/t>
sys-devices-platform-serial8250-tty-ttyS2.device                                                         loaded active plugged   /sys/devices/platform/serial8250/tty/t>
sys-devices-platform-serial8250-tty-ttyS3.device                                                         loaded active plugged   /sys/devices/platform/serial8250/tty/t>
sys-devices-pnp0-00:05-tty-ttyS0.device                                                                  loaded active plugged   /sys/devices/pnp0/00:05/tty/ttyS0
sys-devices-virtual-block-dm\x2d0.device                                                                 loaded active plugged   /sys/devices/virtual/block/dm-0
sys-devices-virtual-block-dm\x2d1.device                                                                 loaded active plugged   /sys/devices/virtual/block/dm-1
sys-devices-virtual-misc-rfkill.device                                                                   loaded active plugged   /sys/devices/virtual/misc/rfkill
sys-devices-virtual-net-virbr0.device                                                                    loaded active plugged   /sys/devices/virtual/net/virbr0
sys-devices-virtual-net-virbr0\x2dnic.device                                                             
dev-hugepages.mount                                                                                      loaded active mounted   Huge Pages File System
dev-mqueue.mount                                                                                         loaded active mounted   POSIX Message Queue File System
run-media-root-RHEL\x2d8\x2d1\x2d0\x2dBaseOS\x2dx86_64.mount                                             loaded active mounted   /run/media/root/RHEL-8-1-0-BaseOS-x86_>
run-user-0-gvfs.mount                                                                                    loaded active mounted   /run/user/0/gvfs

...........................
                                        

How To List Systemd Failed Units In Linux

To list systemd failed units, use the command,

[root@HQEBPRD ~]# systemctl --failed

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

How To List All Systemd Services In Linux

To list all systemd services, both the ones that are enabled and disabled, use the command,

[root@HQEBPRD ~]# systemctl list-unit-files --type=service

UNIT FILE                                  STATE
accounts-daemon.service                    enabled
alsa-restore.service                       static
alsa-state.service                         static
anaconda-direct.service                    static
anaconda-nm-config.service                 static
anaconda-noshell.service                   static
anaconda-pre.service                       static
anaconda-shell@.service                    static
anaconda-sshd.service                      static
anaconda-tmux@.service                     static
anaconda.service                           static
arp-ethers.service                         disabled
atd.service                                enabled
auditd.service                             enabled
auth-rpcgss-module.service                 static
autovt@.service                            enabled
avahi-daemon.service                       enabled
blivet.service                             static
blk-availability.service                   disabled
bluetooth.service                          enabled
bolt.service                               static

How To List All Systemd Targets That Are Currently Active

To list all systemd targets that are currently active, use the command,

[root@HQEBPRD ~]# systemctl list-units --type=target

UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
basic.target           loaded active active Basic System
bluetooth.target       loaded active active Bluetooth
cryptsetup.target      loaded active active Local Encrypted Volumes
getty.target           loaded active active Login Prompts
graphical.target       loaded active active Graphical Interface
local-fs-pre.target    loaded active active Local File Systems (Pre)
local-fs.target        loaded active active Local File Systems
multi-user.target      loaded active active Multi-User System
network-online.target  loaded active active Network is Online
network-pre.target     loaded active active Network (Pre)
network.target         loaded active active Network
nfs-client.target      loaded active active NFS client services
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target           loaded active active Paths
remote-fs-pre.target   loaded active active Remote File Systems (Pre)
remote-fs.target       loaded active active Remote File Systems
rpc_pipefs.target      loaded active active rpc_pipefs.target


How To Kill A service In Linux With Examples

To kill a service running on a systemd system, use the command,

systemctl kill <service-name>

For example, to kill the sshd service, use the command,

[root@HQEBPRD ~]# systemctl kill sshd

How To Mask and Unmask a Service In Linux With Example

To mask a service in Linux, use the command,

# systemctl mask <service-name>

For example, to mask the vsftpd service, use the command,

[root@HQEBPRD ~]# systemctl mask vsftpd

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

To unmask a service in Linux, use the command,

# systemctl unmask <service-name>

For example, to unmask the vsftpd service, use the command,

[root@HQEBPRD ~]# systemctl unmask vsftpd

Removed /etc/systemd/system/vsftpd.service.

To list a masked service in Linux, use the command,

[root@DRDEV1 ~]# systemctl list-unit-files | grep masked

systemd-timedated.service                     masked                                           

RHCSA, Q&A On Managing Services In Linux

Your feedback is welcomed. If you love others, you will share with other

5 Comments

  1. This was perfect. Thanks for the work and effort you put on this and overall on the website.
    A question, I could not find the way to get the premium membership and its information. Could you please provide the details and link for it?

    Thanks

Leave a Reply

Your email address will not be published.


*