5.2 Objective 2: Change Runlevels
and Shutdown or Reboot the System
As mentioned in the introduction, Linux and
many Unix systems share the concept of runlevels. This concept
specifies how a system is used by controlling which services
are running. For example, a system that operates a web server
program is configured to boot and initiate processing in a
runlevel designated for sharing data, at which point the web
server is started. However, the same system would not run the
web server in a runlevel used for emergency administration,
when all but the most basic services are shut down.
Runlevels are specified by the integers
through 6 as well as a few single characters. Runlevels and 6
are unusual in that they specify the transitional states of
shutdown and reboot, respectively. By instructing Linux to
enter runlevel 0, it begins a clean shutdown procedure.
Similarly, the use of runlevel 6 begins a reboot. The
remaining runlevels differ in meaning slightly among Linux
distributions and other Unix systems.
When a Linux system boots, the init process is responsible for
taking the system to the default runlevel, which is usually
either 3 or 5. Typical runlevel meanings are listed in Table
5-1.
Table 5-1. Typical Runlevels
0 |
Halt the system; runlevel is a special
transitional device used by administrators to shut down
the system quickly. This, of course, shouldn't be a
default runlevel, because the system would never come up
-- it would shutdown immediately when the kernel
launches the init
process. Also see runlevel 6. |
1, s, S |
Single-user mode, sometimes called
"maintenance mode." In this mode, system services such
as network interfaces, web servers, and file sharing are
not started. This mode is usually used for interactive
filesystem maintenance. |
2 |
Multiuser with no NFS file
sharing. |
3 |
Full multiuser mode. This is often used
as the default runlevel by the init process. |
4 |
Typically unused. |
5 |
Full multiuser mode with GUI login. In
runlevel 3, init does not
attempt to launch the X11 system. In runlevel 5, X11 is
started and the text-mode login is replaced with a GUI
login. This is often used as the default runlevel but
can cause problems if X11 cannot start for some reason.
|
6 |
Reboot the system; used by system
administrators. Just like runlevel 0, this is a
transitional device for administrators. It shouldn't be
a default runlevel because the system would eternally
reboot. |
5.2.1 Single-User Mode
Runlevel 1, the single-user runlevel, is a
bare-bones operating environment intended for system
maintenance. In single-user mode, remote logins are disabled,
networking is disabled, and most daemons are shut down. One
common reason you might be forced to use single-user mode is
to correct problems with a corrupt filesystem that the system
cannot handle automatically. Single-user mode is also used for
the installation of software and other system configuration
tasks that must be performed with no user activity.
If you wish to boot directly into single-user
mode, you may specify it at boot time at the LILO
prompt. After entering your kernel image name, give the
argument single or simply the
numeral 1. These arguments are
not interpreted as kernel arguments but are instead passed
along to the init process. For
example, if your kernel image is named "linux," these
commands would take the system to single-user mode, bypassing
the default: LILO: linux single
or: LILO: linux 1
To switch into single-user mode from another
runlevel, you can simply issue a runlevel change command with
init: # init 1
If others are using resources on the system,
they will be unpleasantly surprised, so be sure to give users
plenty of warning before doing this. To change from the
text-mode login to the X login screen, simply initiate the
X-enabled runlevel, usually 5: # init 5
Make certain that you understand the
use of the transitional runlevels and 6, the single-user
runlevel, and the difference between GUI and text login
configurations. You should also be prepared to
demonstrate how to change the runlevel of a running
system. |
If X is configured improperly, starting the X
login screen will lead to problems because X may die. It will
be automatically restarted, and this will go on in an infinite
loop until you reconfigure X. It is important to be sure X is
working correctly before attempting the GUI logon.
5.2.1.1 Overview of the /etc/rc.d
directory tree and the init process
By themselves, the runlevels listed in Table
5-1 don't mean much. It's what the init process does as a result of a
runlevel specification or change that affects the system. The
actions of init for each
runlevel are derived from Unix System V-style initialization
and are specified in a series of directories and script files
under /etc/rc.d.
When a Linux system starts, a number of
scripts in /etc/rc.d are used to initially configure
the system and switch among runlevels:
- rc.sysinit
-
This file is a script launched by init at boot time. It handles some
essential chores to ready the system for use, such as
mounting filesystems.
- rc.local
-
This file is a script that is called by
rc.sysinit. It contains local customizations
affecting system startup and provides an alternative to
modifying rc.sysinit. Many administrators prefer to
avoid changing rc.sysint because those changes could
be lost during a system upgrade. The contents of
rc.local are not lost in an upgrade.
- rc
-
This file is a script that is used to
change between runlevels.
The job of starting and stopping system
services such as web servers is handled by the files and
symbolic links in /etc/rc.d/init.d and by a series of
runlevel-specific directories, rc0.d through
rc6.d :
- init.d
-
This directory contains individual
startup/shutdown scripts for each service on the system. For
example, the script /etc/rc.d/init.d/httpd is a
Bourne shell script that safely starts or stops the Apache
web server. These scripts have a standard basic form and
take a single argument. Valid arguments are at least the
words start and stop. Additional arguments are
sometimes required by the script; examples are restart, status, and sometimes reload (to ask the service to
reconfigure itself without exiting). Administrators can use
these scripts directly to start and stop services. For
example, to restart Apache, an administrator could issue
commands like these: # /etc/rc.d/init.d/httpd stop
# /etc/rc.d/init.d/httpd start
or simply: # /etc/rc.d/init.d/httpd restart
Either form would completely shut down and
start up the web server. To ask Apache to remain running but
reread its configuration file, you might enter: # /etc/rc.d/init.d/httpd reload
This has the effect of sending the SIGHUP
signal to the running httpd
process, instructing it to initialize.
If you add a new service (a daemon,
intended to always run in the background), one of these
initialization files may be installed automatically for you.
In other cases, you may need to create one yourself, or as a
last resort, place startup commands in the rc.local
file.
- Directories rc0.d through
rc6.d
-
The initialization scripts in
/etc/rc.d/init.d are not directly executed by the
init process. Instead, each
of the directories /etc/rc.d/rc0.d through
rc6.d contain symbolic (soft) links to the
scripts in directory init.d. When the init process enters runlevel n, it examines all of the links in
the associated rcn.d directory. These
links are given special names in the form of
[K|S ][nn ][init.d_name ],
described as follows:
- K and S prefixes
-
The K and
S prefixes mean kill and start, respectively. A runlevel
implies a state in which certain services are running and
all others are not. The S
prefix is used for all services that are to be running
(started) for the runlevel.
The K prefix is used for
all other services, which should not be running.
- nn
-
Sequence number. This part of the link
name is a two-digit integer (with a leading zero, if
necessary). It specifies the relative order for services
to be started or stopped. The lowest number is the first
link executed by init, and
the largest number is the last. There are no hard-and-fast
rules for choosing these numbers, but it is important when
adding a new service to be sure that it starts after any other required services
are already running. If two services have an identical
start order number, the order is indeterminate but
probably alphabetical.
- init.d_name
-
By convention, the name of the script
being linked is used as the last part of the link name.
init does not use this
name, but excluding it makes things difficult for human
readers.
As an example, when init enters runlevel 3 at
boot time, all of the links with the S prefix in
/etc/init.d/rc3.d will be executed in the order given
by their sequence number. They will be run with the single
argument start to launch
their respective services. After the last of the scripts is
executed, the requirements for runlevel 3 are satisfied.
5.2.1.2 Setting the default
runlevel
To determine the default runlevel at boot
time, init reads the
configuration file /etc/inittab looking for a line
containing the word initdefault, which will look like
this: id:n:initdefault:
In the preceding, n is a valid
runlevel number, such as 3. This number is used as the default
runlevel by init. The S scripts in the corresponding
/etc/rc.d/rc n.d directory are executed
to start their respective services. If you change the default
runlevel for your system, it will most likely be to switch
between the standard text login runlevel and the GUI login
runlevel. In any case, never change the default runlevel to or
6, or your system will not boot to a usable state.
5.2.1.3 Determining your system's
runlevel
From time to time, you may be unsure just
what runlevel your system is in. For
example, you may have logged into a Linux system from a remote
location and not know how it was booted or maintained. You may
also need to know what runlevel your system was in prior to
its current runlevel -- perhaps wondering if the system was
last in single-user mode for maintenance.
To determine this runlevel information, use
the runlevel command. When
executed, runlevel displays the
previous and current runlevel as integers, separated by a
space, on standard output. If no runlevel change has occurred
since the system was booted, the previous runlevel is
displayed as the letter N. For
a system that was in runlevel 3 and
is now in runlevel 5, the output is: # runlevel
3 5
For a system with a default runlevel of 5
that has just completed booting, the output would be: # runlevel
N 5
Determining the present and previous
runlevel -- including the correct interpretation of the
N response in the output
from runlevel -- is
important. |
runlevel does
not alter the system runlevel. To do this, use either the
init or the telinit commands.
5.2.2 Changing runlevels with init
and telinit
The init
process is the "grandfather" of all processes. If used as a
command on a running system, init sends signals to the executing
init process, instructing it to
change to a specified runlevel. You must be logged in as the
superuser to use the init
command.
Syntaxinit n
Description
The number of the
runlevel, n, can be changed to
an integer from through 6 or with the letter arguments S, s,
or q. The numeric arguments
instruct init to switch to the
specified runlevel. The S and
s runlevels are equivalent to
runlevel 1. The q
argument is used to tell init to reread its configuration
file, /etc/inittab.
Examples
Shut down immediately: # init 0
Reboot immediately: # init 6
Go to single-user mode immediately: # init 1
or: # init s
The telinit
command may be used in place of init. telinit is simply a hard link to
init, and the two may be used
interchangeably.
Generally, you will use a runlevel change for
the following reasons:
-
To shut down the system using runlevel
0.
-
To go to single-user mode using runlevel
1.
-
To reboot the system using runlevel 6.
-
To switch between text-based and X11 GUI
login modes, usually runlevels 3 and 5, respectively.
Remember that init and telinit can be used
interchangeably, since they both point to the same file.
|
If you are working on a personal Linux
workstation with no logged-in users, shared files, or other
shared resources, changing the state of your system is pretty
much at your discretion. You're aware of the important
processes that are active and will surely save your work
before any runlevel change. You can then simply direct init to change the runlevel as
desired. However, if you are working on a system acting as a
file or web server, or some other public resource, changing a
runlevel without notification could be a disaster for other
users. It is imperative to notify users before any major
system changes are made. Using init to shutdown the system doesn't
automatically provide this courtesy, and in these situations
the shutdown command is
preferred.
5.2.3 System shutdown with
shutdown
When shutdown
is initiated, all users who are logged into terminal sessions
are notified that the system is going down. In addition,
further logins are blocked to prevent new users from entering
the system as it is being shut down.
Syntaxshutdown [options] time [warning message]
Description
The shutdown command brings the system
down in a secure, organized fashion. By default, shutdown takes the system to
single-user mode. Options can be used to either halt or reboot
instead. The command uses init
with an appropriate runlevel argument to affect the system
change.
The mandatory time argument tells the
shutdown command when to initiate the shutdown procedure. It
can be a time of day in the form hh:mm, or it can be in the form +n, where n is a number of minutes to wait. The
time can also be the word now, in which case the shutdown
proceeds immediately. If the time specified is more than 15
minutes away, shutdown waits
until 15 minutes remain before shutdown before making its
first announcement.
If warning
message (a text string) is provided, it is used in the
system's announcements to end users. No quoting is necessary
for warning message unless the message includes special
characters such as * or &.
Frequently used options
- -f
-
Fast boot; this skips filesystem checks on
the next boot.
- -F
-
Force filesystem checks on the next
boot.
- -h
-
Halt after shutdown.
- -k
-
Don't really shutdown, but send the warning
messages anyway.
- -r
-
Reboot after shutdown.
Examples
To reboot immediately: # shutdown -r now
To reboot in five minutes with a maintenance
message: # shutdown -r +5 System maintenance is required
To halt the system just before midnight
tonight: # shutdown -h 23:59
The two most common uses of shutdown by
individuals are: # shutdown -h now
and # shutdown -r now
These initiate for immediate halts and
reboots, respectively. Although it's not really a bug, the
shutdown manpage notes that
omission of the required time
argument yields unusual results. If you do forget the
time argument, shutdown
will probably exit without an error message. This might lead
you to believe that a shutdown is starting, so it's important
to be sure of your syntax when using shutdown.
Make certain that you are aware of the
differences between system shutdown using init (and its link telinit) and shutdown.
|
|