Detailed explanation of chkconfig command in Linux system

Time:2021-2-27

Chkconfig command can be used to check and set various services of the system
Usage syntax:
Chkconfig [– add] [– del] [– list] [system service] or chkconfig [– level < level code >] [system service] [on / off / reset]
Parameter usage:
–Add à adds the specified system service to enable chkconfig command to manage it. At the same time, it adds relevant data in the system startup description file.
–Del à delete the specified system service, which is no longer managed by chkconfig command, and delete the related data in the system startup description file at the same time.
–Level < level code > specifies in which execution level the read system service should be turned on or off.
Usage example:
Chkconfig — list all system services
Chkconfig — add httpd add httpd service
Chkconfig — del httpd delete httpd service
Chkconfig — level httpd 2345 on enables httpd to be on when the running level is 2, 3, 4 and 5.
 
The chkconfig command provides a simple way to set the run level of a service. For example, to set the MySQL server to run at run levels 3 and 4, you must first add MySQL as a chkconfig managed service:

Copy code

The code is as follows:

chkconfig –add mysql

Now, let’s set the service to “on” at levels 3 and 5

Copy code

The code is as follows:

chkconfig –level 35 mysql on

Set to off at other levels

Copy code

The code is as follows:

chkconfig –level 01246 mysql off

To confirm that your configuration has been modified correctly, we can list the running levels of the service, as follows:

Copy code

The code is as follows:

#chkconfig –list mysql

mysql           0:off       1:off       2:off       3:on 4:off       5:on 6:off

The chkconfig command is used to set, view, or change the configuration of services that are automatically started after power on. Here are seven practical examples to illustrate how to use 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, if the service has been configured to the system boot entry, it returns true. The following code segment is used to check whether a service has been configured to start.

Copy code

The code is as follows:

# vi check.sh

chkconfig network && echo “Network service is configured”
chkconfig junk && echo “Junk service is configured”

Copy code

The code is as follows:

# ./check.sh

Network service is configured

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

Copy code

The code is as follows:

# 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”

Copy code

The code is as follows:

# ./check1.sh

Network service is configured for level 3

2. View the status of the current service system startup item
The – list option is used to display the system boot item status of all current services.

Copy code

The code is as follows:

# chkconfig –list

abrtd   0:off   1:off   2:off   3:on    4:off   5:on    6:off
acpid   0:off   1:off   2:off   3:off   4:off   5:off   6:off
atd     0:off   1:off   2:off   3:on    4:on    5:on    6:off

Grep filtering can be used to display services with specified conditions.
The following command shows that only services at run level 3 are displayed.

Copy code

The code is as follows:

chkconfig –list | grep 3:on

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

Copy code

The code is as follows:

chkconfig –list | grep network

3. Add a new service to the boot entry
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 turn on runlevels 2, 3, 4 and 5, as follows:

Copy code

The code is 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 boot list
The following example shows that ip6tables has been configured to the boot item.

Copy code

The code is as follows:

# chkconfig –list | grep ip6tables

ip6tables       0:off   1:off   2:off   3:on   4:off   5:off   6:off
Use the – del option to remove it from the startup list.

Copy code

The code is as follows:

# chkconfig –del ip6tables
# chkconfig –list | grep ip6tables

5. Turns 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 is shutting down runlevel 5.0 for the nfserver service

Copy code

The code is as follows:

# chkconfig –level 5 nfsserver off

You can also shut down multiple runlevels at the same time. Here are runlevels 3 and 5.

Copy code

The code is as follows:

# chkconfig –level 35 nfsserver off

6. Script file under RC. D subdirectory
Whenever you use the chkconfig command to add or remove a service, it performs certain actions in the / etc / RC. D subdirectory.
When the chkconfig – add command is executed, it creates a matching link file in the corresponding RC directory to start and stop the service.
When the chkconfig – del command is executed, it will delete the corresponding symbolic link in the corresponding RC directory.
The following example shows that xinetd service has started runlevels 3 and 5, so xinetd will have two files in RC3. D directory and two files in RC5. D directory. When a file starts with K is shut down, use (k means kill). Start with s when starting the file (s for start).

Copy code

The code is as follows:

# chkconfig –list | grep xinetd

xinetd                    0:off  1:off  2:off  3:on   4:off  5:on   6:off
xinetd based services:

Copy code

The code is as follows:

# cd /etc/rc.d/rc3.d
# ls | grep xinetd

K08xinetd
S14xinetd

Copy code

The code is as follows:

# cd /etc/rc.d/rc5.d

# ls | grep xinetd

K08xinetd
S14xinetd
7. Add operation changes RCX. D directory
When you add a new service through chkconfig command, the default runtime will automatically start the service and create a file in the corresponding RCX directory.
For example, if the nfsserver service is not in the list of startup items, the nfsserver service does not have a file in the / etc / RC. D / RC *. D directory.

Copy code

The code is as follows:

# chkconfig –list | grep nfsserver

nfsserver                 0:off  1:off  2:off  3:off  4:off  5:off  6:off

Copy code

The code is as follows:

# ls /etc/rc.d/rc3.d | grep nfsserver
# ls /etc/rc.d/rc5.d | grep nfsserver

When you add the nfsserver service, you will see symbolic links in these directories.

Copy code

The code is as follows:

# chkconfig –add nfsserver

nfsserver                 0:off  1:off  2:off  3:on   4:off  5:on   6:off

Copy code

The code is as follows:

# cd /etc/rc.d/rc3.d
# ls -l | grep nfsserver

lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

Copy code

The code is as follows:

# cd /etc/rc.d/rc5.d
# ls -l | grep nfsserver

lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver
When you use the – del or – level option to turn off the service, the corresponding symbolic link file in the RCX. D directory will be deleted.

Copy code

The code is as follows:

# chkconfig –level 5 nfsserver off
# ls /etc/rc.d/rc5.d | grep nfsserver

Recommended Today

Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]