chkconfig (Linux run level) that automatically starts the Linux configuration service at boot

Linux configuration service starts automatically at boot (what is the difference between systemd and chkconfig --add settings to start automatically at boot)

1. How to make a service or script start automatically at boot?

1. Three solutions

  • Method 1: Add startup commands directly to the /etc/rc.d/rc.local file
    . For the scripts or services you want to start, just put their startup commands into the /etc/rc.local file. Suitable for simple startup scripts or services. But make sure to check that the file exists and give execute permissions before adding it.

  • Method 2: Configure service auto-start through chkconfig
    You need to use the chkconfig command to add the service, and use the on parameter of the command to enable auto-start.

  • Method 3: Use systemctl enble to configure the service to start automatically at boot
    . This is the recommended method in the latest Linux distribution . It uses the system's service manager to configure automatic startup.

Before CentOS7 (including 7), you can use chkconfig to configure the self-starting service at boot. After Centos7, it is more recommended to control the service through systemctl.

2. systemd and chkconfig

systemd is a modern initialization system that is widely used in most mainstream Linux distributions, such as Ubuntu, Fedora, CentOS, etc. It uses the concept of unit to manage system services. You can add, delete and manage services through the systemctl command. To set a service to start automatically at boot, you can use the systemctl enable command.

For example, to set the nginx service to start automatically at boot, you can run the following command:

systemctl enable nginx

chkconfig --add is an old initialization system management tool, mainly used in some older Linux distributions, such as RHEL (Red Hat Enterprise Linux) 6 and earlier versions. It is used to manage the system service scripts in the /etc/init.d directory and determine whether to start at boot by modifying the symbolic link. You can use the chkconfig command to add, remove, and manage services. To set a service to start automatically at boot, you can use the chkconfig --add command.

For example, to set the httpd service to start automatically at boot, you can run the following command:

chkconfig --add httpd

To summarize, the main difference is that systemd is a modern init system, used in the latest Linux distributions, and uses the systemctl command to manage services; while chkconfig --add is an old init system management tool, used in some older Linux distributions , and use the chkconfig command to manage services.

Two, chkconfig

Before CentOS7 (including 7), you can configure the self-starting service through chkconfig.

1. chkconfig management service script requirements

Chkconfig manages a service or script so that it can start automatically at boot. The following conditions apply:

  1. This script must be stored under /etc/init.dthe directory
  2. Must have execute permission (x permission)
  3. The first few lines of this script or service must have
# chkconfig:  必须要有这一行否则chkconfig不认识

#[空格]chkconfig:[空格]默认在哪个运行级别启动这个服务或软件[空格]第几个开机启动的[空格]关机的顺序
  1. To implement the functions in the POSIX specification in the service script: start() stop(), etc.

If your script does not install the above requirements, an error similar to the following will be reported: chkconfig --add xxx does not support chkconfig

2. chkconfig command

chkconfig –-add xxx //Add services to the chkconfig list, and add services or scripts to chkconfig management.
chkconfig --del xxx // chkconfig --del name: Delete the service and delete the related symbolic links from /etc/rc[0-6].d. chkconfig xxx on //Turn on automatic startup at boot. When configuring the system to start, the script starts chkconfig xxx off
by default. //Turn off automatic startup at boot.

chkconfig --list //View all services in chklist
chkconfig --list xxx View the specified service
chkconfig --list

Note: This output only shows the SysV service and does not include
the native systemd service. SysV configuration data
may be overwritten by native systemd configuration.

The chkconfig command is mainly used to update (start or stop) and query the run-level information of system services. Keep in mind that chkconfig does not automatically disable or activate a service immediately, it simply changes the symbolic link.

3. Linux run level

1. What is the linux run level

Linux OS divides the operating environment into the following 7 levels, namely
0: System shutdown (shutdown) mode. The system default operating level cannot be set to 0, otherwise it will not start normally and will automatically shut down as soon as it is turned on.
1: Single-user mode, root authority, used for system maintenance, remote login is prohibited, just like the safe mode login under Windows .
2: Multi-user mode, no NFS network support.
3: Complete multi-user text mode, with NFS, enter the console command line mode after logging in.
4: The system is not used, and it is reserved for general use. In some special cases, it can be used to do some things. For example, when the battery of the laptop is exhausted, you can switch to this mode to do some settings.
5: Graphical mode, after logging in, enter the graphical GUI mode or GNOME, KDE graphical interface, such as X Window system.
6: Restart mode. The default running level cannot be set to 6, otherwise it will not start normally and will always be turned on and restarted. This is how we usually perform reboot.

Run level changes can be switched through the init command. For example, suppose you want to maintain the system into a single-user state to use init1 to switch.

runlevel View the current run level:

# runlevel
N 5

During the Linux run-level switching process, the system will automatically look for files starting with K and S in the directory /etc/rc[0-6].d corresponding to the run level, and execute these scripts in numerical order. Maintaining these scripts is a very cumbersome matter. Linux provides the chkconfig command to update and query system services at different run levels.

2. Actual demo

Instructions for the default service network of the centos system:

cat /etc/init.d/network

Among them, #chkconfig: 2345 10 90 is used to specify the startup sequence of services at each level.
The meaning of this configuration is that the startup order of network services at levels 2, 3, 4, and 5 is 10, and the startup order at levels 1 and 6 is 90.

3. Reference

[Recommended, the author writes very clearly] Linux 7 running levels (0: shutdown, shutdown mode, 1: single-user mode, 2: multi-user mode, 3: complete multi-user text mode, 4: system is not used, reserved Generally not used, 5: graphical mode, 6: restart mode), reset root password method.
Reference URL: https://www.cnblogs.com/ljs05/p/7325920.html

Guess you like

Origin blog.csdn.net/inthat/article/details/124813407