一些关于sysctl参数设置的收集和解释。
/etc/sysctl.d/00-defaults.conf
kernel.printk
输出内核日志信息的级别。
1 | # 映射到的proc文件系统位置 - /proc/sys/kernel/printk |
内核中共提供了八种不同的日志级别,在 linux/kernel.h 中有相应的宏对应。
1 |
kernel.panic
设置内核的Panic之后自动重启
1 | # Wait 30 seconds and then reboot |
neigh.default.gc
设置arp缓存相关的参数
https://zhuanlan.zhihu.com/p/94413312
1 | # Allow neighbor cache entries to expire even when the cache is not full |
/etc/sysctl.d/99-amazon.conf
sched_autogroup_enabled
通过CFS分组提高了桌面环境的性能表现。这个小小的补丁仅为 Linux Kernel 增加了 233 行代码,却将高负荷下桌面响应最大延迟降低到原先的十分之一,平均延迟降低到六十分之一!该补丁的作用是为每个 TTY 动态地创建任务分组。(https://linuxtoy.org/archives/small-patch-but-huge-improvement.html)
1 | # https://cateee.net/lkddb/web-lkddb/SCHED_AUTOGROUP.html |
/usr/lib/sysctl.d/00-system.conf
bridge-nf-call-iptables
网桥设备关闭netfilter模块,开关需要按需求来指定。
关闭这个模块会在网桥2层可以转发的时候直接转发, 不会走三层进行数据传输,也就是说不会过Iptables。
Kubernetes需要开启这个参数的原因是: https://imroc.cc/post/202105/why-enable-bridge-nf-call-iptables/, 修复了Coredns不定期解析失败的问题。
1 | # Disable netfilter on bridges. |
/usr/lib/sysctl.d/10-default-yama-scope.conf
yama
Yama is a Linux Security Module that collects system-wide DAC security protections that are not handled by the core kernel itself. This is selectable at build-time with CONFIG_SECURITY_YAMA, and can be controlled at run-time through sysctls in /proc/sys/kernel/yama
https://www.kernel.org/doc/html/latest/admin-guide/LSM/Yama.html
1 | [root@ip-172-31-11-235 sysctl.d]$ cat 10-default-yama-scope.conf |
/usr/lib/sysctl.d/50-default.conf
sysrq
是一个Magic Key的设置,无论内核当前在做什么都会立刻响应这个MagicKey。
1 |
|
core_uses_pid
1 | # Append the PID to the core filename |
kptr_restrict
1 | # 查看内核地址符号的命令: cat /proc/kallsyms |
rp_filter
rp_filter参数用于控制系统是否开启对数据包源地址的校验。
1 | # Source route verification |
accept_source_route
指允许数据包的发送者指定数据包的发送路径,以及返回给发送者的数据包所走的路径。
1 | # Do not accept source routing |
promote_secondaries
down掉所属某个子网的primary ip的时候, 所有相关的secondary ip也会down掉,启用的时候,当primary ip宕掉时可以将secondary ip提升为primary ip。
1 | # Promote secondary addresses when the primary address is removed |
protected_links
fs.protected_hardlinks 用于限制普通用户建立硬链接
- 0:不限制用户建立硬链接
- 1:限制,如果文件不属于用户,或者用户对此用户没有读写权限,则不能建立硬链接
fs.protected_symlinks 用于限制普通用户建立软链接
- 0:不限制用户建立软链接
- 1:限制,允许用户建立软连接的情况是 软连接所在目录是全局可读写目录或者软连接的uid与跟从者的uid匹配,又或者目录所有者与软连接所有者匹配
1
2
3# Enable hard and soft link protection
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
/usr/lib/sysctl.d/9-ipv6.conf
关于IPv6地址重复的问题。
In case DAD does find a duplicate address, the address we tried to set is deleted (and in the syslog you see a message like this: “eth0: duplicate address detected!”). In such a case, a sysadmin needs to configure an address manually.
1 | net.ipv6.conf.all.accept_dad=0 |
Case Update
tcp_tw_recycle(Before 4.12 available)
net.ipv4.tcp_timestamps 这个参数开启的时候, 才会生效,可以快速回收处于TIME_WAIT状态的socket。当tcp_tw_recycle开启时(tcp_timestamps同时开启,快速回收socket的效果达到),对于位于NAT设备后面的Client来说,是一场灾难。
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4396e46187ca5070219b81773c4e65088dac50cc
这个参数在2017年之后的内核已经不存在了, 开发者的Blog https://vincent.bernat.ch/en/blog/2014-tcp-time-wait-state-linux
内核彻底移除这个参数的版本是 4.12 , 如果内核的版本是4.12 之后,这个问题就不存在了。
对于NAT后端的多个实例之间,如果有时间的差距会导致另一个实例收到的报文的Timestamp是之前一个时间点的,这个时候会认为这个请求是来自之前的某个请求的返回。 然而其
实是因为时间的原因, 并且这个请求是一个新的请求,由于时间的原因被识别成了旧的请求。
这个配置对 Incomming 和 Outgoing的tcp连接都会生效。由于网络的设计是默认不相信可靠的, 所以在TCP的最后进行等待,这样会导致大量的TIME_WAIT, 而这些TIME_WAIT其实是已经不用的, 但是由于无法获得ACK因此无法释放。tcp_tw_reuse
http://lxr.linux.no/#linux+v3.2.8/Documentation/networking/ip-sysctl.txt#L464
这个配置只是对客户端生效。也就是说, 客户端发起了FIN之后, 只要客户端收到服务端的FIN, 后续的网络报文已经不再继续关注了。 这样客户端的TIME_WAIT就会下降, 提高了客户端的Timewait的利用率,减少了客户端TW连接的数量。可以提高客户端的并发请求数量。
这个配置参数只对 Outgoing 的参数生效。
NOTE: 现在这个选项支持3个参数,0 关闭, 1 开启 ,2 只对回环开启。