Sunday, March 14, 2010

Intro About Run Levels in Linux

Run Levels and inittab – RHEL

Now that we have already seen the boot sequence and GRUB file for linux, lets now check what is run level and one of the important file /etc/inittab which is used during booting linux.
Run Level
Operating system like linux has several services that are running. Each service will provide some functionality to OS. Example ftp service will allow other machine to FTP from your machine. Similarly network service will allow networking functionality.
But keeping all the services running while working on only few serivce will impact performance. Some times administrator does not need all the services and just need few services for working.
For this kind of situation, linux provides different runlevel. Each run level has some specific set of services running. Example if you start your linux in run level 1, some specific set of services will get started but not all. If you boot linux in run level 2 some more services will get started over and above services from run level 1. So run level is just a way to tell OS what are all services it should start during boot time.
Linux has 7 run levels, they are given as below. These run levels are defined in a file /etc/inittab which is the file read during boot time to get the default run level set and linux will boot in that run level.
0 -> Used for shutdown. Setting this in /etc/inittab, linux wont be able to boot.
1 -> Single user mode. Linux will start in single user mode. Usually used for maintenance task.
2 -> Multiuser, without NFS.
3 -> Full multiuser mode, no X Server started so no graphical display.
4 -> unused
5 -> Starts XDisplay as well. X Server is started with all graphical display.
6 -> reboot. Do not set this value in /etc/inittab, else linux will reboot continuously.

You can check the current run level of your Linux using following command.

[chramana@ap6023fems chramana]$ /sbin/runlevel
N 3

Here it gives 2 characters which are . So the current run level is 3 and previous run level is null, meaning that system started with run level 3 from the very beginning.
After having this brief idea about run level we will see the content of /etc/inittab file
/etc/inittab
This file is one of the most important file for booting. Every the entry in this file has following format each seperated by “:”"
Identification tag of that line : Run level numbers : Keyword : Path of script to be executed
Example:
si::sysinit:/etc/rc.d/rc.sysinit
In the above line there is no “Run level number” after 1st “:”, that means that this command will run for all the run levels.
Following are the different entries in the file
The first entry in file is
id:3:initdefault:
This line decide what should be runlevel at the time of booting. Here the run level is 3, that means that system will start in run level 3 by default. If we change this run level from 3 to 5 and reboot then system will start in run level 5.
si::sysinit:/etc/rc.d/rc.sysinit
This line is for system initialization and is executed at all run level. It calls the script /etc/rc.d/rc.sysinit to initialize the system during startup.
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

These lines will carry out the task based on the run level in which we are starting the system. Depending on the run level proccessing will change.
Each of this entry will call same script /etc/rc.d/rc but will pass the arguement as run level #, depending on which run level system is booting.
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
This command is used to trap the key pressed and will be executed for all run level. If a combination of ctrl+Alt+Del key is pressed then it will shutdown the system by running /sbin/shutdown command. -t3 -r now are the arguements to this script.
pf::powerfail:/sbin/shutdown -f -h +2 “Power Failure; System Shutting Down”

The above line tells what needs to be done is a power falure is detected by the system. There are device drivers which will detect change in the UPS status when power goes off. When such condition occurs then this command will guide system for required action.
In this case its doing to display message “Power Failure; System Shutting Down” and system will do a clean shutdown using /sbin/shutdown script in 2 mins (hence +2 in script). This command will run for all run sequence.
ppr:12345:powerokwait:/sbin/shutdown -c “Power Restored; Shutdown Cancelled”
This is a power restore action. Suppose when power goes off and we have configured to run shutdown command after 10 mins (+10 option) and lets say power comes back in 9 mins, then we dont want to shutdown the system as power came back, in this case we can specify the action to be taken in this command.
x:5:respawn:/etc/X11/prefdm -nodaemon
The above command starts a X11 server in run level 5.
/etc/inittab calls several other script which initializes and configures the system at the time of starting.
We can also check which all services starts at what level using chkconfig command as shown below.
[root@ap6023fems chramana]# /sbin/chkconfig –list
xinetd          0:off   1:off   2:off   3:off   4:on    5:on    6:off
gpm             0:off   1:off   2:on    3:off   4:on    5:on    6:off
ypxfrd          0:off   1:off   2:off   3:off   4:off   5:off   6:off
dc_client       0:off   1:off   2:off   3:off   4:off   5:off   6:off
lm_sensors      0:off   1:off   2:on    3:off   4:on    5:on    6:off
ldap            0:off   1:off   2:off   3:off   4:off   5:off   6:off
winbind         0:off   1:off   2:off   3:off   4:off   5:off   6:off
rpcidmapd       0:off   1:off   2:off   3:off   4:on    5:on    6:off
snmptrapd       0:off   1:off   2:off   3:off   4:off   5:off   6:off
ripd            0:off   1:off   2:off   3:off   4:off   5:off   6:off
ocfs2           0:off   1:off   2:on    3:off   4:on    5:on    6:off
sysstat         0:off   1:on    2:on    3:off   4:on    5:on    6:off
squid           0:off   1:off   2:off   3:off   4:off   5:off   6:off
messagebus      0:off   1:off   2:off   3:off   4:on    5:on    6:off
rawdevices      0:off   1:off   2:off   3:off   4:on    5:on    6:off
FreeWnn         0:off   1:off   2:off   3:off   4:off   5:off   6:off
lisa            0:off   1:off   2:off   3:off   4:off   5:off   6:off
crond           0:off   1:off   2:on    3:off   4:on    5:on    6:off
openibd         0:off   1:off   2:on    3:off   4:on    5:on    6:off
cups            0:off   1:off   2:on    3:off   4:on    5:on    6:off
ripngd          0:off   1:off   2:off   3:off   4:off   5:off   6:off
iiim            0:off   1:off   2:on    3:off   4:on    5:on    6:off
cpuspeed        0:off   1:on    2:on    3:off   4:on    5:on    6:off
multipathd      0:off   1:off   2:off   3:off   4:off   5:off   6:off
apmd            0:off   1:off   2:on    3:off   4:on    5:on    6:off
rdisc           0:off   1:off   2:off   3:off   4:off   5:off   6:off
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
yppasswdd       0:off   1:off   2:off   3:off   4:off   5:off   6:off
rpcgssd         0:off   1:off   2:off   3:off   4:on    5:on    6:off
irda            0:off   1:off   2:off   3:off   4:off   5:off   6:off

– output truncated –
The first column gives the service name and further column gives the status of services at different run level starting from 1 to 6. Last script that will be executed is /etc/rd.d/rc.local. So any customization can be done in this script.
You can switch off or switch on any service for that run level using following command.
chkconfig off | on
Example:
chkconfig acpid off

This will turn off acpid service, so next time when you reboot, it wont start acpid serivce which its supposed to start at run level 4 and 5.
system-config-services is the graphical interface to chkconfig.
Same things can be done using system-config-services tool.
Hope this helps !!

No comments:

Post a Comment

Kubernetes 1.31 || Testing the Image Volume mount feature using Minikube

With Kubernetes new version 1.31 ( https://kubernetes.io/blog/2024/08/13/kubernetes-v1-31-release/ ) there are so many features releases for...