以Debian 12为例,A设备为双栈网络,B设备为仅IPv6网络,两端设备均需要安装wireguard
apt install wireguard-tools
实现思路:
使用wireguard将两端设备组成一个局域网,端点为ipv6地址,两端分配内网IPv4地址,在A设备给内网网段配置NAT,实现B设备走A设备的网关出网。
实现步骤:
wireguard的配置文件位于
/etc/wireguard
目录,默认情况下里面什么也没有,进入到该目录cd /etc/wireguard
创建密钥对,AB设备均需操作:
创建私钥, 保存为priv_key文件
wg genkey > priv_key
根据私钥生成公钥
`wg pubkey < priv_key > pub_key`
现在两设备都生成了密钥,可以通过
cat priv_key
查看私钥,cat pub_key
查看公钥编写配置文件,创建vnet.conf,规划了一段/30,10.80.90.0/30,除去头尾,还剩10.80.90.1 10.80.90.2可用
A设备:监听12345端口,用于B设备连接, PostUp时添加nat规则,其中eth0为主网卡出口,请根据实际情况自行替换
[Interface] PrivateKey = 替换为A设备的私钥 Address = 10.80.90.1/30 ListenPort = 12345 PostUp = echo "1" > /proc/sys/net/ipv4/ip_forward PostUp = iptables -t nat -A POSTROUTING -s 10.80.90.0/30 -o eth0 -j MASQUERADE PostDown = iptables -t nat -D POSTROUTING -s 10.80.90.0/30 -o eth0 -j MASQUERADE [Peer] PublicKey = 替换为B设备的公钥 AllowedIPs = 10.80.90.2/32
B设备:
[Interface] PrivateKey = 替换为B设备的私钥 Address = 10.80.90.2/30 [Peer] PublicKey = 替换为A设备的公钥 AllowedIPs = 0.0.0.0/0 Endpoint = [A设备的v6地址]:12345
启动wireguard, 并开机自启
systemctl start wg-quick@vnet
systemctl enable wg-quick@vnet
测试
PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
64 bytes from 114.114.114.114: icmp_seq=1 ttl=88 time=37.8 ms
64 bytes from 114.114.114.114: icmp_seq=2 ttl=88 time=37.7 ms
64 bytes from 114.114.114.114: icmp_seq=3 ttl=80 time=37.8 ms
实现完毕。