Description: Candidates should be able to query and modify the behaviour of system services at various targets / run levels. A thorough understanding of the systemd, SysV Init and the Linux boot process is required. This objective includes interacting with systemd targets and SysV init run levels.
Key Knowledge Areas:
Systemd
SysV init
Linux Standard Base Specification (LSB)
Terms and Utilities:
/usr/lib/systemd/
/etc/systemd/
/run/systemd/
systemctl
systemd-delta
/etc/inittab
/etc/init.d/
/etc/rc.d/
chkconfig
update-rc.d
init and telinit
Over View
During the previous lessons we talked about initrd/initramfs. When the kernel completly loaded it searchs for init process to start it. init process can be init, upstart or systemd. Traditionally System v init is used to start other services but it has some short comings. So other solutions invented like upstart and systemd.
/sbin/init can be linked to upstart or systemd. to check which system you are running, check each directory existence:
Directpry
Description [if exist]
/etc/init.d
Shows you have SysV in your linux box
/usr/share/upstart
You are on a Upstart Based system
/usr/lib/systemd
You are using Systemd-Based system
also try stat /proc/1/exe
link
Description
File: ‘/proc/1/exe’ -> ‘/sbin/init’
in SysV and upstart system
File: '/proc/1/exe' -> '/lib/systemd/systemd'
in Systemd box
Sys v
System "5" or Sys "v" is an ancient method of handling system services from unix world back to 1980s. SysV uses serial loading of services, in another word each service must be loaded in sequence (after each other). SysV uses runlevels concept to define which stat the server should boot in. In each runlevel specific amount of shell scripts is processed to reach the state we desire.
runlevels start from 0 upto 6 and they are different in Redhat based and Debian based systems.
runlevel
Redhat
Debian
0
System Halt (do not set as default)
System Halt (do not use as default)
1
Single User Mode
Single User Mode
2
Multi User without NFS
Full multi User mode with GUI(default)
3
Full Multi User Mode
--same as 2-- --unused--
4
---unused--
--same as 2-- --unused--
5
X11/Full Multi user Mode(default)
--same as 2-- --unused--
6
reboot (Do not set as initdefault)
rebbot (do not set as default)
/etc/inittab is SysV configuration file where default runlevel can be set, We use CentOS 5 for demonstration :
#
# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
#
# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
#
# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:5:initdefault:
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
# When our UPS tells us power has failed, assume we have a few minutes
# of power left. Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
By modifying id:5:initdefault: we can change the run level for the next time boot but there is another proper way :
init / telinit
init and telinit commands are the same.how ever telinit is recommended. They are both used to change current system runlevel
[root@centos5 ~]# telinit 3
and for come back to previous run level:
[root@centos5 ~]# telinit 5
and to see previous runlevel and current runlevel use runlevel command:
[root@centos5 ~]# runlevel
3 5
as you can see 3 was our previous runlevel and we have switched from it to 5.
/etc/init.d and /etc/rc.d
as we said SysV runs scripts in sequence to start services. But how and where they are managed? its simple but implementation is some how complicated.
all scripts are inside /etc/rc.d/init.d but there are symbolic links to desired rc folder. each rc folder specify one runlevel. so if you want to manually start a service in a run level you can create a symbolic link inside desired rc folder from init.d folder and put a name with sequence for that. "K" for Kill the service and "S" to Start it.
chkconfig
Exploring rc folders, creating symbolic links is a hard job. chkconfig is a great tool which let us turn on or off specific service or services in desired runlevel.lets start:
start service on any unspecial run level(3,4,5 centOS)
chkconfig <service name> off
turn service off in all runlevels
chkconfig --list <service name>
List service condition in all unlevels
chkconfig --level 345 <service name> on/off
Turn serve on/off in runlevel 3,4 and 5
update-rc.d
in Debian based systems like ubuntu update-rc.d used as a command instead of chkconfig command
root@ubuntu:/etc# update-rc.d
usage: update-rc.d [-n] [-f] <basename> remove
update-rc.d [-n] <basename> defaults [NN | SS KK]
update-rc.d [-n] <basename> start|stop NN runlvl [runlvl] [...] .
update-rc.d [-n] <basename> disable|enable [S|2|3|4|5]
-n: not really
-f: force
The disable|enable API is not stable and might change in the future.
root@ubuntu:/etc# update-rc.d apparmor disable
update-rc.d: warning: apparmor start runlevel arguments (none) do not match LSB Default-Start values (S)
Disabling system startup links for /etc/init.d/apparmor ...
Removing any system startup links for /etc/init.d/apparmor ...
/etc/rc0.d/K63apparmor
Adding system startup for /etc/init.d/apparmor ...
/etc/rc0.d/K63apparmor -> ../init.d/apparmor
upstart
The first serious attempt to replace systemV was upstart, created by ubuntu. upstart is reactionary, means it takes events and based on them run jobs. In comparison with SysV upstart is more flexible but still it uses scripts and like SysV has some shortages. Although upstart is backward compatible and lets us to use SysV commands. if your system has /etc/init directory it us using upstart.
upstart keeps all previous SysV Folders and uses it, We use Ubuntu 14 machine :
root@server1:/etc# cd /etc/init.d/ && ls
acpid dns-clean procps single
anacron friendly-recovery pulseaudio skeleton
apparmor grub-common rc speech-dispatcher
apport halt rc.local sudo
avahi-daemon irqbalance rcS thermald
bluetooth kerneloops README udev
brltty killprocs reboot umountfs
console-setup kmod resolvconf umountnfs.sh
cron lightdm rsync umountroot
cups networking rsyslog unattended-upgrades
cups-browsed ondemand saned urandom
dbus pppd-dns sendsigs x11-common
root@server1:~# cd /etc/rc6.d/ && ls -l
total 4
lrwxrwxrwx 1 root root 29 Dec 9 02:07 K10unattended-upgrades -> ../init.d/unattended-upgrades
lrwxrwxrwx 1 root root 20 Dec 9 02:07 K20kerneloops -> ../init.d/kerneloops
lrwxrwxrwx 1 root root 15 Dec 9 02:07 K20rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root 27 Dec 9 02:07 K20speech-dispatcher -> ../init.d/speech-dispatcher
-rw-r--r-- 1 root root 351 Mar 12 2014 README
lrwxrwxrwx 1 root root 18 Dec 9 02:07 S20sendsigs -> ../init.d/sendsigs
lrwxrwxrwx 1 root root 17 Dec 9 02:07 S30urandom -> ../init.d/urandom
lrwxrwxrwx 1 root root 22 Dec 9 02:07 S31umountnfs.sh -> ../init.d/umountnfs.sh
lrwxrwxrwx 1 root root 18 Dec 9 02:07 S40umountfs -> ../init.d/umountfs
lrwxrwxrwx 1 root root 20 Dec 9 02:07 S60umountroot -> ../init.d/umountroot
lrwxrwxrwx 1 root root 16 Dec 9 02:07 S90reboot -> ../init.d/reboot
The configuration files of native upstart services are in /etc/init/ directory :
as an example let take a look at inside of ufw.conf:
# ufw - Uncomplicated Firewall
#
# The Uncomplicated Firewall is a front-end for iptables, to make managing a
# Netfilter firewall easier.
description "Uncomplicated firewall"
# Make sure we start before an interface receives traffic
start on (starting network-interface
or starting network-manager
or starting networking)
stop on runlevel [!023456]
console output
pre-start exec /lib/ufw/ufw-init start quiet
post-stop exec /lib/ufw/ufw-init stop
How upstart keeps backward compatibility and live beside old SysV? the secret is inside /etc/init/rcS.conf :
# rcS - System V single-user mode compatibility
#
# This task handles the old System V-style single-user mode, this is
# distinct from the other runlevels since running the rc script would
# be bad.
description "System V single-user mode compatibility"
author "Scott James Remnant <scott@netsplit.com>"
start on runlevel S
stop on runlevel [!S]
console owner
exec /sbin/sulogin
post-stop script
# Don't switch runlevels if we were stopped by an event, since that
# means we're already switching runlevels
if [ -n "${UPSTART_STOP_EVENTS}" ]
then
exit 0
fi
# Switch, passing a magic flag
start --no-wait rc-sysinit FROM_SINGLE_USER_MODE=y
end script
bu using rcS.conf upstart can run SysV scripts which haven't been developed for upstart natively .
Systemd
Systemd is used in all modern linuxes. Its a new way of starting linux services but that is not all. The idea of Systemd project is scary! Systemd developers have this idea to create Systemd OS which runs on linux OS. So it seems usual if we know it has its own tool which work like cron, or fstab, rsyslog ... .
In Systemd world we have tragets and unit files, targets is like our goal which we want to reach. But for reaching targets we need to specified what ever is needed to be load in unit files. As Systemd doing variety of thing, there for different types of unit files exists:
service : unit file to start a service considering its dependencies
mount : replace the mount in /etc/fstab
timer : replacement for cron
automount : mount a directory when needed
target : as we said a bunch of unit files :), target is an end point, tragets can be used as runlevel .
path :observ activities on a path and start a service associated with that
...
/usr/lib/systemd/system and /etc/systemd/system
Orginal systemd ubit files are in /usr/lib/systemd/system directory but they should not be modified by administrators. Modification should be done in /etc/systemd/system and its good to know that they are linked. Inorde to do any modification we should copy service files from /usr/lib/systemd/system to /etc/systemd/system and then set our settings, we use Ubuntu 16: