2. Runlevels

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

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

As already stated, rc.sysinit is the first file run by the system. It 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 Mandriva Linux installation, you might therefore see that there are six runlevels:

Let's take a look at the content of the rc3.d directory:

$ ls /etc/rc.d/rc3.d/
K09dm@         S12syslog@   S24messagebus@     S40atd@         S91dictd-server@
S01udev@       S13partmon@  S25haldaemon@      S55sshd@        S92lisa@
S03iptables@   S15cups@     S25netfs@          S56ntpd@        S95kheader@
S05harddrake@  S17alsa@     S29numlock@        S56rawdevices@  S99local@
S10network@    S18sound@    S33nifd@           S75keytable@
S11shorewall@  S20xfs@      S34mDNSResponder@  S90crond@
$

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) it. The scripts are run in ascending numerical order, and if two scripts have the same number, the ascending alphabetical order will apply. We can also see that each symbolic link points to a given script located in the /etc/init.d directory (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: the rc command looks at where the link is pointing, then calls up the corresponding script with a 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 discussing all the scripts, we can see that when the system goes into runlevel 3, it first runs the K09dm command, (i.e. /etc/init.d/dm stop). Next, it runs all the S scripts: first S01udev, which in turn calls /etc/init.d/udev start, then S03iptables, and so on.

Armed with this information, you can create your own complete runlevel in a few minutes (using runlevel 4, for instance), or prevent a service from starting or stopping by deleting the corresponding symbolic link.

2.1. Configuring Services on Run Levels

You can also use the chkconfig command to add, remove, activate or deactivate services from given run levels. Use chkconfig --add service_name to add (activate) the service service_name to all supported[30]run levels and chkconfig --del service_name to remove (deactivate) the named service from all run levels.

[Tip]Tip

Issue chkconfig --list to see which services are available, their names, and their status in all defined run levels.

Issuing chkconfig --levels 35 sshd on will activate the SSH server (sshd) on run levels 3 and 5, while issuing chkconfig --levels 3 sound off will remove sound support from run level 3. If you omit the --levels levels_list parameter, the named service will be activated or deactivated on run levels 2, 3, 4 and 5. Note however, that you could end up enabling services on run levels without proper support for those services, so it's better to specify the run levels to be affected.

2.2. Controlling Services On a Running System

Services can be controlled on a running system with the service command whether or not they are configured to be run on a particular run level. Its syntax is as follows:

service service_name action

Where service_name is the name of the service to control as listed by chkconfig --list, and action is one of:

start

Starts the named service. Please note that most services will warn you when they are already started and you pretend to start them again: use restart instead, see below.

stop

Stops the named service. Please note that all users connected to this service will be automatically disconnected when you stop the service.

restart

Stops and then starts the named service. It is the equivalent of running service service_name stop && service service_name start. Please note that all users connected to this service will be automatically disconnected when you restart the service.

other actions that are service-dependent

Different services support different actions (the previous ones are supported by all services). For example reload to reload the service's configuration file without restarting the service; force-stop to force shutdown of the service; status to be informed of the service's status; etc. Run service service_name to be informed of all actions supported by the named service.



[30] Supported” run levels mean that, for example, network-related services will not be added to run level 2, which doesn't support networking.