• Controlling Services With chkconfig

    Many 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”.

    This entry was posted on Wednesday, November 15th, 2006 at 7:46 pm and is filed under Data and Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 13 Comments

    Take a look at some of the responses we've had to this article.

    1. Nice article Cliff.

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

      #chkconfig: 235 19 08

    2. Nov 16th
      Reply

      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. Nov 16th
      Reply

      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. Nov 21st
      Reply

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

    5. [...] 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. [...] Remember to use “chkconfig” to make sure “ntpd” is enabled to come up when the system starts. [...]

    7. anonymous
      Mar 7th
      Reply

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

    8. May 10th
      Reply

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

    9. May 10th
      Reply

      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. [...] asterisk service with chkconfig and service.) [...]

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

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

    13. zawmn
      Aug 21st
      Reply

      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?

  • Leave a Reply

    Let us know what you thought.

  • Name (required):

    Email (required):

    Website:

    Message:

Visitors have tagged this post: chkconfig howto (1104) - t (820) - rhel chkconfig (320) - chkconfig tutorial (303) - chkconfig syntax (202) - chkconfig script (175) - linux chkconfig tutorial (162) - chkconfig add example (151) - chkconfig add service (129) - rhel service (119) - rhel services (115) - chkconfig example (110) - chkconfig order (110) - chkconfig rhel (104) - chkconfig examples (103) - chkconfig scripts (102) - chkconfig disable (84) - chkconfig (75) - how to use chkconfig (71) - chkconfig sshd (68) - chkconfig file (68) - howto chkconfig (65) - rhel add service (63) - chkconfig numbers (60) - chkconfig: 2345 (56) - chkconfig service (50) - chkconfig ssh (50) - add service to chkconfig (49) - chkconfig start order (48) - linux chkconfig example (47) - rhel4 chkconfig (44) - chkconfig sshd on (43) - chkconfig solaris (42) - rhel service command (42) - chkconfig format (39) - rhel4 services (35) - what is chkconfig (35) - service does not support chkconfig (35) - add service to runlevel (34) - "chkconfig add service" (34) - chkconfig kill (33) - chkconfig Linux example (33) - chkconfig change order (31) - add service chkconfig (31) - chkconfig unrecognized service (30) - solaris chkconfig (29) - chkconfig start (29) - chkconfig 235 (28) - chkconfig services (28) - how to use chkconfig in linux (26) - chkconfig compatible (26) - chkconfig how to (25) - linux enable service (25) - chkconfig 2345 (25) - chkconfig: 2345 55 25 (24) - service rhel (24) - f (24) - "chkconfig --add" example (23) - chkconfig custom script (23) - adding service to chkconfig (22) - linux enable services (22) - script chkconfig (21) - rhel 5 chkconfig (21) - how to chkconfig (20) - service chkconfig (20) - chkconfig runlevels (20) - chkconfig requirements (20) - chkconfig init (20) - chkconfig rhel4 (19) - linux chkconfig order (19) - asterisk: unrecognized service (19) - linux service enable (18) - chkconfig runlevel (17) - rhel service add (17) - chkconfig turn off service (17) - init chkconfig (17) - init.d chkconfig (17) - init d chkconfig (17) - chkconfig turn on service (16) - chkconfig files (16) - sshd chkconfig (16) - chkconfig init.d (15) - Linux chkconfig script (15) - chkconfig add new service (15) - rhel4 service (14) - chkconfig disable service (14) - chkconfig in solaris (14) - enable service linux (14) - linux chkconfig howto (14) - sshd: unrecognized service (14) - chkconfig file format (13) - rhel4 add service (12) - use chkconfig (12) - add service rhel (12) - services rhel (12) - would (12) - how to add service to chkconfig (12) - chkconfig 3 (11) - chkconfig service order (11) - chkconfig script syntax (11) - chkconfig in RHEL (11) - how to enable services in linux (11) - service sshd does not support chkconfig (11) - chkconfig startup order (11) - add services to chkconfig (11) - sshd unrecognized service (11) - RHEL service control (10) - linux chkconfig syntax (10) - chkconfig off (10) - chkconfig add script (10) - add service with chkconfig (10) - how to add a service to chkconfig (10) - chkconfig new service (10) - create service chkconfig (10) - enable services in linux (10) - chkconfig usage (10) - chkconfig --list (9) - chkconfig: 235 19 08 (9) - chkconfig +init (9) - ssh chkconfig (9) - difference between chkconfig and service (9) - chkconfig --levels 235 (9) - chkconfig and service (9) - enable services linux (9) - script for chkconfig (9) - chkconfig create service (9) - how to add service in chkconfig (9) - S55sshd (8) - chkconfig example script (8) - linux chkconfig add (8) - examples of chkconfig (8) - chkconfig adding (8) - add script to chkconfig (8) - chkconfig turn service off (8) - chkconfig solaris 8 (8) - scripts chkconfig (8) - add service in chkconfig (8) - chkconfig file location (8) - chkconfig script format (8) - chkconfig rhel5 (8) - # chkconfig: 2345 55 25 (8) - ntpd: unrecognized service (8) - asterisk unrecognized service (8) - chkconfig -add + examples (7) - rhel service startup (7) - chkconfig restart (7) - linux chkconfig sshd (7) - chkconfig service on (7) - chkconfig for solaris (7) - adding services with chkconfig (7) -