Bootstrap FreeKB - Linux Commands - chkconfig (runlevel)
Linux Commands - chkconfig (runlevel)

Updated:   |  Linux Commands articles

The chkconfig command is used to view and edit a service to be on or off at a particular runlevel.

IMPORTANT

chkconfig is used by systems using init. systemctl is used by systems using systemd.

 

The ps command can be used to determine if your system is using init or systemd. If PID 1 is init, then you will use chkconfig.

UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 Mar08 ?        00:00:01 /sbin/init

 

If PID 1 is systemd, then you will use systemctl.

ps -ef | head
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 Mar01 ?        00:06:47 /usr/lib/systemd/systemd --switched-root --system --deserialize 22

 

The chkconfig command without any options can be used to determine if a service is on or off at a particular runlevel. If you are not familar with runlevels, check out our article on runlevels.

/sbin/chkconfig
. . .
netconsole    0:off  1:off  2:off  3:off  4:off  5:off  6:off
network       0:off  1:off  2:on   3:on   4:on   5:on   6:off

 

The --list option can be used to only display a certain service.

/sbin/chkconfig --list network
. . .
network       0:off  1:off  2:on   3:on   4:on   5:on   6:off

 

If you attempt to list a service that is not in chkconfig, something like this will be displayed.

service httpd supports chkconfig, but it is not referenced in any runlevel (run 'chkconfig --add httpd')

 

chkconfig gets services from files in the /etc/rc.d/init.d directory. Here is an example of the content of a file.

#!/bin/bash
#
# chkconfig: 345
# description: start httpd

# Source function library
. /etc/rc.d/init.d/functions

case $1 in
  start)
    /opt/apache/bin/apachectl -k start     
    ;;
  stop)
    /opt/apache/bin/apachectl -k stop
    ;;
  *)
    echo "Usage $0 start|stop|restart"
    ;;
esac


The --add option is used to add a service to chkconfig. In this example, the httpd service is added to chkconfig. By default, the service will be off at runlevels 0 1 2 6 and on at runlevels 3 4 5.

/sbin/chkconfig --add httpd

 

The --del option is used to delete a service from chkconfig. In this example, the httpd service is removed from chkconfig.

/sbin/chkconfig --del httpd

 

The chkconfig service_name on or chkconfig service_name off commands can be used to turn a service on or off. This will turn the service on or off at every runlevel. For example, to turn the HTTPD service on or off.

/sbin/chkconfig httpd on
/sbin/chkconfig httpd off

 

To only change the runlevel status for certain runlevels, use the --level option. 

/sbin/chkconfig httpd on --level 345

 

When using the chkconfig command to change the runlevel of a service, a file in the /etc/init.d/ directory will be updated to contain the new runlevel. For example, the prior commands turns on the HTTPD deamon for runlevels 3, 4, and 5. This command updates the /etc/init.d/httpd file to contain the following. Even though this line is commented out, this still turns on the HTTPD deamon for runlevels 3, 4, and 5.

#!/bin/sh
. . .
# chkconfig: 345 95 05	

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 97ca11 in the box below so that we can be sure you are a human.