Use of Linux chkconfig Command

  • 2021-08-21 22:11:16
  • OfStack

1. Brief introduction to commands

The chkconfig command is used to update and query the running level information of system services. It can query which system services will be automatically executed by the operating system in each running level, including various resident services, such as httpd, sshd, mysqld and so on.

chkconfig actually sets which system services the operating system executes in each run level by changing the symbolic links of the service scripts in the seven different run level directories/etc/rc [0-6]. d. Unlike service, chkconfig is not intended to start or stop a service immediately.

chkconfig has five functions:

(1) Adding new services for chkconfig management;
(2) Delete services from the list of chkconfig management services;
(3) List the current startup information of all services managed by chkconfig;
(4) Change the startup information of the service;
(5) Check the startup status of a specific service.

2. Command format


chkconfig
service [OPTIONS] SERVICENAME

chkconfig without any options or with options-list will display a list of all services and their current configuration.

When only the service name is followed by chkconfig, it is checked whether the service is configured to start itself at the current running level. If so, chkconfig returns true, otherwise false. The level option allows chkconfig to query the configuration at another run level instead of the current run level.

If one of on, off, reset, or resetpriorities is specified after the service name, chkconfig changes the startup information for the specified service. The on and off flags cause the service to be set to start or stop in the running level being changed, respectively. The reset flag resets the switch state of all running levels of the service to the values specified in the relevant init script, while the resetpriorities flag resets the start/stop priority of the service to the values specified in the init script. By default, the on and off options only affect run levels 2, 3, 4, and 5, while reset and resetpriorities affect all run levels. The--level option can be used to specify the affected run level.

3. Description of options


--level LEVELS
	 Specifies the run level that the operation should belong to. 0  To  6  Composed of a number string. For example, -level 35  Specify the run level  3  And  5
--no-redirect
	 If the system uses  systemd  As the startup process of the system, chkconfig  Forward the command to  systemd . This option turns off to the  systemd  And only in the  /etc/rc[0-6].d  Actions on symbolic links in. This option is only available in the  on , off  Or when no command is passed to the service (check enabled) 
--add SERVICENAME
	 Add 1 New services for  chkconfig  Management 
--del SERVICENAME
	 Will start from  chkconfig  Delete the service from the management and delete the  /etc/rc[0-6].d  Any symbolic links associated with it in the 
--override SERVICENAME
	 Change service configuration 
--list [SERVICENAME]
	 List  chkconfig  The startup state of all known services at different running levels. If you specify  SERVICENAME Only the startup status of the specific service is listed 

4. Common examples

(1) List all system services.


chkconfig
Note: This output shows SysV services only and does not include native
   systemd services. SysV configuration data might be overridden by native
   systemd configuration.

   If you want to list systemd services use 'systemctl list-unit-files'.
   To see services enabled on particular target use
   'systemctl list-dependencies [target]'.

bootlocal   	0:off	1:off	2:off	3:on	4:off	5:off	6:off
irqaffinity  	0:off	1:off	2:on	3:on	4:on	5:on	6:off
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
qemu-ga    	0:off	1:off	2:on	3:on	4:on	5:on	6:off
rename_netifs 	0:off	1:off	2:off	3:on	4:off	5:off	6:off

(2) Configure the Apache Web server to start every time the system starts.


chkconfig httpd on

When you successfully enable the service using chkconfig, this command does not provide any acknowledgement messages.

(3) Set network to be closed when the running level is 2, 3, 4 and 5, that is, it does not start.


chkconfig network off

#  Or 
chkconfig --level 2345 network off

(4) View the self-startup status of the network service.


chkconfig --list network
Note: This output shows SysV services only and does not include native
   systemd services. SysV configuration data might be overridden by native
   systemd configuration.

   If you want to list systemd services use 'systemctl list-unit-files'.
   To see services enabled on particular target use
   'systemctl list-dependencies [target]'.

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

STEP 5 Expand your knowledge

5.1 Register Service to chkconfig

Each service managed by chkconfig requires two or more lines of comment in the corresponding administrative script under/etc/rc. d/init. d. Line 1 tells chkconfig the default run level to start and the priority of starting and stopping. If a service does not start at any run level by default, use-instead of the run level. Line 2 describes the service, which can be annotated with\ cross-line. For example, the notes in/etc/rc. d/init. d/network:


# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
#       start at boot time.

The first line indicates that the run levels are 2, 3, 4, and 5, the start priority is 10, and the stop priority is 90. Lines 2 and 3 describe the service network.

5.2 Seven Operating Levels of the Linux System

The run level is the functional level at which the operating system is currently running. It causes 1 program to start at 1 level and not start at the other level. Linux System 1 generally uses seven levels.

0 shutdown state. The default running level of the system cannot be set to 0, otherwise it will not start normally
1 single user mode, root authority, for system maintenance, no remote login
2 multi-user mode without network
3 Multi-user mode with network
4 The system is not used, reserved
5 graphical interface
6 The system is normally shut down and restarted. The default running level cannot be set to 6, otherwise it cannot be started normally

The default run level can be viewed in file/etc/inittab. The default login level of Linux for self-use is 5, that is, boot to enter the graphical user interface, and the running level of remote login is 3, entering the command line interactive interface.

The principle of running level:

(1) There are many service management scripts under the direction/etc/rc. d/init. d, each of which is called service;
(2) Under/etc/rc. d, there are seven directories named rcN. d, corresponding to seven running levels of the system;
(3) rcN. d directories are all symbolic link files, which point to service script files in init. d directories. The naming rules are K+nn + service name or S+nn + service name, where nn is two digits.
(4) The system will enter the corresponding rcN. d directory according to the specified running level, and retrieve the linked files under the directory in the order of file names:

For files that begin with K, the system terminates the corresponding service
For files that begin with S, the system starts the corresponding service

(5) To view the running level: runlevel;
(6) Enter other operation levels with: (sudo) init N;
(7) In addition, init 0 is shutdown and init 6 is restart system.

In addition, when using runlevel to view the run level, the result shows the previous run level and the current run level. If the previous run level is N, it means that there was no previous run level (maybe just power on).

The above is the use of Linux chkconfig command details, more information about Linux chkconfig command please pay attention to other related articles on this site!


Related articles: