Sun. Sep 20th, 2020

5 chkconfig Command Examples in CentOS

2 min read

The Chkconfig command is used to set, view or change the services that are automatically started at startup. Five practical examples to illustrate the use of the Chkconfig command.

1. Using Shell Script to Detect Service System Startup Item Status
When you execute the chkconfig command with only the service name, it returns true if the service has been configured to the system startup item. The following code snippet is used to check whether a service has been configured to start up.

# vi check.sh
chkconfig network && echo “Network service is configured”
chkconfig junk && echo “Junk service is configured”

# ./check.sh
Network service is configured

You can also specify to check whether the service is configured to the specified run level.

# vi check1.sh
chkconfig network –level 3 && echo “Network service is configured for level 3”
chkconfig network –level 1 && echo “Network service is configured for level 1”

# ./check1.sh
Network service is configured for level 3

2. View the status of current service system startup items

The –list option is used to display the status of the system startup items for all current services.

You can use grep to filter and display services with specified conditions.
The following command shows only services with run level 3.

chkconfig –list | grep 3:on

The following command shows only the startup item status of the network service.

chkconfig –list | grep network

3. Add a new service to the startup
Use the –add option to add a specified service to the system startup service list.
The following example shows how to add a new service (such as iptables) to the list of services that need to be started. The “chkconfig -add” command will also automatically enable runlevels 2, 3, 4, and 5, as follows:
# chkconfig –list | grep iptables
# chkconfig –add iptables
# chkconfig –list | grep iptables
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
4. Remove a service from the system startup item list

The following example shows that ip6tables has been configured to the startup item.
# chkconfig –list | grep ip6tables
ip6tables 0:off 1:off 2:off 3:on 4:off 5:off 6:off
Remove it from the launch list with the –del option
# chkconfig –del ip6tables
# chkconfig –list | grep ip6tables
5. Turn the selected runlevel on or off for the service
Sometimes you may not want to remove the entire service from the startup list, but you may just want to shut down the specified runlevel.
The following example turns off runlevel 5 for the nfserver service.
# chkconfig –level 5 nfsserver off

You can also close multiple runlevels at the same time, here is the shutdown of 3 and 5 runlevels.
# chkconfig –level 35 nfsserver off