Runlevels

All files related to system startup are located in the directory /etc/rc.d. Here is the list of the files:

$ ls /etc/rc.d
init.d/  rc.local*    rc0.d/  rc2.d/  rc4.d/  rc6.d/
rc*      rc.sysinit*  rc1.d/  rc3.d/  rc5.d/

As we just discussed, rc.sysinit is the first file run by the system. This file is responsible for setting up the basic machine configuration: keyboard type, configuration of certain devices, file system checking, etc.

Then the rc script is run, with the desired runlevel as an argument. As we have seen, the runlevel is a simple integer, and for each runlevel <x> defined, there must be a corresponding rc<x>.d directory. In a typical Mandrake Linux installation, you might therefore see that that there are six runlevels:

Let's take a look at the contents of directory rc5.d:

$ ls rc5.d
K15postgresql@  K60atd@      S15netfs@   S60lpd@        S90xfs@
K20nfs@         K96pcmcia@   S20random@  S60nfs@        S99linuxconf@
K20rstatd@      S05apmd@     S30syslog@  S66yppasswdd@  S99local@
K20rusersd@     S10network@  S40crond@   S75keytable@
K20rwhod@       S11portmap@  S50inet@    S85gpm@
K30sendmail@    S12ypserv@   S55named@   S85httpd@
K35smb@         S13ypbind@   S55routed@  S85sound@

As you can see, all the files in this directory are symbolic links, and they all have a very specific form. Their general form is:

<S|K><order><service_name>

The S means Start service, and K means Kill (stop) service. The scripts are run in ascending numerical order, and if two scripts have the same number, ascending alphabetical order will apply. We can also see that each symbolic link points to a given script located in /etc/rc.d/init.d (other than the local script which is responsible for controlling a specific service.)

When the system goes into a given runlevel, it starts by running the K links in order: rc looks where the link is pointing, then calls up the corresponding script with the single argument stop. It then runs the S scripts using the same method, except that the scripts are called with a start parameter.

So, without mentioning all the scripts, we can see that when the system goes into runlevel 5, it first runs K15postgresql, i.e. /etc/rc.d/init.d/postgresql stop. Then K20nfs, then K20rstatd, etc, until it runs the last command. Next, it runs all the S scripts: first S05apmd, which itself calls /etc/rc.d/init.d/apmd start, and so on.

Armed with this information, you can create your own complete runlevel in a few minutes, or prevent a service starting or stopping by deleting the corresponding symbolic link. You can also use a number of interface programs for doing this, notably drakxservices which uses a graphical program interface and chkconfig for text-mode configuration.