解决Linux优先通过IPv6联网的问题
  WcMlrurH7Ysw 2023年11月02日 48 0

前言

默认情况下,Linux偏好通过IPv6联网。对于没有IPv6的云服务器、机房环境来说没有什么太大的问题,偶尔可能解析出IPv6地址返回网络不可达的话修改DNS即可,但是对于具有IPv6地址的系统来说,如果碰上了V6地址不靠谱的情况,相当浪费时间。虽然网络上建议优先这样,但是DNS返回结果不靠谱不得不修改。


禁用IPV6

计算机操作系统、浏览器之类的纷纷彻底的优先IPv6了,虽然有v4兜底回退,对于浏览器来说不是太大的问题,Windows操作系统也没那么“硬”,至于国内软件,分成两类:一类是运营商、大互联网厂商,具有稳定成熟的v6网络,软件、网页能够顺利流畅通过v6地址加载了;而另外一类就类似百度,百度竟然放弃了ipv6?也许是机房接入的繁琐。

早些时候某些大厂的v6地址访问延迟极大,其实本质就是目标集群所在的机房并没有提供V6带宽接入,通过其他地方“自建路由”变相提供的V6访问,并不是简单的网络没优化好。

至于IPV6在Linux,如果不可靠,基本就是导致缓慢需要等待超时重试IPV4这一种结局。毕竟大多数都是基础软件,软件开发者们所处的网络环境都是比较优异的,一般没有做处理,并不会像浏览器那样,v6地址响应缓慢立刻同时跟踪v4地址。所以可以禁用,当然我不是很建议禁用,可能有不少软件在这个状态下会无法运行,哪怕你的环境没有IPv6,软件也无法在本地禁用v6的情况下运行。

echo "1" > /proc/sys/net/ipv6/conf/all/disable_ipv6

通过这个方法就可以简单的禁用ipv6,试试看有没有软件运行出现问题吧。

echo "0" > /proc/sys/net/ipv6/conf/all/disable_ipv6

如果有问题,输入上面的方式就可以关闭了。基本上RH DEB系都是通过这个虚拟文件临时禁用v6。重启就恢复了。

而有一些软件,临时禁用不会出现问题,但是永久禁用,就会无法运行。


IPv4优先

一个更靠谱的方式,就是修改系统的偏好。这方法,“很官方”。

echo 'precedence ::ffff:0:0/96  100' > /etc/gai.conf

这是相当“官方”的方法,出现问题的时候我就是用这个方法解决的。 ::ffff:0:0/96是IPv4在IPv6方法的兼容地址。

gai.conf,getaddrinfo,此文件用于管理员操作设定网络IP优先级。已CentOS 7或者类似的RH体验复刻系统,具体可以参见/usr/share/doc/glibc-common-2.17位置。debian类系统默认存在/etc/gai.conf,默认状态都是注释解释性的信息,而RH系默认并没有放置,所以直接覆盖此文件。

文件内就有具体的建议,如何进行一些设定。例如这一段;

#    For sites which prefer IPv4 connections change the last line to

#

#precedence ::ffff:0:0/96  100

下面是原文:

# 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
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  jnZtF7Co41Wg   2023年12月06日   27   0   0 sedlinux数据
  COAWCwhiWpsR   2023年12月10日   28   0   0 DNS
  COAWCwhiWpsR   2023年12月06日   30   0   0 DNS
  eHipUjOuzYYH   2023年12月06日   33   0   0 sedbootstrapIPV6
  xIUntf9oR6GI   2023年11月28日   31   0   0 sedvim基础命令
  ozzp9aSSE46S   2023年11月30日   31   0   0 DNSIPPod
  YKMEHzdP8aoh   2023年12月11日   63   0   0 DNSidePod
  eHipUjOuzYYH   2023年12月06日   26   0   0 nginx加载IPV6
WcMlrurH7Ysw