使用 WireGuard 搭建 IPv6 隧道

1 安装服务端软件

1
sudo apt install wireguard

我们需要使用iptables来处理路由,用resolvconf来处理DNS解析,如果系统没有安装这两个软件,那么运行以下命令:

1
sudo apt install resolvconf iptables

2 配置服务器系统

编辑/etc/sysctl.conf,去掉以下两项的注释,或者直接在文件尾部加上这两行:

1
2
net.ipv4.ip_forward=1  
net.ipv6.conf.all.forwarding=1

然后运行这个命令以应用设置:

1
sudo sysctl -p

3 编写配置文件

WireGuard 的 wg-qucik 脚本配置文件格式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 本机虚拟网卡的配置,同时指定转发途径。
[Interface]
Address = 192.168.100.1/32, fd00:ea00:2000:1::1/32
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51200
PrivateKey = Your_Server’s_PrivateKey
DNS = 223.5.5.5, 240c::6666

# 对等点配置,有几个客户端就需要写几个对等点。
[Peer]
PublicKey =Your_Client’s_PublicKey
AllowedIPs = 192.168.100.2/24, fd00:ea00:2000:1::2/64

一组示例的密钥与其搭配的公钥:
密钥:qMiw52uDSAa0+uIZBaWZjFVo6rER4DhaNE+vVqYUjFA=
公钥:iJPYfkEu1fW8+DuLIaWmmrWQbKsZF1Xq446NfePHLw8=

在这个配置文件中使用的是用户 IPv6 地址段,如果你有公网 IPv6 地址段也可以替换上去,以获得更加完美的体验。

4 客户端

1
2
3
4
5
6
7
8
9
10
11
12
13

# 本机虚拟网卡配置,所使用的 IP 地址必须是服务端指定的地址。
[Interface]
Address = 192.168.100.2/24, fd00:ea00:2000:1::2/64
PrivateKey = Your_Client’s_PrivateKey
DNS =223.5.5.5, 240c::6666

# 对等点配置,服务端的信息。
[Peer]
PublicKey = Your_Server’s_PublicKey
AllowedIPs = 0.0.0.0/0, ::0/0
Endpoint = Your_Server’s_IP_Address:51200
PersistentKeepalive = 15

5 服务端开机启动

systemd 服务

1
sudo systemctl enable wg-quick@wg0.service

OpenRC

首先启用 OpenRC 的 local 服务,然后新建 /etc/local.d/wg-quick.start 文件:

1
wg-quick up wg0