Sunday, December 15, 2019

Controlling services using systemctl

Introduction to systemd

System startup and server processes are managed by the systemd system and Service Manager.This program provide a mentod for activating system resources, Server Daemons and other processes, both on boot time and on  running system.

Daemons are process that wait or run in the background performing various task.
To listen for connection Daemon uses a socket.

A Service often refers to one or more daemons,but starting or stopping a service may instead make a on-time change to the state of the system for example, to configure Network interfaces, which does not involve leaving a daemon process running afterward.

In RHEL 7 , Process ID 1 is systemd, the new init system.
In RHEL 6 and older system , Process ID 1 is init process

Few new feature of systemd
  • Paralleization capabilities, which increase the boot speed of a system
  • On-Demand starting of Daemons without requiring a separate service.
  • Automatic service dependency management prevents long timeouts, such as not starting a network service when the network is not active.
  • A method of tracking related processes together using Linux Control groups.

systemctl and systemd units
The systemctl command is used to manage different type of systemd objects, called units. A list of available unit type can be displayed with systemctl -t help .
[root@server1 ~]# systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope

Some of the common unit type are listed as follows.
service unit have a .service extension and represent system services. This type of unit is used to start  frequently accessed daemons, such as a web server
socket unit have a .socket extension and represent Inter process communication sockets. Control of the socket will be passed to a daemon or newly started service when a client connection is made.
Path unit have .path extension and are used to delay the activation of a service unitl a specific file system change occurs.This commonly used for service which use spool directories, such as a printing systems.


Command                                   
Task                                                                                
systemctl  status UNIT
View detailed information about a unit state
systemctl   stop UNIT
Stop a service ona running system
systemctl   start UNIT
Start a service on a running system
systemctl   restart UNIT
Restart a service on a running system
systemctl   reload UNIT
Reload configuration file of a running service.
systemctl   mask UNIT
Completely disable a service from being started, both manually and at boot.
systemctl   unmask UNIT
Make a masked service available
systemctl   enable UNIT
Configure a service to start at boot time
systemctl   disable UNIT
Disable a service from starting at boot time
systemctl   list-dependencies UNIT
List units which are required and wanted by the specified unit.
systemctl   is-active UNIT
Check whether the unit is in active state
systemctl   is-enabled UNIT
Check whether the unit is enabled to  start automatically at boot time

Checking Service status
[root@server1 ~]# systemctl status sshd.service
 sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-12-16 16:17:07 IST; 4h 7min left
     Docs: man:sshd(8)
           man:sshd_config(5)
  Process: 1164 ExecStart=/usr/sbin/sshd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1303 (sshd)
   CGroup: /system.slice/sshd.service
           └─1303 /usr/sbin/sshd

Dec 16 16:17:06 localhost.localdomain systemd[1]: Starting OpenSSH server daemon...
Dec 16 16:17:07 localhost.localdomain systemd[1]: PID file /var/run/sshd.pid not readable (yet?) after start.
Dec 16 16:17:07 localhost.localdomain sshd[1303]: Server listening on 0.0.0.0 port 22.
Dec 16 16:17:07 localhost.localdomain sshd[1303]: Server listening on :: port 22.
Dec 16 16:17:07 localhost.localdomain systemd[1]: Started OpenSSH server daemon.

Keyword:
Description:
loaded
Unit configuration file has been processed
active(running)
Running with one or more continuing processes
active(exited)
Sucessfully completed one-time configuration
active(waiting)
Running but waiting for a event
inactive
Not running
enabled
Will be started at boot time
disabled
Will not be started at boot time
static
Cannot be enabled, but may be started by an enabled unit.

List Unit files with systemctl

Query the state of all units to verify a system startup.
Both the command give the same output.
[root@Server1 ~]# systemctl list-units 
[root@Server1 ~]# systemctl 
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
auditd.service                     loaded active running Security Auditing Service
NetworkManager.service             loaded active running Network Manager

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.


Query the status of only the service units
[root@Server1 ~]# systemctl list-units --type=service
[root@Server1 ~]# systemctl --type=service

The --all option will add inactive units as well.
[root@Server1 ~]# systemctl list-units  -t service -all
[root@Server1 ~]# systemctl -t service -all
  UNIT                                   LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                         loaded    active   running Security Auditing Service
  auth-rpcgss-module.service             loaded    inactive dead    Kernel Module supporting RPCSEC_GSS

List only failed services
[root@Server1 ~]# systemctl --failed --type=service
0 loaded units listed. Pass --all to see loaded but inactive units, too.

Viewing the enabled and disabled setting for all units. Optionally, Limit the type of unit.
[root@Server1 ~]# systemctl list-unit-files --type=service
UNIT FILE                                   STATE
arp-ethers.service                          disabled
auditd.service                              enabled
auth-rpcgss-module.service                  static

We can also check whether the particular service enabled to start after reboot using is-enabled option
[root@Server1 ~]# systemctl is-enabled sshd
enabled

We can check whether particular unit is active using is-active option
[root@Server1 ~]# systemctl is-active sshd
active


Unit Dependencies
Service may be started as dependencies of other services. If a socket unit is enabled and the service unit with the same name is not, the service will automatically started when a request is made to that socket unit.

[root@Server1 ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─besclient.service
● ├─brandbot.path
● ├─choose_repo.service

--reverse show particular unit is required for which other units.
[root@Server1 ~]# systemctl list-dependencies multi-user.target --reverse
multi-user.target
● └─graphical.target


Masking services
A system may have conflicting services installed for a certain function, such as firewalls (iptables and firewalld). To prevent an administrator from accidentally starting a service, a service may be masked.
[root@Server1 ~]# systemctl mask iptables
ln -s '/dev/null' '/etc/systemd/system/iptables.service'
[root@Server1 ~]# systemctl unmask iptables
rm '/etc/systemd/system/iptables.service'

Enabling system daemons to start or stop at boot
Service are started at boot when links are created in the appropriate systemd configuration directories.
[root@server1 ~]# systemctl enable sshd
Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service.

Same way disable can be used to disable the service from starting during the boot.
[root@server1 ~]# systemctl disable sshd
Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service.


No comments: