2. Exam Objectives
Key Knowledge Areas
Set the default runlevel.
Change between run levels including single user mode.
Shutdown and reboot from the command line.
Alert users before switching runlevels or other major system event.
Properly terminate processes.
Objective 1: System Architecture
Change runlevels and shutdown or reboot system Weight: 3
Terms and Utilities
3. Runlevels , shutdown, and reboot
Runlevels define what tasks are accomplished in the current state (runlevel) of a Linux
Every Linux system supports 3 basic runlevels, plus 1 or more runlevels for normal operation.
Basic Linux runlevels
0 Shut down (or halt) the system
1 Single-user mode; usually aliased as s or S
6 Reboot the system
Common Linux runlevels
2 Multiuser mode without networking
3 Multiuser mode with networking
5 Multiuser mode with networking and the X Window System
- Slackware uses runlevel 4 for full system running X Window system.
- Debian and Ubuntu, use a single runlevel for any multiuser mode, (runlevel 2)
consult documentation for the distribution
4. Runlevels , shutdown, and reboot
When system starts, default runlevel is determined from id: entry in /etc/inittab.
yourname@yourcomp~> grep "^id:" /etc/inittab
Entry for Fedora 8 or openSUSE 11.2, with runlevel 5 for the X Window System.
5. Runlevels , shutdown, and reboot
To make a permanent change in runlevel,
•edit /etc/inittab and change the default level
To make one boot in other runlevel
(exemple: to edit the Linux System, or to install or build kernel modules).
•At boot time by editing the kernel cmdline in GRUB or add a parameter to LILO;
•Use a single digit to specify desired runlevel.
each runlevel corresponds to scripts /etc/rc.d/rc?.d/S* (? for number 0 to 6).
When there’s change in runlevel, init runs a script which stops all extra processes in the
current runlevel, and starts required processes from the new runlevel
6. Runlevels , shutdown, and reboot
Ex 1: Changin runlevels at boot time - enter ‘e’ command to edit.
7. Runlevels , shutdown, and reboot
Ex 2: In menu with root, kernel, initrd - select kernel and press 'e' to edit
8. Runlevels , shutdown, and reboot
In end of line - add space and digit runlevel '3'.
Remove 'quiet' if you wish, or modify any other parameters if needed
9. Runlevels , shutdown, and reboot
To change runlevel without rebooting the system. The command used is telinit.
the command runlevel to show previous runlevel and current one.
If first output is 'N': the runlevel has not been changed since boot.
Ex: yourname@yourcomp~> runlevel
yourname@yourcomp~> telinit 4
Confirm the new runlevel after the change. Again with runlevel command
Ex: yourname@yourcomp~> runlevel
10. Runlevels , shutdown, and reboot
telinit and init
telinit is a symbolic link to init
Since init runs as PID 1 at boot time the init executables know if the command invoked on
prompt is the init or telinit and behaves accordingly.
As a symbolic link:
you may use init5 instead of telinit5 to switch to runlevel 5 or any other.
Ex: yourname@yourcomp~> ls -l $(which telinit)
lrwxrwxrwx 1 root root 4 2008-04-01 07:50 /sbin/telinit -> init
11. Runlevels , shutdown, and reboot
Single-user mode (runlevel 1)is selected to:
Recover a filesystem or database, or install and test new hardware.
•shell with a minimal system;
•Usually without networking;
•Without daemons running (or very few);
•Goes straight into a shell prompt as root, or must log with authentication.
Ex: yourname@yourcomp~> telinit 1
yourname@yourcomp~> telinit S
telinit 1 or init switch to Single-user mode for admin will stop multiuser activity.
Its a abrupt process - can cause users to lose work and processes to terminate abnormally.
12. Runlevels , shutdown, and reboot
Preferred method to shut down or reboot the system is the shutdown command
1.Sends warning message to all logged-in users and blocks any further logins.
2.Signals init to switch runlevels.
3. init process then sends all running processes a SIGTERM signal, giving them chance to save data and
4.After 5 sec, or another delay specified, init sends a SIGKILL signal to forcibly end each remaining process.
By default, shutdown switches to runlevel 1 (single-user mode).
Specify -h option to halt system, -r option to reboot.
Standard message is issued in addition to any message you specify
Shutdown time may be:
Absolute in hh:mm format, Relative time in n (n=number minutes until shutdown) +0= Now
Cancel delayed shutdown: press Ctrl-c or command shutdown -c.
13. Runlevels , shutdown, and reboot
Ex: yourname@yourcomp~# shutdown 5 File system recovery needed
Broadcast message from root (pts/1) (Tue Jan 4 08:05:24 2011):
File system recovery needed
The system is going DOWN to maintenance mode in 5 minutes!
yourname@yourcomp~# shutdown -r 10 Reloading updated kernel&
Broadcast message from root (pts/1) (Tue Jan 4 08:05:53 2011):
Reloading updated kernel
The system is going DOWN for reboot in 10 minutes!
shutdown -r 10 Reloading updated kernel
yourname@yourcomp~# shutdown -h 23:59&
[root@pinguino ~]# shutdown –c
+ Done shutdown -h 23:59
14. Runlevels , shutdown, and reboot
If time till shutdown exceeds 15 min., message is not sent until 15 minutes before the event.
yourname@yourcomp~# date;shutdown -t60 17 Time to do backups&
Tue Jan 4 08:12:55 EST 2011
Tue Jan 4 08:14:13 EST 2011
Broadcast message from root (pts/1) (Tue Jan 4 08:14:55 2011):
Time to do backups
The system is going DOWN to maintenance mode in 15 minutes!
If shutdown is canceled, use wall command to send a warning to all users alerting them
that the system is not going down.
15. Runlevels , shutdown, and reboot
Halt, reboot, and poweroff
halt command: halts the system
If any of these 3 commands are called when system is not in runlevel 0 or 6,
then the corresponding shutdown command will be invoked instead.
poweroff command: Is a symbolic link to halt command,
which halts the system and attempts to power it off.
reboot command: Is a symbolic link to halt command,
which halts the system and then reboots it.
16. Runlevels , shutdown, and reboot
When /sbin/init is started by kernel, it reads /etc/inittab for the startup sequence.
The lines in /etc/inittab determine the startup behaviour of your system.
Common inittab Actions
Respawn Restart process whenever it terminates. Usually used for getty processes, that monitors logins.
Wait Start process once when specified runlevel is entered and wait for its termination before init proceeds.
once Start the process once when the specified runlevel is entered.
Initdefault Specifies the runlevel to enter after system boot.
ctrlaltdel Execute associated process when init receives the SIGINT signal.
example: when someone on the system console presses CTRL-ALT-DEL.
Common initab Format [id:runlevels:action:process]
Id: unique identifier of one to four characters. Older versions limited to two characters.
runlevels: lists runlevels for which action for this id should be taken. If no runlevels are listed, do action for
action: describes which of several possible actions should be taken.
process tells which process, if any, should be run when action on this line is performed.
17. Runlevels , shutdown, and reboot
inittab This file describes how the INIT process should set up
# the system in a certain run-level.
Author: Miquel van Smoorenburg, <email@example.com>
# 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)
Before thinking runlevels, init runs the sysinit script.
# System initialization.
18. Runlevels , shutdown, and reboot
Part 2 l0:0:wait:/etc/rc.d/rc 0
For actual runlevel chosen (before in id:5:initdefault), a script is run – in this case /etc/rc.d/rc 5.
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
Trapping Ctrl-Alt-Delete for keyboard system reboot.
# Run gettys in standard runlevels
# Run xdm in runlevel 5
In runlevel 2 ,3 , 4, 5, init verifies if the /etc/inittab as the following programs running:
(mingetty handles terminal login, and mgetty handles modem dialin access)
19. Runlevels , shutdown, and reboot
Part 3 # 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"
Power failure settings.
20. Runlevels , shutdown, and reboot
Scripts used by init for:
starting the system; shutting down; changing runlevels
are typically in /etc/init.d or /etc/rc.d directory.
There’s a series of symbolic links in rcn.d directories. One directory for each runlevel n,
They control if the script is started and stopped when entering a runlevel.
man init and man inittab
21. Runlevels , shutdown, and reboot
Ex: yourname@yourcomp~# find /etc -path "*rc[0-9]*.d/???au*"
yourname@yourcomp~# cd /etc/rc.d/rc5.d
yourname@yourcomp rc5.d# ls -l ???a*
lrwxrwxrwx 1 root root 16 2008-04-07 11:29 S27auditd -> ../init.d/auditd
lrwxrwxrwx 1 root root 16 2008-04-01 07:51 S28autofs -> ../init.d/autofs
lrwxrwxrwx 1 root root 15 2008-04-01 14:03 S44acpid -> ../init.d/acpid
lrwxrwxrwx 1 root root 13 2008-04-01 07:50 S95atd -> ../init.d/atd
lrwxrwxrwx 1 root root 22 2008-04-01 07:54 S96avahi-daemon -> ../init.d/avahi-daemon
lrwxrwxrwx 1 root root 17 2008-11-17 13:40 S99anacron -> ../init.d/anacron
The links have: K (stopped) or S (Started); two-digit number (priority), name of service
Here audit and autofs services have Knn entries in all runlevels and Snn entries for runlevels 3 and 5.
S indicates that the service is started when that runlevel is entered, K indicates that it should be stopped.
nn component of link name indicates priority order in which the service should be started or stopped.
In example, audit is started before autofs, and it is stopped later.
22. Runlevels , shutdown, and reboot
Init vs. Upstart
Traditional method of booting a Linux system is based on UNIX System V init process.
For systems where everything is known and connected when the system starts it works well
Involves loading initial RAM disk (initrd)
passing control to a program called init (init program is installed as part of sysvinit package)
init program runs a series of scripts in a predefined order to bring up the system.
If something that is expected is not available, the init process typically waits for it.
Modern systems with hot-pluggable devices, network file systems, present new challenges.
waiting for hardware that may come available later, is not desirable.
An alternative initialization process was first introduced in Ubuntu 6.10 ("Edgy Eft") in 2006.
It’s called upstart
Has supplanted the init process in Ubuntu, Fedora, and others, with vestiges of init remaining and still in use
23. Runlevels , shutdown, and reboot
In contrast to the static init scripts the upstart system is driven by events.
Events triggered by hardware changes, started or stoped tasks, by processes on the system.
connecting a USB drive might cause udev service to send a block-device-added event,
which would cause a defined task to check /etc/fstab and mount the drive.
upstart initialization program replaces /sbin/init.
•Upstart jobs are defined in /etc/init directory and subdirectories.
•upstart system will process /etc/inittab and System V init scripts.
New Fedora /etc/inittab contains only id entry for initdefault;
New Ubuntu does not have /etc/inittab (must create one to specify a default runlevel).
Events used to trigger tasks or services (known as jobs).
24. Runlevels , shutdown, and reboot
Upstart has command initctl
Gives interaction with upstart init daemon.
(start or stop jobs, list jobs, get status of jobs, emit events, restart init process)
Ex: yourname@yourcomp~# initctl list
tty (/dev/tty3) start/running, process 1486
tty (/dev/tty2) start/running, process 1484
tty (/dev/tty6) start/running, process 1492
tty (/dev/tty5) start/running, process 1490
tty (/dev/tty4) start/running, process 1488
system-setup-keyboard start/running, process 1000
prefdm start/running, process 1479