惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
博客园 - 【当耐特】
H
Heimdal Security Blog
腾讯CDC
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threatpost
月光博客
月光博客
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
D
DataBreaches.Net
O
OpenAI News
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
小众软件
小众软件
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
美团技术团队
P
Privacy International News Feed

烧饼博客

Ubuntu 24.04 Noble 升级 Ubuntu 26.04 Resolute WSL 2 使用 Docker 桥接模式网络访问 HTTPS 超时的解决方法 Debian / Ubuntu 下使用 nginx-acme 自动签发并配置 SSL 证书 使用 acme.sh 配置 Let's Encrypt 签发的 IP 地址 SSL 证书 RDAP.SS - 基于 RDAP 协议的 Whois 查询网站 Debian 使用 extrepo 配置第三方软件源 升级 Debian 后 GitLab PostgreSQL 无法启动的解决方法 Debian 12 Bookworm 升级 Debian 13 Trixie Ubuntu 22.04 Jammy 升级 Ubuntu 24.04 Noble Docker 安装 FreshRSS 教程 Docker 安装 Shlink 自建短网址 Debian 安装 Nextcloud 服务端 Debian / Ubuntu 使用源安装 LAMP 教程 Debian / Ubuntu 使用源安装 LEMP 教程
Debian 双栈网络时开启 IPv4 优先
Showfom · 2022-02-11 · via 烧饼博客
Debian 双栈网络时开启 IPv4 优先

本文原理适用于大多数 Linux 系统,其他系统尚未测试,请读者自行验证。

#背景介绍

双协议栈技术就是指在一台设备上同时启用 IPv4 协议栈和 IPv6 协议栈,这样就可以同时使用 IPv4 和 IPv6 的网络。

现代操作系统和浏览器通常会优先使用 IPv6,只有当 IPv6 无法访问时,才会回退到 IPv4。但在某些特定的应用或场景中,我们可能更希望系统优先使用 IPv4,这时就需要通过配置文件进行调整。

#修改 /etc/gai.conf

在 Debian 等 Linux 系统下,有一个 /etc/gai.conf 文件,用于系统的 getaddrinfo 调用,默认情况下,它会使用 IPv6 优先,如果您安装了 curl 并且本地支持 IPv6,那么可以使用 curl ip.sb 测试:

root@debian ~ # curl ip.sb
2001:db8::2

结果与 curl ip.sb -6 等效。

从 Debian 13 开始,curl (8.14.1) 默认强制使用 IPv6。因此,如果希望测试本地出口公网 IP,可以改用 wget

root@debian ~ # wget -qO- http://ip.sb
2001:db8::2

效果等同于 wget -qO- http://ip.sb -6

如果你不想使用 IPv6 优先,可以在这个文件中找到:

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

取消注释,修改为:

precedence ::ffff:0:0/96  100

一行命令修改:

sed -i 's/#precedence ::ffff:0:0\/96  100/precedence ::ffff:0:0\/96  100/' /etc/gai.conf

此时再次执行 curl ip.sb 测试:

root@debian ~ # curl ip.sb
192.0.2.2

效果等同于 curl ip.sb -4

从 Debian 13 开始的 curl (8.14.1) 会强制 IPv6 优先,所以我们可以使用 wget 命令:

root@debian ~ # wget -qO- http://ip.sb
192.0.2.2

效果等同于 wget -qO- http://ip.sb -4

某些情况下,你可能又需要强制启用 IPv6 优先(是的,有些系统和用户的需求确实有点奇怪😅),因为目前 IANA 分配的公网 IPv6 还未进行到 3000:0000::/4,所以我们只要把这段之前的 IPv6 加到优先级列表即可,加入这两行 label 的优先级:

label 2002::/16    1
label 2001:0::/32   1

#禁用 IPv6

有一些极端情况下,我们可能需要禁止系统的 IPv6 功能,这时候就需要添加或修改 /etc/sysctl.d/local.conf 文件,首先找到你的网卡名称,这里以 eth0 为例,然后加入如下内容:

net.ipv6.conf.all.autoconf = 0
net.ipv6.conf.default.autoconf = 0
net.ipv6.conf.all.accept_ra = 0
net.ipv6.conf.default.accept_ra = 0
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv6.conf.eth0.disable_ipv6 = 1

若需对其他网卡禁用 IPv6,只需将 eth0 替换为对应网卡名称即可。

一句话命令

cat >> /etc/sysctl.d/local.conf << EOF
net.ipv6.conf.all.autoconf = 0
net.ipv6.conf.default.autoconf = 0
net.ipv6.conf.all.accept_ra = 0
net.ipv6.conf.default.accept_ra = 0
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv6.conf.eth0.disable_ipv6 = 1
EOF

注意:cat 命令中的 >> 表示追加内容;若使用 >,则会覆盖原有内容。

然后执行 sysctl --system 重新加载配置文件。此时查看 ip a,即可发现 IPv6 已被禁用。

下图为修改前后的对比示例:

使用前,我们可以看到无论是本地还是公网网卡都有 inet6,即都有 IPv6 地址:

image.png

使用后,无论本地还是公网网卡均无 IPv6 地址:

image.png

#其他系统和软件

Windows 系统可参考这篇回答

Firefox 下打开 about:config 然后把 network.dns.disableIPv6 改成 true 即可禁止 Firefox 请求 IPv6