When a Linux system is booted it passes through several distinct stages, known as runlevels, before the system has completed booting and is usable. Likewise, when a Linux system is shut down it passes through another set of runlevels before the system is ready to be turned off. In this column, we are going to examine runlevels and show you some tricks that can be used to avoid unnecessary reboots of your Linux system.A runlevel could be referred to as a system state. They can also be considered as similar to the several modes into which Microsoft Windows is able to boot: Normal, Safe-mode and Command prompt only. Each runlevel has a collection of associated services that are initialised or stopped when the system enters the runlevel. These services are represented by initialisation scripts located in the directory corresponding to each runlevel: /etc/rc.d/rc?.d or /etc/rc?.d, where ? is the number corresponding to the runlevel. There are generally eight runlevels used by most Linux distributions, though some may vary this organisation slightly.
|0||Halt the system|
|1||Single user mode|
|2||Basic multi user mode|
|3||Multi user mode|
|5||Multi user mode with GUI|
|6||Reboot the system|
|S, s||Single user mode|
Displaying the runlevelTo begin, you may be curious to know which runlevel your system was previously and is currently in. The ‘runlevel’ command can be used to find out this information: $ /sbin/runlevel 3 5 The output of the runlevel command tells me my system was previously in runlevel 3 and is currently in runlevel 5.
Switching runlevelsTo switch runlevels use the ‘telinit’ command to control the ‘init’ process (as root): $ /sbin/telinit
Adding services to a runlevelTo add a service to be started at a certain runlevel, first create a startup script for the service and place it in the directory /etc/rc.d/init.d or /etc/init.d (depending on how your distribution lays out these directories). To associate the script with a runlevel, create a symbolic link in the directory for the runlevel you wish it to be started under. The filename of this symbolic link should be prefixed with SXX where XX is a number. The purpose of the number is to control the order in which scripts are executed within each runlevel. The scripts are executed in ascending numerical order. An example of adding an initialisation script to a runlevel is: $ cp myservice /etc/init.d/ $ ln -s /etc/init.d/myservice /etc/rc3.d/S99myservice The next time runlevel 3 is entered, myservice would be started.
Booting into a runlevelYou can change the runlevel Linux boots into by specifying it at boot time. If your system uses LILO, append the runlevel to the boot command as shown: LILO: linux 5 If your system uses GRUB, you can change the boot runlevel by pressing the ‘e’ key to edit the boot configuration. From here, append the runlevel to the end of the boot command as shown: kernel /vmlinuz ro root=/dev/hda1 5
Brand Post Placeholder: None