Linux Runlevels
![]() |
What is a runlevel?
A runlevel is a state or mode, that is defined by the services that run in that mode. The runlevels are derived from its unix historical roots. Here services means services like sshd, network, ftpd and, crond.
Why is a runlevel needed?
We need runlevels because different systems can be used in different ways. Some services are not availible 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 system disk, may be a LAN server and, 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 filesystem is not available, other high-level services such as multiuser/network login cannot be started or used).
Runlevels
In Linux there are 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.
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". 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. "S" or "K" is followed by a two-digit number indicating the order in which the scripts are to be run (low to high).
Eg. Consider the following entries (symbolic links) in the directory /etc/rc/rc0.d.
K60crond 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 set to runlevel 5. It can be customized to your needs.
Alert : Be sure not to set the default 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 services on all levels) chkconfig --level 5 network off (Turn off network service in runlevel 5) chkconfig --level 345 network on (Turn on network services in runlevels 3-5)
Note
The 8th runlevel in Linux is called runlevel "S" or "s". This runlevel is designed to be used directly, but more for the scripts that are executed when entering runlevel 1.