Monday, December 22, 2008

Understanding Linux (System V) Run Levels

On Linux systems, the run level controls what starts or stops at boot time. Run level directories contain links to scripts that start and stop daemons (services). Run level 0 shuts everything down. Run level 6 is used when a restart is requested. Run level 1 is typically used for emergency repairs such as adminsitrative (root) password recovery. Other run levels (usually 2, 3 , 4, and 5) can be configured to start and stop daemons based on your particular needs. It's pretty common to only use one of the run levels other than 0, 1, and 6.

The number of run levels varies from distro to distro, as do the default settings in each run level. What follows is an example of the default configurations for run levels in a system running a Red Hat-based distribution:

  • runlevel0: Shut down the system. Do not set the inittab value to runlevel0.
  • runlevel1: Single-user mode
  • runlevel2: Multi-user mode, but no NFS support
  • runlevel3: Multi-user mode without “X” (the most commonly used run level and usually the best choice for servers)
  • runlevel4: Not used
  • runlevel5: Full multi-user mode with X11 (graphics support) (Good for end-user workstations, but not recommended for servers.)
  • runlevel6: Reboot (Do not set the inittab value to runlevel6.)

A Debian-based system also has seven run-levels. Run levels 0, 1, and 6 are the same as in a Red Hat-based system. Run levels two through five are identical, but can be configured in whatever way you desire. The default configuration boots the system into run level two which is configured as full multi-user mode with graphics (X windows).

You can view the current run level with this command:

The display will indicate the current and previous run level, separated by a space.

You can change the current run level with this command:
#init [desired run level] or #telinit [desired run level]

Controlling Run Levels

Change the default run level by modifying /etc/inittab. Look for a line near the top of the file similar to this:


The number in the line is the default run level. You can modify it with your favorite text editor to whatever value you want, obviously avoiding 0, 1, and 6.

Control daemons (services) at boot time by modifying scripts within “rc” directories.

  • In Red Hat Linux, they’re in the /etc/rc.d directory
  • In SuSE Linux, they’re in /etc/init.d/rc
  • In Debian Linux, they’re in /etc

There is an “rc” directory that corresponds to each run level. For example, rc3.d corresponds to run level 3. Look for the corresponding directory to the run level you wish to modify. Within that directory, you’ll find links to scripts for each of the services on the system. Each link name includes an “S” or a “K”. Those whose names start with “S” start indicated daemons with the directory’s run level. Those whose names start with “K” kill daemons within the directory’s run level. (Scripts in an rc directory are executed in alphabetical, then numerical order.)

You’ll also notice scripts in /etc/rc.d called rc, rc.local, and rc.sysinit. The rc script is responsible for starting and stopping services when runlevels change, rc.sysinit runs once at boot time before all other rc scripts, and rc.local runs after all the other init scripts. You can put your own initialization scripts in rc.local instead of working through the System V runlevels.