Section 14.2. Objective 2: Change Runlevels and Shut Down or Reboot System


14.2. Objective 2: Change Runlevels and Shut Down or Reboot System

As mentioned in the introduction, Linux and many Unix systems share the concept of runlevels. This concept specifies different ways a system can be 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 0 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 2, 3, or 5 depending on the distribution and the use for the machine. Typical runlevel meanings are listed in Table 14-1.

Table 14-1. Typical runlevels

Runlevel

Description

0

Halt the system; runlevel 0 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 upit would shut down immediately when the kernel launches the init process. See also 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. On Debian this is the default runlevel. On Red Hat it is multiuser without NFS file sharing.

3

On Red Hat the default full multiuser mode. This and levels 4 and 5 are not usually used on Debian.

4

Typically unused.

5

On Red Hat: full multiuser mode with GUI login. Runlevel 5 is like runlevel 3 but X11 is started in addition and a GUI login is available. If your X11 cannot start for some reason, you should avoid this runlevel.

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.


14.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. Single-user mode is used for system configuration tasks that must be performed with no user activity. 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.

If you wish to boot directly into single-user mode, you may specify it at boot time with the kernel's command line through your boot loader. For instance, if you were using LILO, at the LILO: boot prompt, you would specify your kernel's image name and 1 or single for single-user mode. 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.

14.2.1.1. Overview of the /etc directory tree and the init process

By themselves, the runlevels listed in Table 14-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.

On the Exam

Make certain that you understand the use of the transitional runlevels 1 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.


When a Linux system starts, a number of scripts in /etc are used to initially configure the system and switch among runlevels. System initialization techniques differ among Linux distributions. (The examples here are typical of a Red Hat Linux system. Any LSB-compliant distribution should be similar.)


rc.sysinit or /etc/init.d/rcS

On Red Hat systems, rc.sysinit is a monolithic system initialization script. The Debian rcS script does the same job but by running several small scripts placed in two different directories. 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

Not used on Debian. On Red Hat and many other distributions, this file is a script that is called after all other init scripts. It contains local customizations affecting system startup and provides an alternative to modifying the other init files. Many administrators prefer to avoid changing rc.sysint because those changes will 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/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/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 include at least the words start and stop. Additional arguments are sometimes accepted 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/init.d/httpd stop # /etc/init.d/httpd start 

or simply:

 # /etc/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/init.d/httpd reload 

This has the effect of sending the SIGHUP signal to the running httpd process, instructing it to initialize. Signals, such as SIGHUP, are covered in Chapter 3, "Objective 6: Configure Communications Devices."

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/init.d are not directly executed by the init process. Instead, each of the directories /etc/rc0.d through rc6.d contain symbolic (soft) links to the scripts in directory init.d. (These symbolic links could also be files, but using script files in each of the directories would be an administrative headache, since changes to any of the startup scripts would mean identical edits to multiple files.) 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]Nname, 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.


N

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.


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 the default runlevel (3 for the sake of this example) at boot time, all of the links with the S prefix in /etc/rc3.d will be executed in the order given by their sequence number (S10network, S12syslog, and so on). 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.

14.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/rcN.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 0 or 6, or your system will not boot to a usable state.

14.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 runlevelperhaps wondering if the system was last in single-user mode for maintenance.

To determine this 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 

runlevel does not alter the system runlevel. To do this, use the init command (or the historical alias telinit).

14.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.


Syntax

 init n 


Description

The number of the runlevel, n, can be changed to an integer from 1 through 6.

The numeric arguments instruct init to switch to the specified runlevel. init also supports a few alphabetical options such as S and s, which are equivalent to runlevel 1, and q, which 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 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.

On the Exam

Remember that init and telinit can be used interchangeably.


14.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.


Syntax

 shutdown [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. 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.


-h

Halt after shutdown.


-k

Don't really shut down, but send the warning messages anyway.


-r

Reboot after shutdown.


-F

Force filesystem checks on the next boot.


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.

On the Exam

Make certain that you are aware of the differences between system shutdown using init (and its link telinit) and shutdown.




LPI Linux Certification in a Nutshell
LPI Linux Certification in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596005288
EAN: 2147483647
Year: 2004
Pages: 257

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net