Runlevel in Linux?
The runlevel is a predetermined operating state, which defines the tasks and required services accomplished for the current state of a Unix and Linux operating system, which supports the basic 3 runlevels and more runlevels according to the requirements.
Each runlevel defined by a single digit integer and the system can be booted to any runlevel according to the need. Each runlevel chooses a different system configuration and supporting a different combination of processes as required.
List of runlevels supported by a standard Linux kernel.
0 - Halt the system. The system can be powered down safely.
1 - Single-user mode, which is especially for administration and is rarely used.
2 - Supports local multiple users with networking. But, no NFS (network file system), that is network service, which are used rarely.
3 - Supports for multiuser with networking and command line interface, that is all text mode. This is the standard runlevel for most Linux-based server hardware.
4 - This is mostly not used and is User-definable.
5 - Supports for multiple users with networking and also GUI (graphical user interface), which is nothing but X Windows. This is the standard runlevel for most Linux-based desktop systems.
6 - This is nothing but Reboot, that reboot the system.
A Linux Kernel by default boots based of the configuration file "/etc/inittab" either to runlevel 3 or to runlevel 5, where 3 allows the system to run all services except for a GUI, which is allowed by 5.
The default runlevel of a machine can be found in the file "/etc/inittab", where the content "id:5:initdefault:" states that the default runlevel is 5.
This can also be checked by using the below command
[root@localhost ~]$ grep "^id:" /etc/inittab
Change the runlevel?
We can modify the preset runlevels or even create new ones, as per our requirement. The runlevels 2 and 4 are usually used for user defined runlevels.
init is the program, which is responsible for altering the runlevel and can be called using the "telinit" command.
That means, if we need to change from runlevel 3 to 5, which allows the GUI, we should run the below command as root. Also, the vice versa can be done by running "telinit 3" to move from 5 to 3.
[root@localhost ~]$ telinit 5
This can be done by editing the "/etc/inittab" file too. But, this is not safe and thus, it is recommended to take ca backup of any config file before editing it.
Which runlevel are you in currently?
The current and previous runlevel of the system can be found out by running the below command.
'N' states that the runlevel has not been changed since the last boot, where as '5' state the current runlevel. When we run the command "[root@localhost ~]$ telinit 3", the runlevel will be changed to 3.
If you check the runlevel now, the output will be "5 3", where '5' is the previous runlevel and '3' is the current runlevel. The reverse can be checked running "telinit 5" and then, the output will be "5 3". If you run again "telinit 5", the output will be "5 5", where both the previous and current runlevel is 5.
How services depend upon runlevel?
Services, that are starting at the boot time are determined by the contents of the directories "rcN.d", which are located under /etc or /etc/init.d,'N' in rcN.d represent the runlevel. Thus, each and every runlevel has its own directory and the related executable.
See the list of below few services and their directory path:
[root@localhost]# ls -lrt /etc/rc3.d/
lrwxrwxrwx. 1 root root 20 Dec 26 18:50 S22messagebus -> ../init.d/messagebus
lrwxrwxrwx. 1 root root 18 Dec 26 18:50 S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 21 Dec 26 18:51 S11portreserve -> ../init.d/portreserve
lrwxrwxrwx. 1 root root 16 Dec 26 18:53 S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 16 Dec 26 18:53 K10psacct -> ../init.d/psacct
lrwxrwxrwx. 1 root root 20 Dec 26 18:56 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 18 Dec 26 18:57 K95cgconfig -> ../init.d/cgconfig
The name of all the links start either with 'S' or 'K' followed by a number. S indicates the service will be started if it is not running already and K indicates that, the service will be stopped if it is running already according to the run level. Also, the number next to S or K tells in the order, where the service should be started or stopped. So, S11 service will be started once s10 service is started.
Sample content of "/etc/inittab":
[root@localhost rathakar]# cat /etc/inittab
# inittab is only used by upstart for the default runlevel.
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
# System initialization is started by /etc/init/rcS.conf
# Individual runlevels are started by /etc/init/rc.conf
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
# Default runlevel. The runlevels used 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)
So, the system should never defaulted to be with init level 0 or 6.
See more Linux Tutorials
Read What is Linux and should you switch to use Linux rather?