Run Levels and Start up Scripts

Linux by default has 6 run levels. By default most major Linux distributions boot on Run Level 5. Each runlevel has its own purpose and it runs its own services.

The information on how runlevels are configured can be found in ‘inittab in the /etc directory.

The SysV Start Scripts start or stop services.
The /etc/rc.d directory has the scripts that dictate the behavior for each runlevel.
In order to manage each runlevel, subdirectories have been created within /etc/rc.d for each runlevel. Each one of those directories, contains scripts that will launch or terminate services at the corresponding runlevel.
Here is a list on what each runlevel does.

 RunLevels defined in Fedora Core 6.
#   0 - halt
#   1 - Single user mode (Fail Safe)
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11 (Graphical User Interface)
#   6 - reboot (Do NOT set initdefault to this)

There is one additional directory in /etc/rc.d that acts as the master directory if you will. This directory is called /init.d and has all the scripts for all the services. The other directories that correspond for each service, contain links to the scripts in the ./init.d directory. By vieweing the ./init.d directory you can see which services are available to run. In the directories that correspond for each runlevel you can see which ones will be started or killed at each runlevel.

K in front of the script tells the system to kill the service.
S in front of the script tells the system to start the service.

The number in front of each service tells the system in what order to start the services on each runlevel, starting from the lowerst number.
It should be noted that even the default settings for runlevels of Red-Hat are the most widely accepted, each runlevel can be configured to do whatever the user wants it to do. The user can add additional runlevels if he wishes to do.

Here is an example of a Red Hat runlevel setup.

In ./init.d directory we see which services are available for use.

./init.d:
acpid           cpuspeed            halt        kdump          mysqld                    oddjobd          rpcidmapd       vncserver
anacron         crond               hidd        killall        netfs                     pand             rpcsvcgssd      winbind

In ./rc0.d: we see that all services are going to be killed. Runlevel 0 is used for shutting down the Operating System.

./rc0.d:
K01smartd                    K05anacron    K15httpd      K56acpid       K74nscd        K87mcstrans        K89netplugd     K92iptables
K02avahi-daemon              K05atd        K20nfs        K60crond       K74ntpd        K87portmap         K89pand         K95firstboot

./rc1.d: is single user mode. In here see that all services that are network related are killed besides single and cpuspeed services.

./rc1.d:
K01smartd                    K05anacron    K15httpd      K56acpid       K74nscd        K87mcstrans        K89netplugd     K92iptables
K02avahi-daemon              K05atd        K20nfs        K60crond       K74ntpd        K87portmap         S99single         S06cpuspeed

./rc2.d: is multiuser mode with out Network File Server Support.

./rc2.d:
K02avahi-daemon              K05conman     K36mysqld      K74ntpd            K89dund             S04readahead_early  S13irqbalance  S55sshd
K02avahi-dnsconfd            K05saslauthd  K56acpid       K75netfs           K89netplugd         S06cpuspeed         S13mcstrans    S80sendmail

./rc3.d: is full multisuer mode which means this runlevel is configured to have networking features enabled.

./rc3.d:
K02avahi-dnsconfd            K35vncserver   K88wpa_supplicant   S08ip6tables       S14nfslock     S28autofs        S98avahi-daemon
K02dhcdbd                    K35winbind     K89dund             S08iptables        S15mdmonitor   S44acpid         S98haldaemon

./rc4.d: This runlevel supports a multiuser environment and networking.

./rc4.d:
K02avahi-dnsconfd            K24irda        K85mdmpd            S05kudzu        S13portmap         S26lm_sensors  S97yum-updatesd
K02dhcdbd                    K35vncserver   K88wpa_supplicant   S06cpuspeed     S13setroubleshoot  S28autofs      S98avahi-daemon

./rc5.d: Is a full multiuser and graphical environment runlevel

./rc5.d:
K02avahi-dnsconfd            K35vncserver   K88wpa_supplicant   S08iptables        S15mdmonitor   S44acpid            S98avahi-daemon
K02dhcdbd                    K35winbind     K89dund             S09isdn            S18rpcidmapd   S55cups             S98haldaemon

./rc6.d: This runlevel is used to restart the system.

./rc6.d:
K01smartd                    K05anacron    K15httpd      K56acpid       K74nscd        K87mcstrans        K89netplugd     K92iptables
K02avahi-daemon              K05atd        K20nfs        K60crond       K74ntpd        K87portmap         K89pand         K95firstboot

inittab Options Explained

This section includes parts of the manual page about inittab in combination with comments I have included.

This file describes how the INIT process should set up the system at each run-level.
The format of entry lines in inittab is as follows.
id:runlevel:action:process
id: Function identifier.
runlevels: The runlevel (0 to 6) for which this line refers to. It can be one number or
            multiple numbers in order to apply a paramater in multiple levels.
action: Specifies how to treat the process that will be launched.
process: Which process to run.

Most common options in the /etc/inittab file

  • si::sysinit:/etc/rc.d/rc.sysinit

System initialization. This is the first script that is run.
Among others it determines the hostname, loads kernel modules, sets system clock, checks the status of SElinux.

  • id:5:initdefault:

This line sets the default runlevel for the system. In this case it is 5. Obviously you do not want to set this value to 0 which haults the system or to 6 where it reboots the system

  • Runlevels setup

Since the default runlevel is 5 (look above) the system read the appropriate line which in this case is l5:5:wait:/etc/rc.d/rc 5 and executes it while it passes the runlevel as an arguement. The lines here begin with the letter ‘l’ which stands for ‘level’.

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

  • ca::ctrlaltdel:/sbin/shutdown -t3 -r now

Determines what happens when you press Control+Alt+Delete. In this case it shuts down the system. In my system, I have it setup in such a way that it will display the time ‘ca::ctrlaltdel:/sbin/clock’

  • pf::powerfail:/sbin/shutdown -f -h +2 “Power Failure; System Shutting Down”

When our UPS tells us power has failed, assume we have a few minutes
of power left. Schedule a shutdown for 2 minutes from now.
This does, of course, assume you have powerd installed and your
UPS connected and working correctly.

  • pr:12345:powerokwait:/sbin/shutdown -c “Power Restored; Shutdown Cancelled”

If power was restored before the shutdown kicked in, cancel it.

  • TeleTYpe writers
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6

getty is a program that displays the login promt
mingetty mingetty is a minimal getty for use on virtual consoles.
The above section tells the system to run 6 terminals for runlevels 2 to 5. You can switch to these terminals by pressing control+Alt+F1 to F6.

  • x:5:respawn:/etc/X11/prefdm -nodaemon

Runs Display manager at Runlevel 5
prefdm Is a script that determines which display manager to use. If none are set in /etc/sysconfig/desktop it cycles through gmdkdm and finally xdm.


Valid actions that can be passed in inittab (copied from the man page):
              respawn The process will be restarted whenever it terminates (e.g. getty).

              wait The process will be started once when the
              specified runlevel is entered and init will wait for its termination.
              once The process will be executed once when the specified runlevel is entered.
              boot The process will be executed during system boot.
              The runlevels field is ignored.

              bootwait The process will be executed during system boot,
              while init waits for its termination (e.g. /etc/rc). The runlevels field is ignored.

              off This does nothing.
              ondemand A process marked with an ondemand runlevel will be
              executed whenever the specified ondemand runlevel is called.
              However, no runlevel change will occur (ondemand runlevels are 'a', 'b', and 'c').

              initdefault An initdefault entry specifies the runlevel which should be
              entered after system boot. If none exists, init will ask for a runlevel
              on the console. The process field is ignored.
              sysinit The process will be executed during system boot.
              It will be executed before any boot or bootwait entries. The runlevels field is ignored.
              powerwait The process will be executed when the power goes down.
              Init is usually informed about this by a process talking to a UPS connected
              to the computer. Init will wait for the process to finish before continuing.
              powerfail As for powerwait, except that init does not wait for the process's
              completion.
              powerokwait This process will be executed as soon as init is informormed
              that the power has been restored.
              powerfailnowThis process will be executed when init is told that
              the battery of the external UPS is almost empty and the power is failing
              (provided that the external UPS and the monitoring process are able
              to detect this condition).
              ctrlaltdel The process will be executed when init receives
              the SIGINT signal. This means that someone on the system console has
              pressed the CTRL-ALT-DEL key combination.
              Typically one wants to execute some sort of shutdown either to get
              into single-user level or to reboot the machine.

              kbrequest The process will be executed when init receives
              a signal from the keyboard handler that a special key combination
              was pressed on the console keyboard. The documentation for this
              function is not complete yet; more documentation can be found in
              the kbd-x.xx packages (most recent was kbd-0.94 at the time of this writing).
              Basically you want to map some keyboard combination to the "KeyboardSignal" action.
              For example, to map Alt-Uparrow for this purpose use the following in your
              keymaps file: alt keycode 103 = KeyboardSignal

Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.