Runlevel can be defined as a part of Linux OS which determines the processes that need to be started at system startup. While booting a Linux system, 'init' (but different in the case of systemd) is the first process that gets executed with PID 1, which then starts other processes. The processes that need to be started by 'init' at system bootup are determined by the default runlevel mentioned in '/etc/inittab' file.
There are 7 runlevels defined in any Linux machine, which are as follows:
0 System Halt
1 Single User Mode
2 Multi User Mode without networking
3 Multi User Mode with networking
4 Not Used/Special purpose
5 Multi User Mode with GUI
6 System Reboot
All runlevels are defined in a directory '/etc/rcX.d', where X corresponds to the runlevel.
For example, runlevel5 is available at, '/etc/rc5.d'. In this directory you can find many files with the following syntax:
S => Stands for starting the process
K => Stands for kill/stop the processes
nn => These digits determines the order in which the programs need to be executed. The lowest digit programs will be executed first.
So, all programs that start with letter 'K' will be killed in that particular runlevel and all programs that start with 'S' will be started in that particular runlevel.
Changing and Viewing the default runlevel
The default run level is defined in the file '/etc/inittab'. You can view it as follows.
# grep ^id /etc/inittab
As you can see from the above output, the default runlevel is 5. If you want to change this to 3, edit the /etc/inittab file with the following.
Listing the current runlevel
If you want to check the current runlevel in which your machine runs, you can use any of the following commands.
This shows that the current runlevel is 3. 'N' stands for none, meaning there has been no run level change since powering up.
# who –r
run-level 3 2012-08-10 04:30 last=S
This shows the current runlevel is 3 and the last runlevel was 'single user mode'.
You can change the runlevels using the command telinit (stands for telling init o change runlevel). This actually signals “init” process to change runlevel.
For example, if you want to change the runlevel to 5, execute the following command.
# telinit 5
# init 5