![从 IPv6 子网 (/64) 绑定到随机 IP 不起作用](https://linux22.com/image/769388/%E4%BB%8E%20IPv6%20%E5%AD%90%E7%BD%91%20(%2F64)%20%E7%BB%91%E5%AE%9A%E5%88%B0%E9%9A%8F%E6%9C%BA%20IP%20%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我有一个 scaleway.com 服务器 (DEV1-S),它为我提供了一个 /64 IPv6 子网。我想使用此子网中的 IP 发出 HTTP 请求(将在示例中使用 wget),但我无法让它工作。请求(使用 wget 以及其他程序和编程语言)将挂起。
IPv6 地址为 2001:bc8:1830:1b18::1,网关为 2001:bc8:1830:1b18::,网络掩码为 64。
ip -6 addr
在干净的/刚创建的服务器上运行,将输出以下内容:
root@test:~# ip -6 addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 state UNKNOWN qlen 1000
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 2001:bc8:1830:1b18::1/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::dc2e:4cff:fe57:a019/64 scope link
valid_lft forever preferred_lft forever
我在论坛和其他问题/答案中看到,为了能够绑定到子网中的地址,您需要运行以下命令:
ip add add local 2001:bc8:1830:1b18::/64 dev lo
ip route add local 2001:bc8:1830:1b18::/64 dev ens2
sysctl -w net.ipv6.ip_nonlocal_bind=1
执行这些命令后,输出ip -6 addr
如下:
root@test:~# ip -6 addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 state UNKNOWN qlen 1000
inet6 2001:bc8:1830:1b18::/64 scope global
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 2001:bc8:1830:1b18::1/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::dc2e:4cff:fe57:a019/64 scope link
valid_lft forever preferred_lft forever
例如,现在我可以 ping6,而2001:bc8:1830:1b18::9
不仅仅是2001:bc8:1830:1b18::1
,但只能从我自己的服务器。 而不是从其他服务器/网络。
如果没有sysctl -w net.ipv6.ip_nonlocal_bind=1
,wget 将返回以下内容:
root@test:~# wget --bind-address=2001:bc8:1830:1b18::2 -v google.com
--2021-08-13 00:29:45-- http://google.com/
Resolving google.com (google.com)... 2a00:1450:400e:802::200e, 142.250.179.174
Connecting to google.com (google.com)|2a00:1450:400e:802::200e|:80... failed: Cannot assign requested address.
Connecting to google.com (google.com)|142.250.179.174|:80... failed: Address family not supported by protocol.
现在,执行完这 3 条命令后,当我运行 wget 并将 --bind-address 标志设置为除 之外的子网中的地址时2001:bc8:1830:1b18::1
,请求将永远挂起:
root@test:~# wget --bind-address=2001:bc8:1830:1b18::1 google.com
--2021-08-12 23:55:48-- http://google.com/
Resolving google.com (google.com)... 2a00:1450:400e:802::200e, 142.250.179.174
Connecting to google.com (google.com)|2a00:1450:400e:802::200e|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.google.com/ [following]
--2021-08-12 23:55:48-- http://www.google.com/
Resolving www.google.com (www.google.com)... 2a00:1450:400e:80f::2004, 142.251.36.4
Connecting to www.google.com (www.google.com)|2a00:1450:400e:80f::2004|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html’
index.html [ <=> ] 13.03K --.-KB/s in 0.009s
2021-08-12 23:55:48 (1.44 MB/s) - ‘index.html’ saved [13342]
root@test:~# wget --bind-address=2001:bc8:1830:1b18::2 google.com
--2021-08-12 23:55:52-- http://google.com/
Resolving google.com (google.com)... 2a00:1450:400e:802::200e, 142.250.179.174
Connecting to google.com (google.com)|2a00:1450:400e:802::200e|:80...
lsof
对于 wget 进程,内容如下:wget 3413 root 3u IPv6 58660 0t0 TCP[2001:bc8:1830:1b18::2]:56623->ams15s41-in-x0e.1e100.net:http (SYN_SENT)
我做错了什么?提前谢谢您!