Self-starting of Linux system

This article is an original article by joshua317, please indicate when reprinting: Reprinted from joshua317 blog  Linux system boot self-start - joshua317's blog

When you use the windows operating system, there will always be various software that complete the self-startup after booting, and you do not click their icons on the desktop to start them. For example, some security guards, some music listening software, etc., they will start automatically when the system is started, and the linux system also has such a mechanism. Let's take a look at how linux manages to start automatically.

Note: The current progress is located at " https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_basic_system_settings/index ", please slide on the left to find "5. Making systemd services start at boot time ", the content of the previous section is "3. Configuring and managing network access". "4. Registering the system and managing subscriptions" is a paid subscription service exclusive to the redhat system, and this mode does not exist in the rocky linux we use, so we can ignore this section.

First of all, let's clarify some concepts. Please press "Microsoft logo key + R" on the Windows desktop, and the Microsoft logo key is located on the left side of the "alt" side on the left.

Then enter "taskmgr" in the pop-up dialog box to open the task manager, and you will see a large number of running processes, some of which belong to the system process, and some belong to the third-party software process. And your work, entertainment, study, etc. on windows are all processed silently by these processes in the background. And some processes are started when booting, and some are triggered to start when users use them after entering the system.

You can enter the following command on the Linux command line to view the running processes in the system.

ps -ef
# ps --help 或 man ps 了解e和f参数的作用

With these concepts, we can learn how to manage services in the Linux system to start at boot time (services start at boot time).

In the linux system, the system started by the management service is called "systemd". It is the first process started when the linux system starts. All subsequent processes are started by this process, so you will see that its process number PID is 1.

The command to manage whether the service starts or not is systemctl, and its function is more than that, but we only understand its part related to self-starting management at this moment.

# 我们以时间同步服务chrony举例,查看chrony的状态。
# systemctl的参数支持tab键补齐,当你输入systemctl status ch时就可以多次敲击tab键,选择相应的服务名即可。
systemctl status chronyd.service

We only focus on these two points, chrony is currently in the enabled state, it will be started by the systemd service during the boot process, and at the same time process the running state, which means it is running at the moment.

# 禁用chrony,我们不希望它在开机时启动,更改系统设置需要使用管理员权限,此处使用sudo
sudo systemctl disable chronyd.service

# /etc/systemd/system/multi-user.target.wants/下是各种服务的启动描述文件链接,当软件安装在系统中时,所有软件都会在
# /usr/lib/systemd/system/下创建自己的启动描述文件,当需要开机自启时,会创建一个软链接到
# /etc/systemd/system/multi-user.target.wants/中。

# 再次查看chrony的状态,它是disable状态
systemctl status chronyd.service
# 重启系统,好验证禁用后的chronyd会不会被systemd启动。
reboot
# 待系统启动完成后,再次查看chrony的状态,它是disable以及inactive状态
systemctl status chronyd.service

# 我们再次启用chrony,并手动启动它
# 启用chrony
sudo systemctl enable chronyd.service
sudo systemctl start chronyd.service

# 这次查看chrony,它已经恢复到之前的状态了
systemctl status chronyd.service

The official documentation also provides another way to manage service startup: web console.

Web console documentation: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_systems_using_the_rhel_8_web_console/index

# 启动web console的服务
sudo systemctl start cockpit.socket

Bash

Copy

Open Mozilla Firefox in the rocky linux system, visit https://localhost:9090 , and enter the account password.

Pay attention to switching to administrator privileges, and then you can manage the enabling and disabling of services here.

Open service, find chronyd, click chronyd to enter the management page:

To manage enabling and disabling of chronyd:

However, compared with the command line, the management efficiency of the web console is not high, such as:

# 同时禁用多个服务
sudo systemctl disable crond.service chronyd.service
# 将刚刚禁用的多个服务同时启用
sudo systemctl enable crond.service chronyd.service

Therefore, when you are proficient in using the command line (CUI) for system management operations, the efficiency will be much higher than that of the graphical interface (GUI).

However, when you explore the web console, have you noticed this thing:

systemctl can start, stop, and restart services:

sudo systemctl start chronyd.service
sudo systemctl stop chronyd.service
sudo systemctl restart chronyd.service

But what is this mask for?

Let's try it out:

# 将chronyd执行mask动作
sudo systemctl mask chronyd.service

Then the command result prompts "Created symlink /etc/systemd/system/chronyd.service → /dev/null.", which links chrony's self-starting description file to /dev/null.

/dev/null is the "data sink" in the linux system, and it has a brother called /dev/zero. They are like black holes, any file data given to them will disappear, such as:

# echo一些文字保存下来
echo "testing" > test.txt
cat test.txt
# 但是将文字输出到/dev/null中什么都不会发生,输出文件直接消失
echo "testing" > /dev/null

But what is the purpose of systemctl doing this?

Masking off chrony will completely disable the chrony service. The previous disable action just prevents chrony from starting automatically. At least I can manually start this service through systemctl start chronyd.service, but if chronyd is executed with a mask, this service cannot be started at all, nor can it be enabled. It is called the enhanced version of disable.

Therefore, the mask function should be used with caution. Remove the mask and use unmask:

# 解除chrony服务的mask
sudo systemctl unmask chronyd.service
# 启用chronyd服务,--now会同时启动服务
sudo systemctl enable chronyd.service --now

So how to check which services are enabled and which are disabled in the system?

# 显示systemd当前的units状态,此状态是各服务的运行状态
systemctl list-units
# 显示systemd当前已经安装的units状态,如/usr/lib/systemd/system/chronyd.service,这个就是安装chrony产生的units文件。
systemctl list-unit-files
# 查看服务的自启动状态以list-unit-files为准

# 但是系统服务太多,我想分类查看该如何做?

# 查看系统中已经enable的服务
systemctl list-unit-files --state=enabled
# 查看系统中已经disable的服务
systemctl list-unit-files --state=disabled

# --state还可以与list-units结合使用,具体可使用以下命令查询用法
systemctl --state=help

This article is an original article by joshua317, please indicate when reprinting: Reprinted from joshua317 blog  Linux system boot self-start - joshua317's blog

Guess you like

Origin blog.csdn.net/joshua317/article/details/128269412