在 RHEL/CentOS/Alma/Rocky 9 上设置 IPv4 优先于 IPv6
有一些VPS或者云主机同时拥有IPV4和IPV6地址,当目标网站拥有IPV6地址时,Linux系统默认会优先使用IPV6连接目标网站。
如果我们需要优先使用IPV4去联网,又保留IPV6联网的话,可以通过修改系统的 gai.conf 配置文件来实现。
RHEL9系统中并没有这个文件,该文件在 glibc 2.32.9000-27 包更新中已被移除,原因不明。但并没有看到任何阻止其使用的补丁,可以通过获取原始的gai.conf并使用,而不是从 /usr/share/doc/ 获取。
可以从头创建 /etc/gai.conf(下面附上一个副本)。或者从 glibc-2.34 的源代码中,位于 posix 子目录下获取它。再或者直接创建文件并加上行 precedence ::ffff:0:0/96 100,默认情况下几乎每一行都是空白或注释。
具体修改的步骤如下:
- 编辑 gai.conf 文件:
首先,您需要确保系统上存在 /etc/gai.conf 文件。如果没有,可以手动创建它。
运行以下命令编辑文件(如果文件不存在,将会创建):
- 修改配置以优先使用 IPv4: 在文件中添加以下行来将 IPv4 设置为优先使用:
这行配置的意思是:将所有 IPv6 地址范围 ::ffff:0:0/96 的地址优先级设置为 100(比默认的 IPv6 地址更高)。
- 保存并退出: 在 vi 编辑器中,按 Esc,然后输入 :wq 保存并退出。
- 验证配置生效: 您可以使用 ncat 或其他命令来验证 IPv4 是否已优先于 IPv6。例如:
如果一切正常,您应该看到优先使用 IPv4 连接。
- 确保 SELinux 权限正确: 如果启用了 SELinux,请确保 /etc/gai.conf 文件的 SELinux 上下文正确。可以运行以下命令:
以下是从源码 SRPM包(glibc-2.34-83.el9_3.7.src.rpm) 中获取的gai.conf内容:
# Configuration for getaddrinfo(3). # # So far only configuration for the destination address sorting is needed. # RFC 3484 governs the sorting. But the RFC also says that system # administrators should be able to overwrite the defaults. This can be # achieved here. # # All lines have an initial identifier specifying the option followed by # up to two values. Information specified in this file replaces the # default information. Complete absence of data of one kind causes the # appropriate default information to be used. The supported commands include: # # reload <yes|no> # If set to yes, each getaddrinfo(3) call will check whether this file # changed and if necessary reload. This option should not really be # used. There are possible runtime problems. The default is no. # # label <mask> <value> # Add another rule to the RFC 3484 label table. See section 2.1 in # RFC 3484. The default is: # #label ::1/128 0 #label ::/0 1 #label 2002::/16 2 #label ::/96 3 #label ::ffff:0:0/96 4 #label fec0::/10 5 #label fc00::/7 6 #label 2001:0::/32 7 # # This default differs from the tables given in RFC 3484 by handling # (now obsolete) site-local IPv6 addresses and Unique Local Addresses. # The reason for this difference is that these addresses are never # NATed while IPv4 site-local addresses most probably are. Given # the precedence of IPv6 over IPv4 (see below) on machines having only # site-local IPv4 and IPv6 addresses a lookup for a global address would # see the IPv6 be preferred. The result is a long delay because the # site-local IPv6 addresses cannot be used while the IPv4 address is # (at least for the foreseeable future) NATed. We also treat Teredo # tunnels special. # # precedence <mask> <value> # Add another rule to the RFC 3484 precedence table. See section 2.1 # and 10.3 in RFC 3484. The default is: # #precedence ::1/128 50 #precedence ::/0 40 #precedence 2002::/16 30 #precedence ::/96 20 #precedence ::ffff:0:0/96 10 # # For sites which prefer IPv4 connections change the last line to # #precedence ::ffff:0:0/96 100 # # scopev4 <mask> <value> # Add another rule to the RFC 6724 scope table for IPv4 addresses. # By default the scope IDs described in section 3.2 in RFC 6724 are # used. Changing these defaults should hardly ever be necessary. # The defaults are equivalent to: # #scopev4 ::ffff:169.254.0.0/112 2 #scopev4 ::ffff:127.0.0.0/104 2 #scopev4 ::ffff:0.0.0.0/96 14CentOS替代操作系统推荐:Rocky Linux与AlmaLinux的优缺点分析
windows10 开启便签工具(Sticky Notes)桌面便签sticky notes
关于如何恢复Windows 10 便签(Sticky Notes)桌面便签sticky notes
ROS+gre over ipv6,在ipv6的基础上建立GRE隧道ipv6 over ipv4隧道
解决 perhaps pyenv-virtualenvwrapper has not been loaded into your shell properly 问题的最佳方法
centos vps 安装socks5服务centos一键搭建socks5脚本
Docker的三种网络代理配置:dockerd pull镜像代理;容器docker run网络代理;docker build代理--build-argdocker pull 代理
centos系统中可以使用ubuntu docker马 centos跟ubuntu