当前位置:首页 > CN2资讯 > 正文内容

在 RHEL/CentOS/Alma/Rocky 9 上设置 IPv4 优先于 IPv6

2天前CN2资讯

       有一些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 文件。如果没有,可以手动创建它。
    运行以下命令编辑文件(如果文件不存在,将会创建):
sudo vim /etc/gai.conf
  • 修改配置以优先使用 IPv4: 在文件中添加以下行来将 IPv4 设置为优先使用:
precedence ::ffff:0在 RHEL/CentOS/Alma/Rocky 9 上设置 IPv4 优先于 IPv6:0/96 100

这行配置的意思是:将所有 IPv6 地址范围 ::ffff:0:0/96 的地址优先级设置为 100(比默认的 IPv6 地址更高)。

  • 保存并退出: 在 vi 编辑器中,按 Esc,然后输入 :wq 保存并退出。
  • 验证配置生效: 您可以使用 ncat 或其他命令来验证 IPv4 是否已优先于 IPv6。例如:
ncat --verbose --verbose -z

如果一切正常,您应该看到优先使用 IPv4 连接。

  • 确保 SELinux 权限正确: 如果启用了 SELinux,请确保 /etc/gai.conf 文件的 SELinux 上下文正确。可以运行以下命令:
sudo chcon -u system_u -r object_r -t etc_t /etc/gai.conf

以下是从源码 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 14


    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/19427.html

    分享给朋友:

    “在 RHEL/CentOS/Alma/Rocky 9 上设置 IPv4 优先于 IPv6” 的相关文章

    Host Winds:可再生能源的关键因素与未来发展潜力

    什么是 Host Winds? 在谈论可再生能源时,Host Winds 是一个不容忽视的概念。简单来说,Host Winds 指的就是那些发生在某一特定区域内的风速和风向。这些风的模式能够极大地影响一个地区的风力发电潜力。想象一下,如果你在一个风速稳定且方向一致的地区,那么利用这些风来发电就会更加...

    主机论坛:获取信息与交流经验的最佳平台

    主机论坛概述 在当今的数字时代,主机论坛作为一个专注于域名、主机、VPS和服务器的讨论与信息交流平台,显得尤为重要。对于站长、开发者和一般用户来说,它们不仅是资讯获取的渠道,更是一个技术交流和问题解决的空间。主机论坛通过汇聚来自不同背景的用户,形成了一个活跃的社区,每个人都能找到自己感兴趣的话题,分...

    优化RackNerd DC2机房 IP使用体验与性能评测

    我最近对RackNerd的DC2机房产生了越来越多的兴趣,特别是位于美国洛杉矶的这座机房。它被誉为RackNerd中中国国内访问速度较快的机房之一,吸引了很多需求高效网络连接的用户。这座机房的地理位置确实蛮不错,靠近美西主干线,对于需要与国内建立连接的网站和应用来说,能带来相对更快的访问速度。 对于...

    阿里云香港轻量服务器:高性价比云计算解决方案

    阿里云香港轻量服务器是我在寻找云计算解决方案时发现的一个非常实用的选择。它不仅具备高性价比,还有灵活的配置和便捷的管理体验,适合各种用户需求。让我带你深入了解一下这个产品的特点和优势。 首先,香港轻量服务器的价格设置比较合理。我看到它提供多个配置供用户选择,无论是新手还是有经验的开发者都能在这里找到...

    ColoCrossing:优质VPS与安全托管服务提供商解析

    在美国,ColoCrossing作为一家老牌且独立的服务器和托管服务提供商,一直以来以其优质的IT解决方案著称。这家成立多年的公司,致力于满足不同类型客户的需求,无论是面向亚太地区还是欧美市场,它都有相应的数据中心来支持各种使用场景。我个人觉得他们的服务理念是非常值得推崇的,特别是在稳定性和可靠性方...

    Win10一键安装SQLite脚本:简化你的数据库配置过程

    在开始使用SQLite之前,首先需要确保它已经正确安装在你的Windows 10系统上。这个过程包括几个简单的步骤。我会逐步带你完成这些操作,让你能快速进入SQLite的世界。 访问SQLite官网 首先,前往SQLite的官方网站,网址是https://www.sqlite.org/downloa...