Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Linux Run Levels

News

Recommended Books

Recommended Links grub Xinetd  Suse RC Scripts

 Installation Checklist

RPM

YaST

Solaris Run Levels

Humor

Etc

The default runlevel for a system is specified in the /etc/inittab file, which will contain an entry such as id:3:initdefault: if the system starts in runlevel 3, or id:5:initdefault: if it starts in runlevel 5.

There are three method of determining the current runlevel

  1.  who -r command:
    # who -r
    run-level 3  Aug 27 10:12                   last=5
  2. Using runlevel command which produces essentially the same result but in a form of just two numbers. Runlevel command retrieves information from the /var/run/utmp to locate the runlevel record.
  3. The last method is not working in mainstream flavors of Linux (Red Hat 4 & 5 and Suse 10 & 11) but is an interesting option.  In some versions of init from the environment variables RUNLEVEL and PREVLEVEL are populated and changed with each change, similar to $PWD and other system variables, for example:
    echo $RUNLEVEL

The Init Program

After the kernel starts a program called init, the init process reads the file "/etc/inittab" and uses this file to determine how to create processes. It does it in stages called runlevels.

The administrator can also dynamically change the current runlevel by using the init program. the default runlevel into which the system is booted can be changed by editing the "/etc/inittab" file.

Red Hat as well as its derivatives including Suse uses run levels in a following way:

Which services are started in which runlevels can be managed with the chkconfig tool, which keeps its configuration settings in a elaborate set of link to scripts contained in /etc/rc.d/.

/sbin/chkconfig --list lists all the services controlled by chkconfig and whether they are on/off for each runlevel. Setting a service A controlled by chkconfig, for levels X, Y and Z is achieved using /etc/chkconfig --level XYZ A

See Runlevel - Wikipedia, the free encyclopedia

Default level is defined in /etc/inittab, for example id:5:initdefault

The inittab file

The "/etc/inittab" file tells init which runlevel to start the system at and describes the processes to be run at each runlevel. An entry in the inittab file has the following format:

id:runlevels:action:process

Note the order of programs to run as specified above are:

  1. /etc/rc.d/rc.sysinit
  2. /etc/sbin/update
  3. /etc/rc.d/rc 3 - Note: we are running runlevel 3 here.

Therefore, the next thing that the system does is to run the rc.sysinit file, save buffers to the hard drive, then run system script files for the requested runlevel which will start up many system and network services as explained in the next section.

Determining the Default Boot runlevel

The default boot runlevel is set in the file /etc/inittab with the initdefault variable. When set to 3, the system boots up with the text interface on the VGA console; when set to 5, you get the GUI. Here is a snippet of the file (delete the initdefault line you don't need):

# Default runlevel. The runlevels used by RHS 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)
#
id:3:initdefault:                         # Console Text Mode
id:5:initdefault:                         # Console GUI Mode

Note the following:

Old News

Cool Solutions Runlevels in SUSE Linux

Posted: 3 May 2005

Linux operating systems utilize runlevels to determine the services that should be running and to allow specific work to be done on the system. For example, runlevel one is designated single-user mode without networking and is used for critical system maintenance and troubleshooting. Runlevel three is multi-user text mode. Runlevel five is multi-user graphical mode.

Switching between runlevels is accomplished using the init command. Entering:

# init 5

will change to runlevel five. Changing to runlevel six will reboot the machine and runlevel zero will shut down the machine.

Entering runlevel at the command prompt will output the previous and current runlevels. For example:

#runlevel
N 5

indicates that the machine is in runlevel five. The 'N' indicates that the machine was booted into level five, or no previous runlevel was entered.

As mentioned previously, the different runlevels are configured to run a specific set of services. Each runlevel is configured to start its specified services when starting and to shut down its services when switching to a different runlevel.

As a general rule, turn off all services that are not needed at a given runlevel. Managing the services started in each runlevel is simple and can be done graphically in YaST or at the command line with the chkconfig command.

Managing Runlevel Services With YaST

YaST offers a robust and easy to use runlevel service configuration tool. Access the tool at YaST > System > Runlevel Editor. Within the tool, turn services on or off by selecting the service and then selecting the Enable/Disable buttons.

Selecting expert mode allows the administrator to configure the state of a service in each runlevel.

For example, use the YaST runlevel editor to configure the OpenSLP daemon, slpd, to start in runlevels three and five only. Do the following:

  1. Select YaST > System > Runlevel Editor.
  2. Select the Expert Mode radio button.
  3. In the services list, select slpd.
  4. Use the check boxes to select runlevels three and five. Deselect all other runlevels.
  5. Save changes and exit.

Managing Runlevel Services With chkconfig

Most Linux distributions include the chkconfig command for managing runlevel services. The syntax for chkconfig is specified in the chkconfig man page.

For example, use chkconfig to configure the OpenSLP daemon, slpd, to start in runlevels three and five only. Do the following:

1.Check if slpd is on in the current runlevel

# chkconfig slpd
slpd on

2.Check slpd's configuration for every runlevel.

# chkconfig -l slpd
slpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

3.Turn slpd on in runlevels three and five.

#chkconfig slpd 35

2.2. Understanding Runlevels

Unlike most non-UNIX operating systems which only have 2 modes of functionality (on and off), UNIX operating systems, including Linux, have different runlevels such as "maintenance" runlevel or "multi-user" runlevel, etc.

Runlevels are numbered from 0 to 6 and will vary from one Linux distribution to another. The description for each runlevel functionality is sometimes documented in /etc/inittab.

Table 2.1. Linux runlevels

Runlevel Purpose
0 Shuts down the machine safely. The operating system will also attempt to poweroff the system if possible
1 Single user mode. Only one terminal is available for the (single) user root. All other users are logged out
2[a] Multi-user mode, but does not start NFS. Network services like email or web services are also stopped
3[a] Full multi-user mode. Selected network services are all on.
4[a] Not defined and generally unused
5[a] Like runlevel 3 but runs a Display Manager as well
6 restarts the machine safely
[a] These runlevels may vary in functionality between Linux flavours. But these descriptions correspond to the current LSB specification.

2.2.1. INIT Controls Runlevels

Both init and telinit are used to switch from one runlevel to another. Remember that init is the first program launched after the kernel has accessed the root device.

At boot time init is instructed which runlevel to reach in /etc/inittab with the line:

id:5:initdefault:

When the system is started it is possible to change runlevels by invoking init (or telinit which is a symbolic link pointing at init).

For example we switch to runlevel 4 with either of the next commands:

init 4

telinit 4

The PID for init is always 1. It is possible to find out which runlevel the system is currently in with the command runlevel

runlevel
N 5

The first number is the previous runlevel (or N if not applicable) and the second number is the current runlevel.

Recommended Links



Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: August 27, 2009