February 1, 2007

Run Levels in Linux

The Linux OS like Windows runs "services." There are times when you may want some or all installed services to run. This is where "runlevels" are used. Runlevels are used to specify different configurations of running services. The chart below shows a basic outline of the standard init levels.

Init Level

Comments

0

Runlevel 0 is reserved for the "shutdown" phase. Entering init 0 from the shell prompt will shutdown the system and usually power off the machine.

1

Runlevel 1 is usually for very basic commands. This is the equivalent to "safe mode" used by Windows. This level is usually only used to asses repairs or maintenance to the system. This is a single-user mode and does not allow other users to login to the machine.

2

Runlevel 2 is used to start most of the machines services. However, it does not start the network file sharing service (SMB, NFS). This will allows multiple users to login to the machine.

3

Runlevel 3 is commonly used by servers. This loads all services except the X windows system. This means the system will boot to the equivalent of DOS. No GUIs (KDE, Gnome) will start. This level allows multiple users to login to the machine.

4

Runlevel 4 is usually a "custom" level. By default it will start a few more services than level 3. This level is usually only used under special circumstances.

5

Runlevel 5 is everything! This will start any GUIs, extra services for printing, and 3rd party services. Full multi-users support also. This runlevel is generally used on by workstations.

6

Runlevel 6 is reserved for "reboot" only. Be carefully when running this command. Once you have entered init 6, there is no stopping it!

What is Running and When?

There will be a time when you want to know what services are running, and the different runlevels they are specified in. You can use a simple command to display a list of all runlevels and services used by them. The text below shows the output of the chkconfig --list command.

[root@roswell root]# chkconfig --list
microcode_ctl   0:off   1:off   2:on    3:on    4:on    5:on    6:off
gpm             0:off   1:off   2:on    3:on    4:on    5:on    6:off
kudzu           0:off   1:off   2:off   3:off   4:on    5:off   6:off
syslog          0:off   1:off   2:on    3:on    4:on    5:on    6:off
daytime:     off
echo-udp:    off
echo:        off
services:    off
time:        off
time-udp:    off
cups-lpd:    off
sgi_fam:     on
tftp:        off
ktalk:       off
swat:        on



Notice that each services is followed by columns for each of the 6 runlevels. Now of course there will also be a time when you need to make changes to a specific runlevel. Using the output from above, let's say we want to turn on lisa for runlevel 5. You can do this using the command shown below:

[root@roswell root]# chkconfig --level 5 lisa on
[root@roswell root]# 

If no errors are reported, you must have correctly entered the syntax of the this command. Notice that you must have 2 - signs before level. The opposite is also true. Let's now say that we don't want lisa to start everytime we boot the machine to init 5. You can turn that service off by entering the reciprocal of the previous command show below:

[root@roswell root]# chkconfig --level 5 lisa off
[root@roswell root]# 



More info is also available about runlevels from the manual pages. You can view the man pages by using the command:

[root@roswell root]# man init
[root@roswell root]# 
 
INIT(8)               Linux System Administrators Manual              INIT(8)
 
NAME
       init, telinit - process control initialization
 
SYNOPSIS
       /sbin/init [ -a ] [ -s ] [ -b ] [ -z xxx ] [ 0123456Ss ]
       /sbin/telinit [ -t sec ] [ 0123456sSQqabcUu ]
 
DESCRIPTION
  Init
       Init  is  the  parent  of all processes.  Its primary role is to create
       processes from a script stored in  the  file  /etc/inittab  (see  init-
       tab(5)).   This file usually has entries which cause init to spawn get-
       tys on each line that users can log in.  It  also  controls  autonomous
       processes required by any particular system.
 
RUNLEVELS
       A  runlevel is a software configuration of the system which allows only
       a selected group of processes to exist.  The processes spawned by  init



I truncated the rest of the manual entry above. Using runlevels is must for every Linux user. The init file configuration are stored in the /etc/inittab file. By editing this file, you can change the default runlevel upon boot up. The output below shows the inttab file:

[root@roswell root]# vi /etc/inittab
#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#
# Author:       Miquel van Smoorenburg, 
#               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
#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#
# Author:       Miquel van Smoorenburg, 
#               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)
#
id:3:initdefault:
 
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
 
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
 
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
 
# 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"
Notice the line above that I have highlighted in red:

id:3:initdefault:

This specifies the default runlevel for the system. This means that every time the system reboots; it will bring up init 3. You can easily change this to whatever runlevel you want. Just be sure not to change the numeric value to 0 or 6! The two most common values are 3 and 5.

No comments: