如何使用 iptables 转发 IPsec VPN

如何使用 iptables 转发 IPsec VPN

您好,我对如何使用 iptables 转发 IPsec VPN 数据有一些疑问。以下是我想要做的事情:

WAN 计算机 -- (eth1/WAN IP)服务器 1(eth0/10.81.1.2) -- (eth0/10.66.2.3)服务器 2(eth1/WAN IP) -- WAN

笔记:Server1和Server2的内部网络可以连通

我已尝试在 Server1 上设置这些:

iptables -t nat -A PREROUTING -p udp --dport 4500 -j DNAT --to-destination 10.66.2.3
iptables -t nat -A PREROUTING -p udp --dport 500 -j DNAT --to-destination 10.66.2.3
iptables -t nat -A PREROUTING -p udp --dport 1701 -j DNAT --to-destination 10.66.2.3
iptables -t nat -A POSTROUTING -p udp -d 10.66.2.3 --dport 4500 -j SNAT --to-source 10.81.1.2
iptables -t nat -A POSTROUTING -p udp -d 10.66.2.3 --dport 500 -j SNAT --to-source 10.81.1.2
iptables -t nat -A POSTROUTING -p udp -d 10.66.2.3 --dport 1701 -j SNAT --to-source 10.81.1.2
iptables -A FORWARD -p esp -j ACCEPT
iptables -A FORWARD -p ah -j ACCEPT

但是现在我无法使用 Server1 的 WAN IP 连接到 WAN 计算机上的 Server2(可以使用 IPsec VPN 通过 WAN 直接连接到 WAN 计算机上的 Server2)。

我可能有些部分搞错了,请问要如何设置才能使用Server1连接Server2来访问WAN呢?

答案1

我建议你这样做

eth0 是你的“公共接口”

/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

主动路由

/bin/echo 1 >  /proc/sys/net/ipv4/ip_forward

设置 nat 将请求重定向到内部 ipsec 服务器

/sbin/iptables -t nat -A PREROUTING -i eth0 -p utp --dport 1701 -j DNAT --to-destination 10.66.2.3:1701
/sbin/iptables -t nat -A PREROUTING -i eth0 -p utp --dport 500 -j DNAT --to-destination 10.66.2.3:500
/sbin/iptables -t nat -A PREROUTING -i eth0 -p utp --dport 4500 -j DNAT --to-destination 10.66.2.3:4500

答案2

您的 iptables 设置看起来不错。您是否已在 server1 上启用 ip 转发?(默认情况下禁用)

echo 1 > /proc/sys/net/ipv4/ip_forward

相关内容