CentOS/RHEL provides us with a simple command-line tool (chkconfig) for managing services that are started during the various runlevels of your system. chkconfig requires some additional comment lines in the actual init script to tell it in which run levels the service should be started, and when, relatively, the service should be started during the initialization of the run level. (init scripts are processed in a specific order to ensure that services dependent on others are started after the services they depend on.) These lines, taken from the httpd init script, are as follows:
# chkconfig: 345 85 15 # description: Apache is a World Wide Web server. It is used to serve # HTML files and CGI.
345 – runlevels that the service will be enabled for by default.
85 – start priority. The lower the number the higher the priority and the sooner a service will be started within a given runlevel.
15 – stop priority. The lower the number the higher the priority and the sooner a service will be stopped within a given runlevel.
Listing Services by Using chkconfig
To get a list of which services are started at which run level, use the command “chkconfig –list“.
# chkconfig --list acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off cgconfig 0:off 1:off 2:off 3:off 4:off 5:off 6:off ...
Optionally, you can add a name as an additional argument, and chkconfig will list only the information for that service. Following is the output of chkconfig –list iptables on my system:
# chkconfig --list iptables iptables 0:off 1:off 2:off 3:on 4:on 5:on 6:off
In this case, chkconfig reports that the iptables service is to be started for run levels 3, 4, and 5.
Enabling or Disabling a Service at boot
In this example, we will use iptables service. If you want, list the currents rulevels where the services will be starting:
# chkconfig --list iptables httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
“chkconfig on” without specifying any runlevel will enable the service on runlevel 2,3,4 and 5. For example:
# chkconfig iptables on
# chkconfig --list iptables iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Similarly, to disable the service at all run levels, use the “chkconfig off” command. For example:
# chkconfig iptables off
# chkconfig --list iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
chkconfig Fine Control
The –level option can be given to chkconfig to specify which runlevels to make the change (either on or off). Other runlevels will not be altered. This would configure the system to start iptables in runlevels 3 and 5:
# chkconfig --level 35 iptables on
# chkconfig --list iptables iptables 0:off 1:off 2:off 3:on 4:off 5:on 6:off
Adding a Service by Using chkconfig
To add a new service to all run levels according to the recommendations given to chkconfig, use the following command:
# chkconfig --add [servicename]
chkconfig sets all the links for the service in the correct directories in one swoop.
Resetting Service Information
Playing with services is educational, as long as you have a backup of your /etc/rc.d directory tree and a way to get back into the system to restore it. However, this type of drastic action is usually not necessary. Instead, you can restore the service’s startup priority and other information to the recommended settings by issuing the following command.
# chkconfig [servicename] reset
This command returns everything to a (hopefully) sane default.
Removing a service using chkconfig
If you no longer require the use of a service, you can disable it at boot by using the “chkconfig off” switch:
# chkconfig [servicename] off
You should then proceed to stop the service from running with the following command:
# service [servicename] stop
The preceding command will take immediate effect. However, in order to finalize this procedure you may want to remove it from the chkconfig management tool by typing:
# chkconfig --del [servicename]