Powered By GitBook
101.3. Change runlevels / boot targets and shutdown or reboot system

101.3 Change runlevels / boot targets and shutdown or reboot system

Weight:3
Description: Candidates should be able to manage the SysVinit runlevel or systemd boot target of the system. This objective includes changing to single user mode, shutdown or rebooting the system. Candidates should be able to alert users before switching runlevels / boot targets and properly terminate processes. This objective also includes setting the default SysVinit runlevel or systemd boot target. It also includes awareness of Upstart as an alternative to SysVinit or systemd.
Key Knowledge Areas:
    Set the default runlevel or boot target
    Change between runlevels / boot targets including single user mode
    Shutdown and reboot from the command line
    Alert users before switching runlevels / boot targets or other major system events
    Properly terminate processes
Terms and Utilities:
    /etc/inittab
    shutdown
    init
    /etc/init.d/
    telinit
    systemd
    systemctl
    /etc/systemd/
    /usr/lib/systemd/
    wall
In previous lesson we explain the sequence of system boot in liunx, then we get introduced to Sysv, Upstart and system as different service managers in linux world. In this lesson we learn how to take control over our linux system services using these service managers.
Lets start with SysV. SysV is is nothing more than many executable scripts which are run after init process. How we can define which service should be run when computer starts ? I'm glad you asked. It seems that huge text configuration files would be needed, there is where runlevels come to play.

runlevels

A runlevel is one of the modes that a Unix -based operating system will run in. Each runlevel has a certain number of services stopped or started, giving the user control over the behavior of the machine. runlevels avoid of having a few large files to edit by hand.
Conventionally, seven runlevels exist, numbered from zero to six. And there are some differences between Debian and RedHat based systems:
Runlevel
Debian
RedHat
0
Halt
Halt
1
Single User Mode
Single User Mode
2
Full,Multi-User,GUI
Multi-User, No Net
3
Nothing
Multi-User, with Net, No GUI
4
Nothing
Not used
5
Nothing
Full,Multi-User,GUI
6
Reboot
Reboot
CentsOS 5 was the last version which used SysV and from CentOS 7 Systemd is used.

runlevel

To find the current and previous runlevels , the runlevel command is used:
1
[[email protected] ~]# runlevel
2
N 5
Copied!
In the above output, the letter ‘N’ indicates that the runlevel has not been changed since the system was booted. And, 5 is the current runlevel.
Now that we now about runlevels, How we can switch between them? well just tell 'init' what runlevel you like.

telinit

telinit is used to change the SysV system runlevel.
1
telinit RUNLEVEL
Copied!
Lets try it on a CentOS5 machine:
1
[[email protected] ~]# runlevel
2
N 5
3
[[email protected] ~]# telinit 3
Copied!
and system goes to runlevel 3 lets go back to runlevel 5:
1
[[email protected] ~]# telinit 5
Copied!

init

init (as a command) is a process control initialization like telinit.
1
[[email protected] ~]# init 3
Copied!

telinit vs init

telinit is a smaller tool that informs init when it needs to switch runlevels. So we can use "telinit" to "tell init" that it needs to switch runlevel. telinit is actually linked to init command and it is possible to use init command instead but it is not recommanded.
1
[[email protected] ~]# ls -l /sbin/telinit
2
lrwxrwxrwx 1 root root 4 Aug 26 23:20 /sbin/telinit -> init
Copied!
There are several ways to change runlevels. To make a permanent change, we can edit /etc/inittab and change the default level that we just saw above.

/etc/inttab

After the Linux kernel has booted, the init program reads the /etc/inittab file to determine the behavior for each runlevel. Unless the user specifies another value as a kernel boot parameter, the system will attempt to enter (start) the default runlevel.(CentOs5)
1
[[email protected] ~]# cat /etc/inittab
2
#
3
# inittab This file describes how the INIT process should set up
4
# the system in a certain run-level.
5
#
6
# Author: Miquel van Smoorenburg, <[email protected]>
7
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
8
#
9
10
# Default runlevel. The runlevels used by RHS are:
11
# 0 - halt (Do NOT set initdefault to this)
12
# 1 - Single user mode
13
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
14
# 3 - Full multiuser mode
15
# 4 - unused
16
# 5 - X11
17
# 6 - reboot (Do NOT set initdefault to this)
18
#
19
id:5:initdefault:
20
21
# System initialization.
22
si::sysinit:/etc/rc.d/rc.sysinit
23
24
l0:0:wait:/etc/rc.d/rc 0
25
l1:1:wait:/etc/rc.d/rc 1
26
l2:2:wait:/etc/rc.d/rc 2
27
l3:3:wait:/etc/rc.d/rc 3
28
l4:4:wait:/etc/rc.d/rc 4
29
l5:5:wait:/etc/rc.d/rc 5
30
l6:6:wait:/etc/rc.d/rc 6
31
32
# Trap CTRL-ALT-DELETE
33
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
34
35
# When our UPS tells us power has failed, assume we have a few minutes
36
# of power left. Schedule a shutdown for 2 minutes from now.
37
# This does, of course, assume you have powerd installed and your
38
# UPS connected and working correctly.
39
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
40
41
# If power was restored before the shutdown kicked in, cancel it.
42
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
43
44
45
# Run gettys in standard runlevels
46
1:2345:respawn:/sbin/mingetty tty1
47
2:2345:respawn:/sbin/mingetty tty2
48
3:2345:respawn:/sbin/mingetty tty3
49
4:2345:respawn:/sbin/mingetty tty4
50
5:2345:respawn:/sbin/mingetty tty5
51
6:2345:respawn:/sbin/mingetty tty6
52
53
# Run xdm in runlevel 5
54
x:5:respawn:/etc/X11/prefdm -nodaemon
Copied!
the default runlevel is determined from theid:entry in /etc/inittab. How run levels are set up by default and how they are configured depends in part on the particular distribution you are running.
The format of each line in inittab file is as follows:
id:runlevel:action:process
Here is a description of these fields:
    id (identification code) – consists of a sequence of one to four characters that identifies its function.
    runlevels – lists the run levels to which this entry applies.
    action – specific codes in this field tell init how to treat the process. Possible values include: initdefault, sysinit, boot, bootwait, wait, and respawn.
    process – defines the command or script to execute.
Now lets see how SysV implements the concept of run levels.

/etc/init.d

/etc/init.d contains scripts used by the System V init tools (SysVinit).
1
[[email protected] ~]# ls /etc/init.d/
2
acpid hidd netconsole rpcidmapd
3
anacron httpd netfs rpcsvcgssd
4
atd ip6tables netplugd saslauthd
5
auditd ipmi network sendmail
6
autofs iptables NetworkManager single
7
avahi-daemon irda nfs smartd
8
avahi-dnsconfd irqbalance nfslock sshd
9
bluetooth iscsi nscd svnserve
10
conman iscsid ntpd syslog
11
cpuspeed killall oddjobd vmware-tools
12
crond krb524 pand vmware-tools-thinprint
13
cups kudzu pcscd vncserver
14
cups-config-daemon ldap portmap wdaemon
15
dnsmasq lvm2-monitor psacct wpa_supplicant
16
dund mcstrans rawdevices xfs
17
firstboot mdmonitor rdisc ypbind
18
functions mdmpd readahead_early yum-updatesd
19
gpm messagebus readahead_later
20
haldaemon microcode_ctl restorecond
21
halt multipathd rpcgssd
Copied!
As we said, in SysV, init program is the first process that is run and consequently some infrastructure services are started. Files in /etc/init.d are shell scripts that respond to start, stop, restart, and (when supported) reload commands to manage a particular service. But how SysV determine which services inside /etc/init.d should be started or stopped depend on default runlevel ?Lets draw a picture:

/etc/rc.d/

SysV uses grouping. Scripts of each runlevel are grouped and placed in /etc/rc{runlevel}.d/ where runlevel is the runlevel.
As many services might be existed in different runlevels, the real script files are hold in /etc/init.d and /etc/rc{runlevel}.d/just point to required ones.
1
[[email protected] etc]# ls | grep rc.
2
rc0.d
3
rc1.d
4
rc2.d
5
rc3.d
6
rc4.d
7
rc5.d
8
rc6.d
9
rc.d
10
rc.local
11
rc.sysinit
Copied!
Lets take a look at rc.5 for example:
1
[[email protected] etc]# ls -l rc5.d/
2
total 288
3
lrwxrwxrwx 1 root root 17 Aug 26 23:21 K01dnsmasq -> ../init.d/dnsmasq
4
lrwxrwxrwx 1 root root 24 Aug 26 23:21 K02avahi-dnsconfd -> ../init.d/avahi-dnsconfd
5
lrwxrwxrwx 1 root root 24 Aug 26 23:23 K02NetworkManager -> ../init.d/NetworkManager
6
lrwxrwxrwx 1 root root 17 Aug 26 23:23 K02oddjobd -> ../init.d/oddjobd
7
lrwxrwxrwx 1 root root 16 Aug 26 23:20 K05conman -> ../init.d/conman
8
lrwxrwxrwx 1 root root 19 Aug 26 23:21 K05saslauthd -> ../init.d/saslauthd
9
lrwxrwxrwx 1 root root 17 Aug 26 23:20 K05wdaemon -> ../init.d/wdaemon
10
lrwxrwxrwx 1 root root 16 Aug 26 23:20 K10psacct -> ../init.d/psacct
11
lrwxrwxrwx 1 root root 15 Aug 26 23:21 K15httpd -> ../init.d/httpd
12
lrwxrwxrwx 1 root root 18 Aug 26 23:22 K15svnserve -> ../init.d/svnserve
13
lrwxrwxrwx 1 root root 13 Aug 26 23:23 K20nfs -> ../init.d/nfs
14
lrwxrwxrwx 1 root root 14 Aug 26 23:23 K24irda -> ../init.d/irda
15
<output has been truncated>
16
lrwxrwxrwx 1 root root 23 Aug 26 23:20 S00microcode_ctl -> ../init.d/microcode_ctl
17
lrwxrwxrwx 1 root root 22 Aug 26 23:28 S03vmware-tools -> ../init.d/vmware-tools
18
lrwxrwxrwx 1 root root 25 Aug 26 23:20 S04readahead_early -> ../init.d/readahead_early
19
lrwxrwxrwx 1 root root 15 Aug 26 23:23 S05kudzu -> ../init.d/kudzu
20
lrwxrwxrwx 1 root root 16 Aug 26 23:20 S07iscsid -> ../init.d/iscsid
21
lrwxrwxrwx 1 root root 19 Aug 26 23:19 S08ip6tables -> ../init.d/ip6tables
22
lrwxrwxrwx 1 root root 18 Aug 26 23:19 S08iptables -> ../init.d/iptables
23
lrwxrwxrwx 1 root root 18 Aug 26 23:21 S08mcstrans -> ../init.d/mcstrans
24
lrwxrwxrwx 1 root root 17 Aug 26 23:21 S10network -> ../init.d/network
25
lrwxrwxrwx 1 root root 16 Aug 26 23:20 S11auditd -> ../init.d/auditd
26
lrwxrwxrwx 1 root root 21 Aug 26 23:21 S12restorecond -> ../init.d/restorecond
27
lrwxrwxrwx 1 root root 16 Aug 26 23:21 S12syslog -> ../init.d/syslog
28
lrwxrwxrwx 1 root root 18 Aug 26 23:19 S13cpuspeed -> ../init.d/cpuspeed
29
lrwxrwxrwx 1 root root 20 Aug 26 23:21 S13irqbalance -> ../init.d/irqbalance
30
lrwxrwxrwx 1 root root 15 Aug 26 23:20 S13iscsi -> ../init.d/iscsi
31
lrwxrwxrwx 1 root root 17 Aug 26 23:21 S13portmap -> ../init.d/portmap
32
lrwxrwxrwx 1 root root 17 Aug 26 23:23 S14nfslock -> ../init.d/nfslock
33
lrwxrwxrwx 1 root root 19 Aug 26 23:21 S15mdmonitor -> ../init.d/mdmonitor
34
<output has been truncated>
Copied!
Each script in each runlevel is run with its startup or shutdown functions depending on if that runlevel is going up or going down. S means starting script and K shows that it is a killing script .The sequence of actions is defined by the numbers. try cat command to see what is inside :
1
[[email protected] rc5.d]# cat S55sshd
2
#!/bin/bash
3
#
4
# Init file for OpenSSH server daemon
5
#
6
# chkconfig: 2345 55 25
7
# description: OpenSSH server daemon
8
#
9
# processname: sshd
10
# config: /etc/ssh/ssh_host_key
11
# config: /etc/ssh/ssh_host_key.pub
12
# config: /etc/ssh/ssh_random_seed
13
# config: /etc/ssh/sshd_config
14
# pidfile: /var/run/sshd.pid
15
16
# source function library
17
. /etc/rc.d/init.d/functions
18
19
# pull in sysconfig settings
20
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
21
22
RETVAL=0
23
prog="sshd"
24
25
# Some functions to make the below more readable
26
KEYGEN=/usr/bin/ssh-keygen
27
SSHD=/usr/sbin/sshd
28
RSA1_KEY=/etc/ssh/ssh_host_key
29
RSA_KEY=/etc/ssh/ssh_host_rsa_key
30
DSA_KEY=/etc/ssh/ssh_host_dsa_key
31
PID_FILE=/var/run/sshd.pid
32
33
runlevel=$(set -- $(runlevel); eval "echo \$#" )
34
35
do_rsa1_keygen() {
36
if [ ! -s $RSA1_KEY ]; then
37
echo -n quot;Generating SSH1 RSA host key: "
38
rm -f $RSA1_KEY
39
if $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
40
chmod 600 $RSA1_KEY
41
chmod 644 $RSA1_KEY.pub
42
if [ -x /sbin/restorecon ]; then
43
/sbin/restorecon $RSA1_KEY.pub
44
fi
45
success quot;RSA1 key generation"
46
echo
47
else
48
failure quot;RSA1 key generation"
49
echo
50
exit 1
51
fi
52
fi
53
}
54
55
do_rsa_keygen() {
56
if [ ! -s $RSA_KEY ]; then
57
echo -n quot;Generating SSH2 RSA host key: "
58
rm -f $RSA_KEY
59
if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
60
chmod 600 $RSA_KEY
61
chmod 644 $RSA_KEY.pub
62
if [ -x /sbin/restorecon ]; then
63
/sbin/restorecon $RSA_KEY.pub
64
fi
65
success quot;RSA key generation"
66
echo
67
else
68
failure quot;RSA key generation"
69
echo
70
exit 1
71
fi
72
fi
73
}
74
75
do_dsa_keygen() {
76
if [ ! -s $DSA_KEY ]; then
77
echo -n quot;Generating SSH2 DSA host key: "
78
rm -f $DSA_KEY
79
if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
80
chmod 600 $DSA_KEY
81
chmod 644 $DSA_KEY.pub
82
if [ -x /sbin/restorecon ]; then
83
/sbin/restorecon $DSA_KEY.pub
84
fi
85
success quot;DSA key generation"
86
echo
87
else
88
failure quot;DSA key generation"
89
echo
90
exit 1
91
fi
92
fi
93
}
94
95
do_restart_sanity_check()
96
{
97
$SSHD -t
98
RETVAL=$?
99
if [ ! "$RETVAL" = 0 ]; then
100
failure quot;Configuration file or keys are invalid"
101
echo
102
fi
103
}
104
105
start()
106
{
107
# Create keys if necessary
108
do_rsa1_keygen
109
do_rsa_keygen
110
do_dsa_keygen
111
112
cp -af /etc/localtime /var/empty/sshd/etc
113
114
echo -n quot;Starting $prog: "
115
$SSHD $OPTIONS && success || failure
116
RETVAL=$?
117
[ "$RETVAL" = 0 ] && touch /var/lock/subsys/sshd
118
echo
119
}
120
121
stop()
122
{
123
echo -n quot;Stopping $prog: "
124
if [ -n "`pidfileofproc $SSHD`" ] ; then
125
killproc $SSHD
126
else
127
failure quot;Stopping $prog"
128
fi
129
RETVAL=$?
130
# if we are in halt or reboot runlevel kill all running sessions
131
# so the TCP connections are closed cleanly
132
if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
133
killall $prog 2>/dev/null
134
fi
135
[ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/sshd
136
echo
137
}
138
139
reload()
140
{
141
echo -n quot;Reloading $prog: "
142
if [ -n "`pidfileofproc $SSHD`" ] ; then
143
killproc $SSHD -HUP
144
else
145
failure quot;Reloading $prog"
146
fi
147
RETVAL=$?
148
echo
149
}
150
151
case "$1" in
152
start)
153
start
154
;;
155
stop)
156
stop
157
;;
158
restart)
159
stop
160
start
161
;;
162
reload)
163
reload
164
;;
165
condrestart)
166
if [ -f /var/lock/subsys/sshd ] ; then
167
do_restart_sanity_check
168
if [ "$RETVAL" = 0 ] ; then
169
stop
170
# avoid race
171
sleep 3
172
start
173
fi
174
fi
175
;;
176
status)
177
status -p $PID_FILE openssh-daemon
178
RETVAL=$?
179
;;
180
*)
181
echo quot;Usage: $0 {start|stop|restart|reload|condrestart|status}"
182
RETVAL=1
183
esac
184
exit $RETVAL
Copied!
And all of these places were places that SysV uses to manage scripts and runlevels. In Sysv system we can manage services with servicecommand andchkconfigcommand to define how and when services are started. We will talk about them later.
rc.local
But what about /etc/rc.local ? This file runs after all other init level scripts have run, so it's safe to put various commands that you want to have issued upon startup.This is also a good place to place "troubleshooting" scripts in. But do not forget rc.local may not work properly in Upstart and Systemd. Test it, search it and do required configuration before using in production environment.
Lets go back to our topic and get familiar with runlevels equivalent in systemd. Although /etc/inittab still exist in Systemd systems but that is not part of configuration, we have something called "Targets".

systemd targets

Like runlevels there are some modes in systemd system that our system can run in, systemd runlevels are referred to as "targets". "targets" are described as a collection of services. Look at the equivalents:
1
┌─────────┬───────────────────┐
2
│Runlevel │ Target │
3
├─────────┼───────────────────┤
4
│0 │ poweroff.target │
5
├─────────┼───────────────────┤
6
│1 │ rescue.target │
7
├─────────┼───────────────────┤
8
│2, 3, 4 │ multi-user.target │
9
├─────────┼───────────────────┤
10
│5 │ graphical.target │
11
├─────────┼───────────────────┤
12
│6 │ reboot.target │
13
└─────────┴───────────────────┘
Copied!
In order to switch between Boot Targets we use systemctl tool.

systemctl

Systemctl is a systemd utility which is responsible for Controlling the systemd system and service manager. It can does lots of thing but what we need here and use it for is changing boot targets.
To see the current boot target use systemctl get-default command:
1
[[email protected] ~]# systemctl get-default
2
graphical.target
Copied!
To change the target use systemctl isolate xxxxx.target , for example:
1
[[email protected] ~]# systemctl isolate rescue.target
Copied!
1
[[email protected] ~]# systemctl isolate multi-user.target
Copied!
To set the default target, run systemctl set-default xxxxxx.target command :
1
[[email protected] ~]# systemctl set-default graphical.target
2
Removed symlink /etc/systemd/system/default.target.
3
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
Copied!
But how systemd knows what to do and how to do things ? We have something called "Unit". The concept of "Unit Files" replaces the SysV init scripts for services.

systemd unit files

There are different types of unit files and the best way to describe unit files, is 'that is a thing which should be started'.Yes that is a thing because there are different kinds of unit files. Each unit file is a simple text file describing a unit, what it does, what needs to run before or afterward, and other details
Unit files can be stored in a few different places on your system. systemd looks for system unit files in this order:
    1.
    /etc/systemd/system:directory stores unit files that extend a service. This directory will take precedence over unit files located anywhere else in the system.
    2.
    /run/systemd/system:directory is the runtime location for unit files.
    3.
    /usr/lib/systemd/system:directory is the default location where unit files are installed by packages. Unit files in the default directory should not be altered.
Unit files in the earlier directories override later ones. Lets take a look at them:

1./etc/systemd/system

1
[[email protected] ~]# ls -l /etc/systemd/system
2
total 4
3
drwxr-xr-x. 2 root root 31 Oct 28 2017 basic.target.wants
4
drwxr-xr-x. 2 root root 31 Oct 28 2017 bluetooth.target.wants
5
lrwxrwxrwx. 1 root root 41 Oct 28 2017 dbus-org.bluez.service -> /usr/lib/systemd/system/bluetooth.service
6
lrwxrwxrwx. 1 root root 41 Oct 28 2017 dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
7
lrwxrwxrwx. 1 root root 44 Oct 28 2017 dbus-org.freedesktop.Avahi.service -> /usr/lib/systemd/system/avahi-daemon.service
8
lrwxrwxrwx. 1 root root 44 Oct 28 2017 dbus-org.freedesktop.ModemManager1.service -> /usr/lib/systemd/system/ModemManager.service
9
lrwxrwxrwx. 1 root root 46 Oct 28 2017 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
10
lrwxrwxrwx. 1 root root 57 Oct 28 2017 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
11
lrwxrwxrwx. 1 root root 36 Oct 28 2017 default.target -> /lib/systemd/system/graphical.target
12
drwxr-xr-x. 2 root root 87 Oct 28 2017 default.target.wants
13
drwxr-xr-x. 2 root root 38 Oct 28 2017 dev-virtio\x2dports-org.qemu.guest_agent.0.device.wants
14
lrwxrwxrwx. 1 root root 35 Oct 28 2017 display-manager.service -> /usr/lib/systemd/system/gdm.service
15
drwxr-xr-x. 2 root root 32 Oct 28 2017 getty.target.wants
16
drwxr-xr-x. 2 root root 65 Oct 28 2017 graphical.target.wants
17
drwxr-xr-x. 2 root root 4096 Oct 28 2017 multi-user.target.wants
18
drwxr-xr-x. 2 root root 26 Oct 28 2017 printer.target.wants
19
drwxr-xr-x. 2 root root 31 Oct 28 2017 remote-fs.target.wants
20
drwxr-xr-x. 2 root root 188 Oct 28 2017 sockets.target.wants
21
drwxr-xr-x. 2 root root 36 Oct 28 2017 spice-vdagentd.target.wants
22
drwxr-xr-x. 2 root root 26 Oct 28 2017 sssd.service.d
23
drwxr-xr-x. 2 root root 169 Oct 28 2017 sysinit.target.wants
24
drwxr-xr-x. 2 root root 44 Oct 28 2017 system-update.target.wants
25
drwxr-xr-x. 2 root root 29 Oct 28 2017 vmtoolsd.service.requires
Copied!

2./run/systemd/system

1
[[email protected] ~]# ls -l /run/systemd/system/
2
total 8
3
-rw-r--r-- 1 root root 17 Jan 2 2019 session-1.scope
4
drwxr-xr-x 2 root root 160 Jan 2 2019 session-1.scope.d
5
-rw-r--r-- 1 root root 17 Jan 2 2019 user-1000.slice
6
drwxr-xr-x 2 root root 120 Jan 2 2019 user-1000.slice.d
Copied!

3./usr/lib/systemd/system

1
[[email protected] ~]# ls /usr/lib/systemd/system
2
3
<output has been trancuated>
4
5
6
cryptsetup-pre.target runlevel0.target
7
cryptsetup.target runlevel1.target
8
ctrl-alt-del.target runlevel1.target.wants
9
cups-browsed.service runlevel2.target
10
cups.path runlevel2.target.wants
11
cups.service runlevel3.target
12
cups.socket runlevel3.target.wants
13
dbus-org.freedesktop.hostname1.service runlevel4.target
14
dbus-org.freedesktop.import1.service runlevel4.target.wants
15
dbus-org.freedesktop.locale1.service runlevel5.target
16
dbus-org.freedesktop.login1.service runlevel5.target.wants
17
dbus-org.freedesktop.machine1.service runlevel6.target
18
19
<output has been trancuated>
20
21
nfs-rquotad.service umount.target
22
nfs-secure.service upower.service
23
nfs-server.service [email protected]
24
nfs.service usbmuxd.service
25
nfs-utils.service user.slice
26
nss-lookup.target var-lib-nfs-rpc_pipefs.mount
27
nss-user-lookup.target vgauthd.service
28
ntpdate.service virt-guest-shutdown.target
29
ntpd.service virtlockd.service
30
numad.service virtlockd.socket
31
oddjobd.service virtlogd.service
32
packagekit-offline-update.service virtlogd.socket
33
packagekit.service vmtoolsd.service
34
paths.target [email protected]
35
plymouth-halt.service wpa_supplicant.service
36
plymouth-kexec.service zram.service
37
plymouth-poweroff.service
Copied!
lets follow one of target unit files.Try ls -al *.target to see all of target files. As an instance default.target:
1
[[email protected] ~]# ls -al /usr/lib/systemd/system/default.target
2
lrwxrwxrwx. 1 root root 16 Oct 28 2017 /usr/lib/systemd/system/default.target -> graphical.target
Copied!
Lets take a look at that graphical.target:
1
[[email protected] ~]# cat /usr/lib/systemd/system/graphical.target
2
# This file is part of systemd.
3
#
4
# systemd is free software; you can redistribute it and/or modify it
5
# under the terms of the GNU Lesser General Public License as published by
6
# the Free Software Foundation; either version 2.1 of the License, or
7
# (at your option) any later version.
8
9
[Unit]
10
Description=Graphical Interface
11
Documentation=man:systemd.special(7)
12
Requires=multi-user.target
13
Wants=display-manager.service
14
Conflicts=rescue.service rescue.target
15
After=multi-user.target rescue.service rescue.target display-manager.service
16
AllowIsolate=yes
Copied!
And what it requires is multi-user.target, lets see:
1
[[email protected] ~]# cat /usr/lib/systemd/system/multi-user.target
2
# This file is part of systemd.
3
#
4
# systemd is free software; you can redistribute it and/or modify it
5
# under the terms of the GNU Lesser General Public License as published by
6
# the Free Software Foundation; either version 2.1 of the License, or
7
# (at your option) any later version.
8
9
[Unit]
10
Description=Multi-User System
11
Documentation=man:systemd.special(7)
12
Requires=basic.target
13
Conflicts=rescue.service rescue.target
14
After=basic.target rescue.service rescue.target
15
AllowIsolate=yes
Copied!
and it requires basic.target :
1
[[email protected] ~]# cat /usr/lib/systemd/system/basic.target
2
# This file is part of systemd.
3
#
4
# systemd is free software; you can redistribute it and/or modify it
5
# under the terms of the GNU Lesser General Public License as published by
6
# the Free Software Foundation; either version 2.1 of the License, or
7
# (at your option) any later version.
8
9
[Unit]
10
Description=Basic System
11
Documentation=man:systemd.special(7)
12
13
Requires=sysinit.target
14
After=sysinit.target
15
Wants=sockets.target timers.target paths.target slices.target
16
After=sockets.target paths.target slices.target
Copied!
Finally sysinit.target :
1
[[email protected] ~]# cat /usr/lib/systemd/system/sysinit.target
2
# This file is part of systemd.
3
#
4
# systemd is free software; you can redistribute it and/or modify it
5
# under the terms of the GNU Lesser General Public License as published by
6
# the Free Software Foundation; either version 2.1 of the License, or
7
# (at your option) any later version.
8
9
[Unit]
10
Description=System Initialization
11
Documentation=man:systemd.special(7)
12
Conflicts=emergency.service emergency.target
13
Wants=local-fs.target swap.target
14
After=local-fs.target swap.target emergency.service emergency.target
Copied!
And sysint.target does not require any thing. It seems that we have reached top level target in our tree and we can go back.
Using unit files beside Targets concept make Systemd more flexible in comparison to SysV.

wall

There are times when multiple users are logged in to a server computer, and we need to, say, restart the server to perform some maintenance task. Of course, the correct way is to inform all those who are logged in about the maintenance activity.
wall (an abbreviation of write to all) is a Unix command-line utility that displays the contents of a file or standard input to all logged-in users. It is typically used by root to send out shutting down message to all users just before poweroff.(Ubuntu16)
1
wall [-n] [-t timeout] [-g group] [message | file]
Copied!
1
OPTIONS
2
-n, --nobanner
3
Suppress the banner.
4
5
-t, --timeout timeout
6
Abandon the write attempt to the terminals after timeout
7
seconds. This timeout must be a positive integer. The
8
default value is 300 seconds, which is a legacy from the time
9
when people ran terminals over modem lines.
10
11
-g, --group group
12
Limit printing message to members of group defined as a group
13
argument. The argument can be group name or GID.
14
15
-V, --version
16
Display version information and exit.
17
18
-h, --help
19
Display help text and exit.
Copied!
Lets try sending out "we are going down" message:
1
[[email protected] ~]# wall "we are going down"
2
3
Broadcast message from [email protected] (pts/1) (Sun Nov 11 00:46:58 2018):
4
5
we are going down
Copied!
and what will user1 receive:
2
Broadcast message from [email protected] (pts/1) (Sun Nov 11 00:46:58 2018):
3
4
we are going down
Copied!
options might not work is some old distributions.

shutdown

shutdown - Halt, power-off or reboot the machine
1
shutdown [OPTIONS...] [TIME] [WALL...]
Copied!
1
OPTIONS
2
The following options are understood:
3
4
--help
5
Print a short help text and exit.
6
7
-H, --halt
8
Halt the machine.
9
10
-P, --poweroff
11
Power-off the machine (the default).
12
13
-r, --reboot
14
Reboot the machine.
15
16
-h
17
Equivalent to --poweroff, unless --halt is specified.
18
19
-k
20
Do not halt, power-off, reboot, just write wall message.
21
22
--no-wall
23
Do not send wall message before halt, power-off, reboot.
24
25
-c
26
Cancel a pending shutdown. This may be used cancel the effect of
27
an invocation of shutdown with a time argument that is not "+0"
28
or "now".
29
-t sec
30
Tell init to wait sec seconds between sending processes the warning
31
and the kill signal, before changing to another runlevel.
32
[TIME]
33
The time argument specifies when to perform the shutdown operation.
34
35
[WALL]
36
A message to be sent to all users, along with the standard shutdown notification.
Copied!
shutdown does its job by signalling the init process, asking it to change the runlevel. According to previous title, runlevel 0 is used to halt the system, runlevel 6 is used to reboot the system, and runlevel 1 is used to put the system into a state where administrative tasks can be performed (single-user mode).
1
[email protected]:~# shutdown -r -t 15 +5 "Server will restart in 5 minutes. Please save your work."
2
Shutdown scheduled for Sun 2018-11-11 03:19:56 PST, use 'shutdown -c' to cancel.
3
[email protected]:~# shutdown -c
Copied!
We may specify a time string (which is usually “now” or “hh:mm” for hour/minutes):
1
[email protected]:~# shutdown -r 10:10 "Server will be rebooted at 10:10am"
2
Shutdown scheduled for Tue 2018-11-13 10:10:00 PST, use 'shutdown -c' to cancel.
3
[email protected]:~# shutdown -c
Copied!

halt vs poweroff ! it's a bit historical

    halt was used before ACPI (Advanced Configuration and Power Interface)which today will turn off the power for us. It would halt the system and then print a message to the effect of "it's ok to power off now". Back then there were physical on/off switches, rather than the combo ACPI controlled power button of modern computers.
    poweroff, naturally will halt the system and then call ACPI power off.
These days halt is smart enough to automatically call poweroff if ACPI is enabled. In fact, they are functionally equivalent now.

reboot

reboot command can be used to shutdown or reboot linux.
1
[[email protected] ~]# reboot --help
2
reboot [OPTIONS...] [ARG]
3
4
Reboot the system.
5
6
--help Show this help
7
--halt Halt the machine
8
-p --poweroff Switch off the machine
9
--reboot Reboot the machine
10
-f --force Force immediate halt/power-off/reboot
11
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
12
-d --no-wtmp Don't write wtmp record
13
--no-wall Don't send wall message before halt/power-off/reboot
Copied!
To reboot linux just call the reboot command directly without any options.
1
[[email protected] ~]# reboot
Copied!
This will perform a graceful shutdown and restart of the machine. This is what happens when we click restart from your menu.
-f option will forcefully reboot the machine. This is similar to pressing the power button of the CPU. No shutdown takes place. The system will reset instantly.

halt

The next command is the halt command. This can shutdown a system but has some other options:
1
[[email protected] ~]# halt --help
2
halt [OPTIONS...]
3
4
Halt the system.
5
6
--help Show this help
7
--halt Halt the machine
8
-p --poweroff Switch off the machine
9
--reboot Reboot the machine
10
-f --force Force immediate halt/power-off/reboot
11
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
12
-d --no-wtmp Don't write wtmp record
13
--no-wall Don't send wall message before halt/power-off/reboot
Copied!
the halt command also has force option but try not to use it, because it might put your system in an in consistant state.

poweroff

There is another command exactly same as the halt command. It does the same things and takes the same options.
1
[[email protected] ~]# poweroff --help
2
poweroff [OPTIONS...]
3
4
Power off the system.
5
6
--help Show this help
7
--halt Halt the machine
8
-p --poweroff Switch off the machine
9
--reboot Reboot the machine
10
-f --force Force immediate halt/power-off/reboot
11
-w --wtmp-only Don't halt/power-off/reboot, just write wtmp record
12
-d --no-wtmp Don't write wtmp record
13
--no-wall Don't send wall message before halt/power-off/reboot
Copied!
and we are done.
.
.
.
Sources:
Last modified 1yr ago