WireGuard客户端全流量路由至服务器配置问题求助
WireGuard客户端全流量路由至服务器配置问题求助
各位WireGuard大佬好,我目前搭了个极简的单服务器+单客户端WireGuard环境,服务器有多个公网IP,核心需求是让客户端的所有流量都通过WireGuard隧道走服务器转发到互联网(也就是Client → WG Server → Internet的路径),但配置过程中遇到了两个棘手的问题,想请大家帮忙排查下。
当前环境与配置详情
WireGuard服务器侧配置
服务器的wg0.conf内容如下:
[Interface] PrivateKey = Address = 10.0.0.1/22, fd10:8:0:1::1/64 ListenPort = 51820 # 客户端1配置 [Peer] PublicKey = AllowedIPs = 10.0.0.2/32, fd10:8:0:1::2/128
另外我已经开启了IP转发并配置了iptables规则:
# 启用IPv4和IPv6转发 sysctl -w net.ipv4.ip_forward=1 sysctl -w net.ipv6.conf.all.forwarding=1 # 写入配置文件永久生效 echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf echo "net.ipv6.conf.all.forwarding=1" >> /etc/sysctl.conf # 设置iptables转发与NAT规则 iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i wg+ -j ACCEPT iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
WireGuard客户端侧配置
客户端的wg0.conf内容如下:
[Interface] PrivateKey = Address = 10.0.0.2/22, fd10:8:0:1::2/64 [Peer] PublicKey = Endpoint = publicip:51820 AllowedIPs = 10.0.0.1/32, fd10:8:0:1::1/128
遇到的具体问题
- 部分流量路由的情况:当客户端
AllowedIPs只设置为隧道内网段(10.0.0.1/32, fd10:8:0:1::1/128)时,我能正常ping通隧道内的10.0.0.0/22网段,但客户端的互联网流量完全不走隧道,还是走本地网络出口; - 全流量路由的情况:如果我把客户端
AllowedIPs改成0.0.0.0/0, ::/0(也就是让所有IPv4/IPv6流量都走隧道),启动WireGuard客户端的瞬间就直接断网了,连WireGuard服务器都连不上。
求助方向
想请教大家,这两个问题分别是哪里出问题了?应该怎么调整配置,才能实现客户端全流量通过WireGuard服务器转发到互联网,同时不会出现启动客户端就断连的情况?
备注:内容来源于stack exchange,提问作者Kurogane




