Linux Runlevels
What is a runlevel?
A runlevel is a state or mode, that is defined by the services that run in that mode. Here services means services like sshd, network, ftpd, crond, ...... etc.
Why is runlevel needed?
We need runlevels because different systems can be used in different ways. Some services cannot be used until the system
is in a particular state or mode. Only when some lower services are available, other higher services can be started/used.
Consider that your disk (maybe a LAN server) is corrupted and you want to repair it. In such situations, you do not expect
other users to login to the system. Now you can switch to runlevel 1 and perform the maintenance tasks on your disk. Since runlevel 1 doesnt support network/multiuser login, other users cannot login to the system, when it is under maintenance. (i.e. when a low level service [proper filesystem] is not available, other high level services [ multiuser/network login] cannot be started/used).
Runlevels
In Linux there are basically 7 runlevels ( from 0 to 6 )
0 — Halt (Shutdown) 1 — Single-user mode 2 — Basic multi user mode without NFS 3 — Full multi-user mode 4 — Not used (user-definable) 5 — Full multi-user mode (with an X-based login screen) 6 — Reboot
Each runlevel runs a particular set of services. The list of all services in the system will be in the /etc/init.d directory. There is a directory that corresponds to each runlevels.
Eg.
For runlevel 0 ----> /etc/rc/rc0.d or /etc/rc0.d For runlevel 1 ----> /etc/rc/rc1.d or /etc/rc0.d For runlevel 2 ----> /etc/rc/rc2.d or /etc/rc0.d For runlevel 3 ----> /etc/rc/rc3.d or /etc/rc0.d For runlevel 4 ----> /etc/rc/rc4.d or /etc/rc0.d For runlevel 5 ----> /etc/rc/rc5.d or /etc/rc0.d For runlevel 6 ----> /etc/rc/rc6.d or /etc/rc0.d
Here /etc/rc/rc0.d is the real directory and /etc/rc0.d is a symbolic link to /etc/rc/rc0.d. Each of these directory will
contain many symbolic links. These links will point to the services in the /etc/init.d directory. All these links will start with either an "S" or "K".
"S" means Start "K" means Kill. Each link is named with a prefix of "K" or "S" according to whether that particular service need to be killed or started in
that runlevel.
Eg. Consider the following entries (symbolic links) in the directory /etc/rc/rc0.d.
K60crond K70alsa K88syslog S00killall S01halt ... ... K08httpd K90network K09smb
This directory corresponds to runlevel 0 which is "shutdown". Here the services "killall" and "halt" are started. All other
services are killed. This can be seen since only killall and halt start with "S" and all other entries start with "K". You may wonder what if "killall" and "halt" services start before the kill of all the other services. Unfortunately that doesnt happen. First all the kill services in the directory will be executed, followed by the start services.
If you need further info, tweak into the /etc/rc.d/rc file which manages the start and stop of services when switching runlevels.
The default runlevel is defined in the /etc/inittab file (id:5:initdefault:) By default it is runlevel 5. It can be customized to your needs. Alert : Be sure you dont set it to 0 or 6. :-)
chkconfig
This command is used for setting/getting runlevel information.
chkconfig --list ( List the status of all services on all runlevels) chkconfig --list network ( List the status of network service on all runlevels) chkconfig --level 5 network on/off ( Turn on/off network service in runlevel 5 ) chkconfig --level 345 network on/off ( Turn on/off network service in runlevels 3,4,5 )
Note
Actually there are 8 runlevels in Linux. The 8th one is called runlevel "S" or "s". This runlevel is not really meant
to be used directly, but more for the scripts that are executed when entering runlevel 1 .