大网数据广告活动
服务器运维问答

WireGuard in routing mode rather than NAT mode (AWS)

· 52CE 我爱测

I have been trying to get this working for a long time and finally decided I would join serverfault in hope that someone can help before I lose my mind. Current setup: Users connect into the AWS network via a WireGuard VPN server…

问题原文

原帖链接 · Tipex · CC BY-SA 4.0

I have been trying to get this working for a long time and finally decided I would join serverfault in hope that someone can help before I lose my mind.

Current setup:

Users connect into the AWS network via a WireGuard VPN server I setup. Its configured in NAT mode which means all users are hidden behind the WireGuard server IP address. They can SSH to machines using hostnames, or IP addresses and can also connect to the internet. Their WireGuard client config is set to route all traffic through the VPN as this adds a layer of protection if they are working in say a coffee shop.

Desired setup:

I would like to change the config so WireGuard is running in routing mode rather than NAT mode. This way users will get a unique IP address and I will be able to use AWS security groups to prevent certain users from reaching certain servers. Adds an extra layer of security which is nice.

How far I have got:

I can get routing mode "working", in that users get a unique IP address, they are still able to SSH to machines using hostnames or IP addresses. The problem I am facing is that they lose internet connection! So I solve one problem but create another. I have tried pinging 8.8.8.8 to rule out DNS being the problem.

Server config (NAT mode commented out):

[Interface]
Address = 10.0.0.162
SaveConfig = false
PrivateKey = abcdefg
ListenPort = 51820

#NAT mode:
#PostUp   = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE;
#PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE;

#Routing Mode attempt 1:
#Machines in AWS show connections from the actual client IP which is good. No internet access though.
PostUp   = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT;
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT;

[Peer] #My_Laptop
PublicKey = abcdefg
PresharedKey = abcdefg
AllowedIPs = 192.168.1.2/32

Client config:

[Interface]
PrivateKey = abcdefg
Address = 192.168.1.2/32
DNS = 10.0.0.162

[Peer]
PublicKey = abcdefg
PresharedKey = abcdefg
AllowedIPs = 0.0.0.0/0
Endpoint = x.x.x.x:51820
PersistentKeepalive = 25

Hopefully the above is enough information to show what I have tried. Note I have BIND running on the same machine as WireGuard. BIND simply forwards all DNS queries to AWS Route53.

I feel like its so close to working but I have been stuck at this point for months.

已采纳解答

原帖链接 · Tipex · CC BY-SA 4.0

I found the solution. It was all to do with iptables in Linux.

The following was needed in the WireGuard config:

PostUp = iptables -t nat -A POSTROUTING ! -d 10.0.0.0/16 -o eth0 -j MASQUERADE;
PostDown = iptables -t nat -D POSTROUTING ! -d 10.0.0.0/16 -o eth0 -j MASQUERADE;

What this does is not masquerade when the traffic is for the local AWS network. This way each user gets their own IP address which I can then use in firewall rules to prevent them reaching certain machines. For all other traffic masquerading is done which then means user can go out to the internet.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-06-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。