Ict-innovation/LPI/101.3

101.3 Runlevels, Rebooting & Shutting down the systemEdit

Candidates should be able to manage the runlevel 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 runlevel and properly terminate processes. This objective also includes setting the default runlevel.


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.

IntroductionEdit

Traditionally Linux has 7 runlevels numbered from 0- 6. A runlevel defines the state of the computer after boot. Except for runlevels 0 and 6, the the allocation of runlevel numbers to particular configurations differ from distribution to distribution. Run level 0 is the runlevel used to halt the system and run level 6 is used for rebooting the system. The remaining runlevels are typically assigned to the following configurations

  1. single user mode
  2. multi-user, text mode, no servers
  3. multi-user, text mode, servers
  4. multi-user, GUI mode, no servers
  5. multi-user, GUI mode, servers.

Below are the runlevel for a Redhat server.


# 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

What services and hardware is configured for the different runlevels is determined by a set of scripts configured for each level. Runlevel specific scripts are stored under /etc/rc.d/rc?.d, /etc/init.d/rc?.d or /etc/rc?.d or similar location where the ? Is the number of the runlevel.

Under each of these directories there are symbolic links back to a common set of scripts, under the /etc/rc.d, /etc/init.d, or /etc/rc.d/init.d (note the missing number from the paths) , that all runlevels use.

The symbolic links have a name that starts with a S or K, a number and then the name of the service the script controls. The S means that this is a script run with 'start' when the runlevel is entered and K signifies a script which is run when the run level is exited. The number indicates the order in which the scripts are run while the name indicates the services. The order of the scripts is determined by system requirements, for example the network needs to be configured before the mail or web server can be started. If one were to take a long listing of the symbolic links you would see that they point to the same script, they only differ in the parameters based to the scripts by init. 'start' to start it, and 'stop' to stop it.

Startup scripts are simply bash scripts that call the appropriate application when run. These scripts take a standard set of parameters namely; start,stop,restart and reload. Some poorly written scripts may not implement all of the parameters.

Configuring RunlevelsEdit

You can add or remove services from run levels by changing the symbolic links in the respective rc directory but various tools exists to enable easy management of these scripts.

Under Debian systems and their derivatives you can use the update-rc.d or rc-update scripts, passing in the name of the service you wish to add or remove with the appropriate parameter.

Example:

# update-rc.d disable 3 apache2

will remove the apache2 service from runlevel 3

# update-rc.d enable 3 apache2

will add apache to runlevel 3.

For rpm based distributions the command chkconfig does the same thing.

Example:

# chkconfig -del httpd

will remove the apache web server from all configured run levels

# chkconfig -add httpd

will add the apache web server to all default runlevel,

# chkconfig -level 3 httpd on

will add apache to runlevel 3


Besides adding and removing these services chkconfig can also list the runlevels for which a service has been configured with chkconfig –list apache2

Changing RunlevelsEdit

Runlevels can be changed by invoking the init binary with the run level number; for example init 6 will reboot the machine, while init 0 will halt or shutdown the machine. An alternative to init is telinit. Init and telinit perform the same function and differ only in the parameters they accept.

Teleinit/init is often used to change the state of a computer without rebooting it. Sometimes it is necessary to change the state of a computer to single user mode for system maintenance. This can be done with init 1. Before changing run levels it is good practise to let currently logged In users know about it. This can be done with the wall command. E.G. wall “users will be logged out in 5 minutes...” will send the text message to all logged in users.

If you need to determine your current runlevel you can run the command “runlevel”. To change runlevels you use the init command as explained above. Runlevels can also be changed by the following commands:

  • halt – stops the system,
  • reboot – reboots the system,
  • shutdown – will halt the system after a specified time or at a specified time interval. The command can also be used to schedule a reboot. The shutdown command can also take a text message that is displayed to any users logged in on a console on the system. For example:
  • shutdown -h now - will halt the system immediately,
  • shutdown -h +10 - System shutdown in 10 minutes – will halt the system in 10 minutes time,
  • shutdown -r 14:30 - System will be rebooted at 14:30
  • shutdown -c - will cancel any scheduled shutdown or reboot

Starting & Stopping ServicesEdit

You may also stop and start services at runtime. On Debian-based systems, by either invoking the start-up script directly on debian based systems with the appropriate parameter i.e. start for starting the service and stop for halting the service.

On Ubuntu one can run:

# /etc/init.d/apache2 stop

# /etc/init.d/apache2 start

To start or stop apache server

On Redhat based systems you can use the service command to stop and start services.

# service httpd start

# service httpd stop

To start or stop apache server

These commands can also be used to reload service configuration files to get the service to implement any configuration changes that may have been made. e.g.

# /etc/init.d/apache2 reload

# service http reload

It should be noted that not all services support the ability to reload their configuration files.



Used files, terms and utilities:

  • /etc/inittab
  • shutdown
  • init
  • /etc/init.d
  • telinit


Previous Chapter | Next Chapter

Last modified on 4 October 2012, at 21:19