Loading...
Basic Linux

Run Levels

Runlevels are an important part of Linux operating system.If the administrator Understands runlevels concept clearly,It will be another layer of control and flexibility over the servers for them.

Whenever the boot loader loads the Linux kernel, the kernel boots up it and scans the hardware. Then it loads the loadable modules it needs, and finally starts process number 1, /sbin/init. By starting the init process it is responsible for running other start up scripts which mainly involves initialization of you hardware, bringing up the network, starting the graphical interface or command line interface.

The init first finds the default runlevel of the system so that it could run the start scripts corresponding to the default run level.

A runlevel can simply be thought of as the state your system enters like

  1. If a system is in a single-user mode it will have a runlevel 1.
  2. If the system is in a multi-user mode it will have a runlevel 5.

The standard LINUX kernel supports these seven different runlevels :

0 -Halt the system.
1 -Single-user mode (for special administration).
2 -Local Multiuser with Networking but without network service (like NFS)
3 -Multiuser mode with Networking under command line interface and not graphical user interface.
4 -Not Used
5 -Multiuser Mode with Networking under graphical user interface (X Windows-GUI).this is the standard runlevel for most of the LINUX based systems
6 -Reboot(which is used to restart the system)

By default most of the LINUX based system boots to runlevel 3 or runlevel 5.

In addition to the standard run levels, users can modify the present runlevels or even create new ones according to the requirement.
Runlevels 2 and 4 are used for user defined runlevels and Runlevel 0 and 6 are used for halting and rebooting the system.

There are different Start up scripts for different run levels, those will perform different tasks. These start scripts corresponding to each run level can be found in special files present under rc sub directories.

The applications that are started by init are located in the /etc/rc.d folder. Within this directory there is a separate folder for each run level, eg rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d and rc6.d or there will be a set of files named rc.0, rc.1, rc.2, rc.3, rc.4, rc.5 and rc.6.

For example, run level 1 will have its start script either in file /etc/rc.d/rc.1 or any files in the directory /etc/rc.d/rc1.d.

Changing runlevel
init is the program responsible for altering the run level which can be called using telinit command.

For example, to change a runlevel from 3 to runlevel 5, possible by telinit command:
telinit 5

Need for changing the runlevel
when you unable to login or in a case you don’t remember the password and if the corrupted /etc/passwd file (have all the user names and passwords), in all these cases the problem can be solved by booting into a single user mode i.e runlevel 1.

Single User mode
Single User Mode is used primarily for system maintenance especially if the file systems are in need of repair.
– This mode is also normally assigned to run level 1.
– No daemon processes are running.
– Network is not configured.
– Only the root file system is mounted by default.
– No users can log in other than the super user.
– This mode is used primarily to repair the system.

Multi-user mode: Multi-user mode is the normal operating mode of a Linux system. When the system boots into multi-user mode the following happens.
– The file systems are checked using the fsck(8) command. The fsck(8) command causes the file system specific program to be executed.
– The file systems are mounted.
– Init(8) parses the /etc/inittab file.
● The default run level is determined.
● The command scripts associated with this run level are executed.
– Service daemons are started.
– General user logins are permitted.

chkconfig
The chkconfig command is used in Red Hat based systems (like CentOS) to know what services are started at which runlevels.

command
chkconfig –list

will display a list of services whether they are enabled or disabled for each runlevel.

The default runlevel for a system is specified in /etc/initab file which will have an

Entry id : 5 : initdefault ( Default runlevel is set to 5 )

Or else will have an

Entry id : 3 : initdefault ( Default runlevel is set to 3)

But now days when we use cat /etc/inittab we will get message like

[root@localhost kumar]# cat  /etc/inittab
# inittab is no longer used when using systemd.

ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

 Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target

 systemd uses ‘targets’ instead of runlevels. By default, there are two main targets:
 multi-user.target:  analogous to runlevel 3
 graphical.target:   analogous to runlevel 5

 To view current default target, run:
 systemctl get-default

 To set a default target, run:
 systemctl set-default TARGET.target

To know the current runlevel of your system

[root@localhost kumar]# systemctl get-default
graphical.target

Basically the runlevel “graphical.target” is the equivalent to the runlevel 5 (Graphical Display and networking)
and the runlevel “multi-user.target” is the equivalent to the runlevel 3 (command line interface and networking)

You can display the new runlevels/targets

ls -al  /lib/systemd/system/runlevel*

[root@localhost kumar]# ls -al  /lib/systemd/system/runlevel*

lrwxrwxrwx 1 root root 15 May 14 09:34 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx 1 root root 13 May 14 09:34 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx 1 root root 17 May 14 09:34 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx 1 root root 17 May 14 09:34 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx 1 root root 17 May 14 09:34 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx 1 root root 16 May 14 09:34 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx 1 root root 13 May 14 09:34 /lib/systemd/system/runlevel6.target -> reboot.target

Runlevel Systemd Description

0 – poweroff.target
1 – rescue.target
2 – multi-user.target
3 – multi-user.target
4 – multi-user.target
5 – graphical.target
6 – reboot.target

To change the runlevel from “graphical.target” to “multi-user.target”. (Runlevel 5 to Runlevel 3).

First delete the existing “default.target” and then replace it with our link command. Our new “target.default” will be that of “runlevel3.target”

[root@localhost kumar]# rm  /etc/systemd/system/default.target
rm: remove symbolic link ‘/etc/systemd/system/default.target’? y
[root@localhost kumar]# ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target
[root@localhost kumar]# systemctl get-default
runlevel3.target

Now if we reboot the system, it would start in “runlevel 3 – multi-user.target”.

To revert back to the “runlevel 5 – graphical.target” we can done by using following commands

[root@localhost kumar]#  systemctl get-default
runlevel3.target
[root@localhost kumar]#  rm  /etc/systemd/system/default.target
rm: remove symbolic link ‘/etc/systemd/system/default.target’? y
[root@localhost kumar]#  ln -s  /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target
[root@localhost kumar]# systemctl get-default
runlevel5.target

Leave a Reply

Your email address will not be published. Required fields are marked *