写在最前

1. 内核参数

/etc/sysctl.conf 就像“店铺的规章制度”

  • 是你开店前写好的规章制度文档,告诉员工该怎么做事。

  • 比如“每天最多接待多少客人”、“晚上几点打烊”、“哪些顾客不能进”等等。

  • 这些规则(内核参数)不需要你亲自去做,而是操作系统(员工)自动照做。

🧠 对应 Linux 行为:

  • /etc/sysctl.conf 是内核参数配置文件。设置网络、内存、进程等方面的行为。

  • sysctl -p 就是“让员工按照这份规章重新执行一遍”。

1.1 配置文件

sysctl.conf 是 Linux 系统用于配置内核参数的配置文件,主要通过 sysctl 工具进行加载和应用 /etc/sysctl.conf 是什么?这是最常用、默认的主配置文件。它用于设置内核参数,比如网络、内存、文件句柄限制等。

注意:数字前缀(如 99-custom.conf)越大,优先级越高。

Kylin Linux Advanced Server V10 (Halberd)

4.19.90-89.11.v2401.ky10.x86_64

# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
kernel.sysrq=0
net.ipv4.ip_forward=0
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.send_redirects=0
net.ipv4.conf.all.accept_source_route=0
net.ipv4.conf.default.accept_source_route=0
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.default.accept_redirects=0
net.ipv4.conf.all.secure_redirects=0
net.ipv4.conf.default.secure_redirects=0
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.icmp_ignore_bogus_error_responses=1
net.ipv4.conf.all.rp_filter=1
net.ipv4.conf.default.rp_filter=1
net.ipv4.tcp_syncookies=1
kernel.dmesg_restrict=1
net.ipv6.conf.all.accept_redirects=0
net.ipv6.conf.default.accept_redirects=0

[root@localhost ~]# ll /etc/sysctl.d/
总用量 0
lrwxrwxrwx 1 root root 14  7月 11 15:37 99-sysctl.conf -> ../sysctl.conf

2. 内核模块

/etc/sysconfig/modules/*.modules 就像“开店时雇佣的临时工脚本”

  • 你开店前,还得叫一些临时工来搬货、打扫、装饰。

  • 这些人需要你点名:“来,把这个设备驱动装上!”

🧠 对应 Linux 行为:

  • .modules 脚本用于在系统启动过程中,自动加载一些内核模块。

2.1 常见配置

/etc/sysconfig/modules/br_netfilter.modules

  • 作用:该脚本用于在系统启动时加载 br_netfilter 内核模块。

  • 用途br_netfilter 模块是与网络桥接(bridge)相关的模块,主要用于启用网络桥接的网络过滤功能。在 Kubernetes 中,当使用 FlannelCalico 等网络插件时,这个模块会用来支持跨节点的网络包过滤。

  • 常见用途

    • 启用桥接接口的 iptables 过滤。

    • 用于容器网络(如 Docker、Kubernetes)的桥接网络模式。

#!/bin/bash
modprobe br_netfilter

/etc/sysconfig/modules/ipvs.modules

  • 作用:该脚本用于在系统启动时加载与 IPVS(IP Virtual Server)相关的内核模块。

  • 用途IPVS 是一种高性能的 Linux 内核负载均衡技术,用于代理流量到多个后端服务器。当 Kubernetes 配置为 IPVS 模式时,kube-proxy 将使用 IPVS 进行流量路由。这些内核模块提供了负载均衡、调度和流量分发的功能。

  • 常见用途

    • 启用 IPVS 支持,通常用于 Kubernetes 中的负载均衡。

    • 启动时加载必要的模块,如 ip_vs, ip_vs_rr, ip_vs_wrr, ip_vs_sh 等。

#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack

3. 加载顺序

  • rc0: 停机(不能使用)

  • rc1: 单用户模式

  • rc2: 多用户模式,但是没有NFS

  • rc3: 完全多用户模式

  • rc4: 没有使用,系统预留

  • rc5: 图形界面模式

  • rc6: 重启模式(不能使用)

[root@localhost rc.d]# pwd
/etc/rc.d
[root@localhost rc.d]# tree
.
├── init.d
│   ├── functions
│   └── README
├── rc0.d
├── rc1.d
├── rc2.d
├── rc3.d
├── rc4.d
├── rc5.d
├── rc6.d
└── rc.local

8 directories, 3 files

在 Debian 系统中,系统启动和运行级别管理采用了传统的 SysVinit 机制。具体来说,/etc/rc0.d/etc/rc6.d 这七个目录分别对应不同的运行级别(runlevel),系统根据当前运行级别自动执行对应目录下的启动和停止脚本。

/etc/rc1.d 目录为例,其内容如下:

lrwxrwxrwx 1 root root  20 Feb  9 10:33 K01alsa-utils -> ../init.d/alsa-utils
lrwxrwxrwx 1 root root  13 Feb  9 10:33 K01atd -> ../init.d/atd
lrwxrwxrwx 1 root root  22 Feb  9 10:33 K02avahi-daemon -> ../init.d/avahi-daemon
lrwxrwxrwx 1 root root  14 Feb  9 10:33 K02gdm3 -> ../init.d/gdm3
lrwxrwxrwx 1 root root  17 Feb  9 10:33 K04rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root  20 Feb  9 10:33 K06nfs-common -> ../init.d/nfs-common
lrwxrwxrwx 1 root root  17 Feb  9 10:33 K06rpcbind -> ../init.d/rpcbind
lrwxrwxrwx 1 root root  14 Feb  9 10:33 S01motd -> ../init.d/motd
lrwxrwxrwx 1 root root  18 Feb  9 10:33 S05bootlogs -> ../init.d/bootlogs
lrwxrwxrwx 1 root root  16 Feb  9 10:33 S06single -> ../init.d/single

命名规则及含义

  • KXX开头的脚本
    “K”代表 Kill,表示在该运行级别切换时,这些脚本对应的服务需要被停止。后面的数字“XX”代表优先级,数字越小,停止操作越先执行。

  • SXX开头的脚本
    “S”代表 Start,表示在该运行级别切换时,这些脚本对应的服务需要被启动。数字越小,启动的优先级越高,越先执行。

这些脚本其实是 /etc/init.d/ 目录下服务启动脚本的软链接,系统根据运行级别执行这些软链接对应的实际脚本来启动或停止服务。

此外,服务的具体配置文件一般位于 /etc/init//etc/ 目录下的相应子目录,供服务脚本调用和使用。