Controlling Services With chkconfig

linux.jpgMany system 5 UNIX variants use scripts in the /etc/rcN.d/ directories to control which services should be started in the various runlevels. If, for instance, you wanted the secure shell daemon to run in runlevel 4, you would put a script named something like “S55sshd” in “/etc/rc4.d”. This script would usually accept the “start” “stop” and “restart” arguments, as well as the commands to perform these functions. When the system came up, it would execute “/etc/rc4.d/S55sshd start” when it transitioned into runlevel 4. On the way down, it would execute “/etc/rc4.d/S55sshd stop” as the system passed from runlevel 4 to runlevel 3. If you had made some changes to the sshd configuration file, and wanted to restart the service, you could manually execute “/etc/rc3.d/sshd restart” to kill and then restart the daemon.

Since this model involved having multiple copies of the same script in many different directories, Linux and others have adopted the standard of putting all service control scripts in “/etc/init.d/”, and using symbolic links to these scripts in the various “/etc/rcN.d/” directories. This allowed for the SGI IRIX innovation of the “chkconfig” command, which is command line tool that manages the symbolic links for you.

How to use “chkconfig” in Red Hat Enterprise Linux:

First, all your service control scripts need to be in the “/etc/init.d/” directory. They should reflect the name of the service they control. In our example, the file is named /etc/init.d/sshd”.

Secondly, they have a tag at the head of the script that looks something like this so that “chkconfig” understands that it can controll it:

# Basic support for IRIX style chkconfig
###
# chkconfig: 2345 55 25
# description: Manages the services you are controlling with the chkconfig command
###

The first set of numbers “2345″ is are the default runlevels for the service, and “55″ and “25″ represent the name of the “S” and “K” symbolic links, and the order in which the service will be started and stopped in the respective runlevel. You will need to change these last two numbers, making them unique.

Once these requirements are met, using the command is fairly simple. When we go into /etc/rc3.d, we see a file called “S55sshd”.

[root@calvin rc2.d]# cd /etc/rc3.d
[root@calvin rc2.d]# ls -al S55sshd
lrwxrwxrwx 1 root root 14 Nov 15 15:10 S55sshd -> ../init.d/sshd

We see this file is a symbolic link to “../init.d/sshd”. Let’s run the “chkconfig” command to turn sshd off.

[root@calvin init.d]# /sbin/chkconfig sshd off
[root@calvin init.d]# /sbin/chkconfig –list sshd
sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

chkconfig --list sshd confirms that sshd has been disabled in all runlevels, and the symbolic link has been removed from all “/etc/rcN.d/” directories.

Let’s turn sshd back on:

[root@calvin init.d]# /sbin/chkconfig sshd on
[root@calvin rc2.d]# /sbin/chkconfig –list sshd
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

chkconfig --list sshd confirms that sshd has now been enabled in runlevels 2, 3, 4 and 5, and we see s symbolic link to “/etc/init.d/sshd” named “S55sshd” in “/etc/rc2.d/”, “/etc/rc3.d/”, “/etc/rc4.d/” and “/etc/rc5.d/”.

Let’s imagine now that we only want sshd to be enabled in runlevel 5. We run the following command to accomplish this:

[root@calvin rc2.d]# /sbin/chkconfig sshd –level 234 off
cd /etc/[root@calvin rc2.d]# /sbin/chkconfig –list sshd
sshd 0:off 1:off 2:off 3:off 4:off 5:on 6:off

chkconfig --list sshd confirms that sshd has been disabled in all runlevels except 5, and the “S55sshd” has been removed from “/etc/rc2.d/”, “/etc/rc3.d/” and “/etc/rc4.d/”.

There is, of course, more to it, but this should get you well on your way to happily managing your system services with “chkconfig”.

Discuss This Article


13 Responses to “Controlling Services With chkconfig”

  1. Jon Emmons on November 15th, 2006 9:36 pm #

    Nice article Cliff.

    Any insight on what the numbers on this line are for?

    #chkconfig: 235 19 08

  2. cliff on November 16th, 2006 10:23 am #

    Sure… Good question.

    The “chkconfig: 235″ section indicates the the default runlevels. For instance, if we ran “chkconfig sshd on”, it would be active in runlevels 2,3 and 5.

    The “19″ and “08″ numbers indicate the order of startup and kill. This means that using this tag, the startup symbolic link would be named “S19sshd” and the symbolic link to kill the process would be named “K08sshd”.

  3. cliff on November 16th, 2006 11:36 am #

    Update:

    I have changed “#chkconfig: 235 19 08″ in the story to “#chkconfig: 2345 55 25″ so that it works with the rest of the examples.

    “#chkconfig: 235 19 08″ actually came from the VmWare script, but it was nicely crafted, making it easy to understand what it is doing.

    I’ve also included a quick explanation of what these numbers mean.

  4. Mike D on November 21st, 2006 12:24 am #

    Yeah, “chkconfig” is a nice management tool. Run level 4 is unused, so your example might confuse some n00bs (lol h4×0r!)

  5. spiralbound.net » Setting Up The Automounter Service on RHEL on December 6th, 2006 12:27 pm #

    [...] That should pretty much do it. If you don’t have autofs configured to start up, you can use chkconfig to enable it. “/misc/backups” will now be mounted whenever a user or process attempts to access data on it, and it will be automatically disconnected after 120 seconds of inactivity. Last, but not least, you can always confirm that it is running with the “service” command: [...]

  6. spiralbound.net » Getting ntpd to work correctly on RHEL on December 19th, 2006 10:50 am #

    [...] Remember to use “chkconfig” to make sure “ntpd” is enabled to come up when the system starts. [...]

  7. anonymous on March 7th, 2007 3:05 am #

    Thanks for the nice explanation of how-to do it.

  8. Kaumil on May 10th, 2007 12:18 am #

    what is the difference between run level 3 and run level 4 in linux.

  9. cliff on May 10th, 2007 10:07 pm #

    Well, by default, runlevel 4 is unused. If you were to enter runlevel 4 on a new system, it would look exactly the same as runlevel 5. It is there, however, so you can controll custom items with it in case you find yourself needing a middle ground between runlevel 3 and 5. 3, of course is full multiuser mode without X, while 5 is full multiuser mode with X. You would use runlevel 4 if you still wanted X running, but not everything normally running in 5.

  10. spiralbound.net » Quick Start Guide for Asterisk on June 20th, 2007 11:43 am #

    [...] asterisk service with chkconfig and service.) [...]

  11. spiralbound.net » Example LINUX init Script on July 23rd, 2007 5:22 pm #

    [...] script is chkconfig compatible, so call it the name of your new service and put it in [...]

  12. pk stuff » Example Linux chkconfig/init Script on March 18th, 2008 4:09 pm #

    [...] script is chkconfig compatible, so call it the name of your new service and put it in [...]

  13. zawmn on August 21st, 2008 8:37 am #

    I’m starting openoffice as startup service according the follow url
    http://little.bluethings.net/2008/05/30/automating-document-conversion-in-linux-using-jodconverterooo/

    [root@suse ~]# sudo service openoffice start
    openoffice: unrecognized service

    What is this error?


Got something to say?






Post Tags (user submitted): t (820) - chkconfig howto (275) - rhel chkconfig (140) - chkconfig script (87) - chkconfig tutorial (86) - rhel service (59) - rhel services (51) - chkconfig (47) - chkconfig scripts (41) - chkconfig examples (39) - chkconfig: 2345 (36) - chkconfig rhel (35) - "chkconfig add service" (34) - chkconfig service (32) - chkconfig disable (31) - chkconfig solaris (29) - rhel4 chkconfig (29) - rhel add service (27) - chkconfig sshd (26) - howto chkconfig (26) - rhel4 services (24) - chkconfig syntax (24) - f (24) - chkconfig numbers (23) - "chkconfig --add" example (23) - chkconfig file (23) - rhel service command (22) - chkconfig services (21) - chkconfig example (19) - how to use chkconfig (19) - chkconfig 235 (18) - solaris chkconfig (17) - chkconfig start (15) - chkconfig rhel4 (15) - chkconfig runlevel (14) - service chkconfig (14) - chkconfig ssh (14) - chkconfig order (14) - service rhel (13) - chkconfig runlevels (13) - how to chkconfig (12) - chkconfig: 2345 55 25 (12) - rhel4 service (11) - add service to chkconfig (10) - linux chkconfig order (10) - chkconfig how to (9) - chkconfig +init (9) - chkconfig kill (9) - linux chkconfig tutorial (9) - chkconfig requirements (9) - linux enable services (9) - rhel4 add service (8) - add service to runlevel (8) - chkconfig change order (8) - linux chkconfig howto (8) - chkconfig sshd on (8) - chkconfig --list (7) - chkconfig -add + examples (7) - rhel service startup (7) - how to use chkconfig in linux (7) - difference between chkconfig and service (7) - rhel4 service command (7) - chkconfig adding (7) - chkconfig script syntax (7) - chkconfig off (7) - solaris chkconfig command (6) - chkconfig restart (6) - adding services with chkconfig (6) - chkconfig in solaris (6) - add service rhel (6) - chkconfig 3 (6) - linux chkconfig add (6) - linux chkconfig syntax (6) - chkconfig install service (6) - chkconfig: 2345 55 10 (5) - chkconfig disable service (5) - RHEL service control (5) - "chkconfig --add" (5) - use chkconfig (5) - linux chkconfig service (5) - rhel service add (5) - chkconfig start kill (5) - chkconfig turn off service (5) - chkconfig script requirements (5) - adding service to chkconfig (5) - chkconfig command in solaris (5) - "tutorial chkconfig" (5) - chkconfig sshd off (5) - chkconfig start order (5) - ‘chkconfig’ and ‘service’ (5) - linux service enable (5) - chkconfig in RHEL (5) - RHEL service list (5) - how to enable services in linux (5) - chkconfig and service command (5) - chkconfig: 235 19 08 (4) - rhel service start order (4) - rhel4 runlevels (4) - services chkconfig (4) - RHEL service enable (4) - chkconfig for solaris (4) - adding a service to chkconfig (4) - chkconfig solaris 9 (4) - init chkconfig (4) - chkconfig on solaris (4) - rhel startup services (4) - linux chkconfig (4) - linux chkconfig example (4) - chkconfig adding service (4) - chkconfig adding services (4) - Basic support for IRIX style chkconfig (4) - linux chkconfig add service (4) - rhel4 services list (4) - linux: chkconfig runlevel (4) - chkconfig change level (4) - "add service" chkconfig (4) - chkconfig --levels 235 (4) - howto use chkconfig (4) - rhel4 service restart (4) - "Linux" "chkconfig" "file" (4) - rhel service chkconfig (4) - chkconfig solaris 8 (4) - chkconfig add custom script (4) - script for chkconfig (4) - script chkconfig (4) - scripts chkconfig (4) - chkconfig files (4) - chkconfig file format (4) - disable chkconfig (4) - chkconfig howto install (4) - all (3) - init.d chkconfig 2345 (3) - RHEL chkconfig add service (3) - rhel4 service control (3) - S55sshd (3) - add service to rc3 (3) - adding service to runlevel (3) - chkconfig RHEL 4 (3) - chkconfig script howto (3) - "rhel 4" chkconfig (3) - linux chkconfig sshd (3) - RHEL4 service list (3) - chkconfig service on (3) - chkconfig on sshd (3) - rhel disable services (3) - chkconfig turn service on (3) - rhel add service to startup (3) - rhel service start (3) - chkconfig add startup (3) - runlevel rhel (3) -