Using chkconfig to control initscripts

Dealing with Linux initscripts is a world unto itself for most users. Without the knowledge of an absolute time-saver like chkconfig, you might find yourself hacking away at scripts all night. Vincent Danen shows you around this amazing tool.

This month, instead of looking at an actual script, we'll look instead at manipulating your system initscripts (initialization scripts). Anyone using a flavor of Linux that uses the Sys-V style initscripts (distributions like Linux-Mandrake, Red Hat, Connectiva, and so on) should be familiar with the chkconfig tool to control startup programs via their initscripts; it is an absolute time-saver.

What chkconfig does is handle how each startup service that uses an initscript is called and in which runlevel. Under Linux, you typically have six different runlevels that are executed, depending on how the system is started or stopped. For instance, runlevel 3 is typically multiuser console mode, while runlevel 5 is typically multiuser X mode, which will start the X server automatically on boot. Some distributions, like SuSE, use different runlevels for different things, so if you're unaware of what a particular runlevel is used for, you can check the /etc/inittab file, which should have a description of each runlevel.

The initscripts that we deal with are typically found in /etc/rc.d/init.d or /etc/init.d (again, depending on the distribution), and these are scripts that start and stop local daemons. Usually, they are used for starting or stopping network services, like a Web or FTP server, but they can also be used to start local services, like gpm for console mouse access.

During bootup, the system will execute scripts found in a directory that corresponds with the executing runlevel. For instance, if you boot into runlevel 3, scripts will be executed that are found in /etc/rc.d/rc3.d on a Linux-Mandrake system. If you look in this directory, you will see a number of symbolic links that point to the real files, which are found in the /etc/rc.d/init.d directory. There will be one difference, however, in that the symbolic link starts with S or K and then a number. The S denotes a starting script, and the K denotes a killing script. These scripts will be executed with a “start” or “stop” command, respectively. The number following the S or K is the order in which they will be executed. An S12syslog link will be started before an S55sshd link. The same is true for the K scripts.

In the old days, you had to manually maintain these symbolic links to the initscripts by hand. For instance, if you wanted to start the Apache Web server on runlevel 3, you would have to go into the /etc/rc.d/rc3.d directory and manually create the symbolic link. Luckily, this is no longer the case, because chkconfig handles all of this for you.

As root, type the following to get a list of all services chkconfig handles:
chkconfig —list

This will print out a list of all the initscripts that are under the control of chkconfig (see Table 1):

Table 1
httpd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
atd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
An example of initscripts under the control of chkconfig

Here we see that the httpd initscript is started in runlevels 3, 4, and 5. The mysql initscript is started in runlevels 2, 3, 4, and 5, while the atd initscript is not started at all.

If you wanted to make Apache start in runlevel 2 as well, you would use the following:
chkconfig —level 2 httpd on

This tells chkconfig to create a symbolic link in /etc/rc.d/rc2.d that will start the httpd initscript. So when you boot into runlevel 2 now, Apache will start. Likewise, if there is a service that is currently running that you don't want to have started, you can use chkconfig to turn it off. If, for example, you want to turn MySQL off for runlevels 2 and 5, you can use:
chkconfig —level 25 mysql off

If you want to turn MySQL off for all runlevels, you can use:
chkconfig —del mysql

If, at a later point, you want to turn MySQL back on, you can use:
chkconfig —add mysql

to re-enable it with its default settings.

Finally, if you want to simply list the settings for one service, you can do so by using:
chkconfig —list httpd

to see which runlevels httpd is configured to run in.

You can do this with any initscript as found in /etc/rc.d/init.d. Using chkconfig is much easier than manually maintaining the symbolic links, and it makes your system much cleaner; more often than not, using chkconfig will ensure things work the way they are supposed to. With later versions of chkconfig, you can even use it to turn on and off xinetd services, which are typically found in the /etc/xinetd.d directory. xinetd is a replacement for the old inetd “super-daemon,” which many older distributions used to start services such as FTP and telnet so that you did not need to have a number of stand-alone daemons running.

For those of you unlucky enough to run a distribution that does not contain chkconfig, you may have to manually maintain your symbolic links for initscript startup. However, most recent distributions either contain chkconfig itself or have an alternative program that does the same thing. I believe that in SuSE, the YaST configuration tool will do this for you, and in Linux-Mandrake you can use chkconfig on the command line or DrakConf in a GUI to control which programs start in a certain runlevel.

By Vincent Danen

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.