LINUX-25
SERVICE MANAGEMENT FRAMEWORK (RHEL-7)
SYSTEMD/SYSTEMCTL
– PART-2
We learned the basics of init/upstart and little bit of “systemd”.
Now let’s do a brief analysis of SYSTEMD.
We were at units and targets in systemd.
Systemd is a system and service manager for Linux operating systems.
It is designed to be backwards compatible with SysV init scripts, and
provides a number of features such as parallel startup of system services at
boot time, on-demand activation of daemons, support for system state snapshots,
or dependency-based service control logic. In Red Hat Enterprise Linux 7,
systemd replaces Upstart as the default init system.
Systemd introduces the concept of systemd units. These units are
represented by unit configuration files located at,
Directory
|
Description
|
/usr/lib/systemd/system/
|
Systemd unit
files distributed with installed RPM packages.
|
/run/systemd/system/
|
Systemd unit
files created at run time. This directory
takes precedence over the directory with installed service unit files. |
/etc/systemd/system/
|
Systemd unit
files created by systemctl enable as well as
unit files added for extending a service. This directory takes precedence over the directory with runtime unit files. |
Units are basic building blocks of “systemd”. Most common are SERVICE
UNITS with “.service” extension.
“systemd” is a daemon which supposed to run in background. If it is
running and controlling the things from background then there must be a
frontend for the same.
Yes………… it’s “SYSTEMCTL”
Systemctl is the command to control systemd units.
To list all active systemd units,
[root@rhel7-client ~]# systemctl list-units
To list all active/inactive/maintenance/failed systemd units,
[root@rhel7-client ~]# systemctl list-units --all
To list enabled/disabled units at startup,
[root@rhel7-client ~]# systemctl list-unit-files
loaded active running
loaded active exited
loaded inactive dead
loaded active running Enable
loaded active waiting
anaconda.service static
arp-ethers.service disabled
bluetooth.service enabled
Field
|
Description
|
Loaded
|
Information
whether the service unit has been loaded, the absolute path to the unit file,
and a note whether the unit is enabled.
|
Active
|
Information
whether the service unit is running followed by a time stamp.
|
active (running)
|
running
|
active (exited)
|
successfully
completed
|
active (waiting)
|
active but
waiting for an event
|
Main PID
|
The PID of
the corresponding system service followed by its name.
|
Status
|
Additional
information about the corresponding system service.
|
Process
|
Additional
information about related processes.
|
CGroup
|
Additional
information about related Control Groups (cgroups).
|
inactive
|
not running
|
enabled
|
will start
at boot
|
disabled
|
will not
start at boot time
|
static
|
cannot be
enabled, but may be started by an enabled unit automatically
|
To list available unit types,
[root@rhel7-client ~]# systemctl -t help
Available unit types:
service
socket
target
device
mount
automount
snapshot
timer
swap
path
slice
scope
To get the info about particular units.
[root@rhel7-client ~]# systemctl list-units --type=service
--all
[root@rhel7-client ~]# systemctl list-units --type=target –all
[root@rhel7-client ~]# systemctl list-units --type=swap –all
[root@rhel7-client ~]# systemctl list-units --type=device –all
[root@rhel7-client ~]# systemctl list-units --type=mount
--all
It is always good to consult “help” for available options.
[root@rhel7-client ~]# systemctl –help
MANAGE SERVICES/UNITS WITH
SYSTEMCTL,
What we can do with “SYSTEMCTL”
When using systemctl, we have to specify the complete name of the
unit file, including its suffix, for example sshd.socket. There are however a
few short forms when specifying the unit in the following systemctl commands:
·
If we do not specify the suffix, systemctl will
assume .service. For example, netctl and netctl.service are equivalent.
·
Mount points will automatically be translated into
the appropriate .mount unit. For example, specifying /home is equivalent to
home.mount.
·
Similar to mount points, devices are automatically
translated into the appropriate .device unit, therefore specifying /dev/sda2 is
equivalent to dev-sda2.device.
[root@rhel7-client ~]# man systemctl
Custom unit configuration files stored in…
[root@rhel7-client ~]# ls -ltr /etc/systemd/system |more
Default unit configuration files stored in…
[root@rhel7-client ~]# ls -l /usr/lib/systemd/system |more
total 1164
-rw-r--r--. 1 root
root 275 Mar 3 2014
abrt-ccpp.service
-rw-r--r--. 1 root
root 380 Mar 3 2014
abrtd.service
-rw-r--r--. 1 root
root 361 Mar 3 2014
abrt-oops.service
-rw-r--r--. 1 root
root 266 Mar 3 2014
abrt-pstoreoops.service
-rw-r--r--. 1 root
root 248 Mar 3 2014
abrt-vmcore.service
-rw-r--r--. 1 root
root 311 Mar 3 2014
abrt-xorg.service
-rw-r--r--. 1 root
root 421 Jan 29 2014 accounts-daemon.service
-rw-r--r--. 1 root
root 501 Jan 29 2014 alsa-restore.service
-rw-r--r--. 1 root
root 558 Jan 29 2014 alsa-state.service
-rw-r--r--. 1 root
root 412 Jan 29 2014 alsa-store.service
-rw-r--r--. 1 root
root 646 Apr 9 2014
anaconda-direct.service
-rw-r--r--. 1 root
root 660 Apr 9 2014
anaconda-noshell.service
-rw-r--r--. 1 root
root 387 Apr 9 2014
anaconda.service
-rw-r--r--. 1 root
root 684 Apr 9 2014
anaconda-shell@.service
-rw-r--r--. 1 root
root 322 Apr 9 2014
anaconda-sshd.service
-rw-r--r--. 1 root
root 312 Apr 9 2014
anaconda.target
drwxr-xr-x. 2 root
root 4096 Jan 21 04:11 anaconda.target.wants
-rw-r--r--. 1 root
root 498 Apr 9 2014
anaconda-tmux@.service
-rw-r--r--. 1 root
root 275 Feb 14 2014 arp-ethers.service
-rw-r--r--. 1 root
root 205 Jan 29 2014 atd.service
====================O/P REMOVED=========================
Here each file contains the configuration of a systemd unit whose
type matches the file extension.
Like, network.target includes the configuration for the network
service target.
TARGET:
In systemd “target” serves the same function as “runlevel” but acts
as little different.
Target manages a set of services under a single unit, represented by
a target name rather than a runlevel number.
Systemd targets are represented by target units. Target units end
with the “.target” file extension and their only purpose is to group together
other systemd units through a chain of dependencies.
Current Target,
[root@rhel7-client ~]# systemctl -t target
UNIT LOAD ACTIVE SUB
DESCRIPTION
basic.target loaded active active Basic System
bluetooth.target loaded active active Bluetooth
cryptsetup.target loaded active active 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.target loaded active active Network
nfs.target loaded active active Network File
System Server
paths.target loaded active active Paths
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
swap.target loaded active active Swap
sysinit.target loaded active active System
Initialization
timers.target loaded active active Timers
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.
18 loaded units
listed. Pass --all to see loaded but inactive units, too.
To show all
installed unit files use 'systemctl list-unit-files'.
[root@rhel7-client ~]#
All available Targets,
[root@rhel7-client ~]# systemctl list-units --type=target
--all
UNIT LOAD ACTIVE
SUB DESCRIPTION
basic.target loaded active active Basic System
bluetooth.target loaded active active Bluetooth
cryptsetup.target loaded active active Encrypted Volumes
emergency.target loaded inactive dead Emergency Mode
final.target loaded inactive dead Final Step
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 inactive dead Network is Online
network.target loaded active active Network
nfs.target loaded active active Network File System Server
nss-lookup.target loaded inactive dead Host and Network Name Lookups
nss-user-lookup.target
loaded inactive dead User and Group
Name Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded inactive dead Remote File Systems (Pre)
remote-fs.target loaded active active Remote File Systems
rescue.target loaded inactive dead Rescue Mode
shutdown.target loaded inactive dead Shutdown
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
syslog.target not-found inactive dead syslog.target
time-sync.target loaded inactive dead System Time Synchronized
timers.target loaded active active Timers
umount.target loaded inactive dead Unmount All Filesystems
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.
29 loaded units
listed.
To show all
installed unit files use 'systemctl list-unit-files'.
[root@rhel7-client ~]#
How the targets replaced runlevel…
[root@rhel7-client ~]# ls -ltr
/usr/lib/systemd/system/runlevel?.target
lrwxrwxrwx. 1 root
root 15 Oct 22 12:30 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root
root 13 Oct 22 12:30 /usr/lib/systemd/system/runlevel1.target ->
rescue.target
lrwxrwxrwx. 1 root
root 17 Oct 22 12:30 /usr/lib/systemd/system/runlevel2.target ->
multi-user.target
lrwxrwxrwx. 1 root
root 17 Oct 22 12:30 /usr/lib/systemd/system/runlevel3.target ->
multi-user.target
lrwxrwxrwx. 1 root
root 17 Oct 22 12:30 /usr/lib/systemd/system/runlevel4.target ->
multi-user.target
lrwxrwxrwx. 1 root
root 16 Oct 22 12:30 /usr/lib/systemd/system/runlevel5.target ->
graphical.target
lrwxrwxrwx. 1 root
root 13 Oct 22 12:30 /usr/lib/systemd/system/runlevel6.target ->
reboot.target
we can see how each target is associated with a runlevel by sym link
to it. These links provide backward compatibility with the old sysV runlevels.
Default target is defined under /etc/systemd/system. That can be overridden
manually by GRUB2 menu during Boot process.
RUNLEVEL
|
TARGET
|
DESCRIPTION
|
0
|
poweroff.target
|
Shut down and power off
the system.
|
1, s, single
|
rescue.target
|
Set up a rescue shell,
all FS are mounted
|
2
|
multi-user.target
|
User-defined/Site-specific
runlevels. By default, identical to 3, non-graphical
|
3
|
multi-user.target
|
Full multi user,
non-graphical
|
4
|
multi-user.target
|
User-defined/Site-specific
runlevels. By default, identical to 3, non-graphical
|
5
|
graphical.target
|
Set up a graphical
multi-user system, Default Target
|
6
|
reboot.target
|
Shut down and reboot
the system.
|
emergency
|
emergency
|
Emergency shell, only
"/" FS mounted in RO mode.
|
Default Target,
[root@rhel7-client ~]# systemctl get-default
graphical.target
each targets are associated with several systemd units, these units
are dependencies for that target.
List Dependencies for particular target,
[root@rhel7-client ~]# systemctl list-dependencies
graphical.target
We can see that the default target under /etc/systemd/system having
sym link to the current default runlevel under /usr/lib/systemd/system/
[root@rhel7-client ~]# ls -ltr /etc/systemd/system |grep -i
default
drwxr-xr-x. 2 root
root 85 Oct 22 12:30
default.target.wants
lrwxrwxrwx. 1 root
root 36 Oct 22 12:47 default.target
-> /lib/systemd/system/graphical.target
Change Default Target,
[root@rhel7-client ~]# systemctl set-default
multi-user.target
rm
'/etc/systemd/system/default.target'
ln -s
'/usr/lib/systemd/system/multi-user.target'
'/etc/systemd/system/default.target'
Now check the new default Target,
[root@rhel7-client ~]# systemctl get-default
multi-user.target
Confirm the sym link also,
[root@rhel7-client ~]# ls -ltr /etc/systemd/system |grep -i
default
drwxr-xr-x. 2 root
root 85 Oct 22 12:30
default.target.wants
lrwxrwxrwx. 1 root
root 41 Feb 2 12:03 default.target ->
/usr/lib/systemd/system/multi-user.target
Rollback to original,
[root@rhel7-client ~]# systemctl set-default
graphical.target
rm
'/etc/systemd/system/default.target'
ln -s
'/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'
[root@rhel7-client ~]# systemctl get-default
graphical.target
[root@rhel7-client ~]# ls -ltr /etc/systemd/system |grep -i
default
drwxr-xr-x. 2 root
root 85 Oct 22 12:30
default.target.wants
lrwxrwxrwx. 1 root
root 40 Feb 2 12:05 default.target ->
/usr/lib/systemd/system/graphical.target
Switching targets online,
[root@rhel7-client ~]# systemctl isolate
Invalid number of arguments.
[root@rhel7-client ~]# systemctl isolate multi-user.target
Rollback to original,
[root@rhel7-client ~]# systemctl isolate graphical.target
[root@rhel7-client ~]# systemctl emergency
[root@rhel7-client ~]# systemctl rescue
To avoid sending message to logged in users, we can use “--no-wall”
flag.
[root@rhel7-client ~]# systemctl --no-wall rescue
SERVICES:
ð
Start/stop/restart/reload/status/check/enable/disable/mask/unmask
To see all available commands,
[root@rhel7-client ~]# systemctl <tab>
cancel halt list-unit-files set-environment
condreload help list-units show
condrestart hibernate mask show-environment
condstop hybrid-sleep poweroff snapshot
daemon-reexec is-active preset start
daemon-reload is-enabled reboot status
default is-failed reenable stop
delete isolate reload suspend
disable kexec reload-or-restart try-restart
emergency kill reload-or-try-restart unmask
enable link rescue unset-environment
exit list-dependencies reset-failed
force-reload list-jobs restart
get-default list-sockets set-default
Activates a service immediately:
[root@rhel7-client ~]# systemctl start vsftpd
Deactivates a service immediately:
[root@rhel7-client ~]# systemctl stop vsftpd
Restarts a service:
[root@rhel7-client ~]# systemctl restart vsftpd
Ask a unit to reload its configuration in case any config change:
[root@rhel7-client ~]# systemctl reload vsftpd
Failed to issue method
call: Job type reload is not applicable for unit vsftpd.service.
Shows status of a service including whether it is running or not:
[root@rhel7-client ~]# systemctl status vsftpd
vsftpd.service -
Vsftpd ftp daemon
Loaded: loaded
(/usr/lib/systemd/system/vsftpd.service; enabled)
Active: active (running) since Thu
2017-02-02 12:24:32 IST; 26s ago
Process: 22978 ExecStart=/usr/sbin/vsftpd
/etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
Main PID: 22979 (vsftpd)
CGroup: /system.slice/vsftpd.service
└─22979 /usr/sbin/vsftpd
/etc/vsftpd/vsftpd.conf
Feb 02 12:24:31
rhel7-client systemd[1]: Starting Vsftpd ftp daemon...
Feb 02 12:24:32
rhel7-client systemd[1]: Started Vsftpd ftp daemon.
Deactivating the service:
[root@rhel7-client ~]# systemctl stop vsftpd
Check whether a unit is already enabled or not:
[root@rhel7-client ~]# systemctl is-enabled vsftpd
Enabled ç currently it is deactivated but enabled
here.
See the status:
[root@rhel7-client ~]# systemctl status vsftpd
vsftpd.service -
Vsftpd ftp daemon
Loaded: loaded
(/usr/lib/systemd/system/vsftpd.service; enabled)
Active: inactive (dead) since Thu 2017-02-02
12:25:23 IST; 43s ago
Process: 22978 ExecStart=/usr/sbin/vsftpd
/etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
Main PID: 22979 (code=killed, signal=TERM)
Feb 02 12:24:31
rhel7-client systemd[1]: Starting Vsftpd ftp daemon...
Feb 02 12:24:32
rhel7-client systemd[1]: Started Vsftpd ftp daemon.
Feb 02 12:25:23
rhel7-client systemd[1]: Stopping Vsftpd ftp daemon...
Feb 02 12:25:23
rhel7-client systemd[1]: Stopped Vsftpd ftp daemon.
What does it mean…?
This enable/disable has nothing to do with current status, it is
notifying that the service will enable/disable at boot time or not.
Enables a service to be started on bootup:
[root@rhel7-client ~]# systemctl enable vsftpd
Disables a service to not start during bootup:
[root@rhel7-client ~]# systemctl disable vsftpd
rm '/etc/systemd/system/multi-user.target.wants/vsftpd.service'
[root@rhel7-client ~]# systemctl is-enabled vsftpd
Disabled
[root@rhel7-client ~]# systemctl start vsftpd
[root@rhel7-client ~]# systemctl status vsftpd
vsftpd.service -
Vsftpd ftp daemon
Loaded: loaded
(/usr/lib/systemd/system/vsftpd.service; disabled)
Active: active (running) since Thu
2017-02-02 12:37:59 IST; 2s ago
Process: 23586 ExecStart=/usr/sbin/vsftpd
/etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
Main PID: 23587 (vsftpd)
CGroup: /system.slice/vsftpd.service
└─23587 /usr/sbin/vsftpd
/etc/vsftpd/vsftpd.conf
Feb 02 12:37:59
rhel7-client systemd[1]: Started Vsftpd ftp daemon.
Though it is running now, but it will not start at boot-up.
Rollback to original,
[root@rhel7-client ~]# systemctl enable vsftpd
ln -s
'/usr/lib/systemd/system/vsftpd.service'
'/etc/systemd/system/multi-user.target.wants/vsftpd.service'
[root@rhel7-client ~]# systemctl is-enabled vsftpd
Enabled
Mask a unit to make it impossible to start it:
[root@rhel7-client ~]# systemctl mask vsftpd
ln -s '/dev/null' '/etc/systemd/system/vsftpd.service'
[root@rhel7-client ~]# systemctl is-enabled vsftpd
Masked
Unmask a unit:
[root@rhel7-client ~]# systemctl unmask vsftpd
rm '/etc/systemd/system/vsftpd.service'
[root@rhel7-client ~]# systemctl is-enabled vsftpd
Enabled
To list all failed services:
[root@rhel7-client ~]# systemctl --failed --type=service
|more
To list only service units:
[root@rhel7-client ~]# systemctl -t service
To show only active services:
[root@rhel7-client ~]# systemctl list-units --type=service
To show all active services:
[root@rhel7-client ~]# systemctl list-units --type=service –all
To get detailed status info about service:
[root@rhel7-client ~]# systemctl status -l vsftpd
To list dependencies:
[root@rhel7-client ~]# systemctl list-dependencies vsftpd
CONTINUED……………………………..
I am definitely enjoying your website. You definitely have some great insight and great stories.
ReplyDeleteboutique sans gluten strasbourg