Networking

使用 OpenVPN 連接兩個網路

  • September 26, 2015

我正在嘗試通過 OpenVPN 將兩個網路連接在一起。

網關可以相互 ping 通,但是它們不能訪問它們加入的網路上的其他電腦。日誌沒有顯示任何錯誤,並且連接已建立。

我在這裡想念什麼?似乎很多人出於各種原因遇到此問題,但經過 50 多次嘗試後,我放棄並決定問:)

網路 1:

dev tun port 1194 ifconfig 10.8.222.40 10.8.222.41 route 10.2.1.0 255.255.255.0 vpn_gateway comp-lzo keepalive 10 60 persist-key persist-tun user nobody group nobody secret /etc/openvpn/static.key

ip route default via 10.0.1.1 dev eth0 10.0.1.0/27 dev eth0 proto kernel scope link src 10.0.1.9 10.2.1.0/24 via 10.8.222.41 dev tun0 10.3.0.0/24 via 10.3.0.2 dev tun2 10.3.0.2 dev tun2 proto kernel scope link src 10.3.0.1 10.8.222.41 dev tun0 proto kernel scope link src 10.8.222.40 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.42.1

網路 2

dev tun port 1194 remote my_ext_ip 1194 ifconfig 10.8.222.41 10.8.222.40 route 10.0.0.0 255.254.0.0 vpn_gateway comp-lzo keepalive 10 60 persist-key persist-tun user nobody group nobody secret /etc/openvpn/static.key

ip route default via 10.2.1.1 dev eth0 10.0.0.0/15 via 10.8.222.40 dev tun0 10.2.1.0/24 dev eth0 proto kernel scope link src 10.2.1.9 10.8.222.40 dev tun0 proto kernel scope link src 10.8.222.41 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.42.1 172.27.224.0/22 dev as0t0 proto kernel scope link src 172.27.224.1 172.27.228.0/22 dev as0t1 proto kernel scope link src 172.27.228.1 172.27.232.0/22 dev as0t2 proto kernel scope link src 172.27.232.1 172.27.236.0/22 dev as0t3 proto kernel scope link src 172.27.236.1

更新:

這是我在 iptables 方面擁有的:

兩個網路: iptables -I FORWARD -i eth0 -o tun0 -m conntrack --ctstate NEW -j ACCEPT iptables -I FORWARD -i tun0 -o eth0 -m conntrack --ctstate NEW -j ACCEPT

網路 1: iptables -t nat -A POSTROUTING -s "10.0.0.0/15" -o "eth0" -j MASQUERADE iptables -A FORWARD -p tcp -s 10.0.0.0/15 -d 0.0.0.0/0 -j ACCEPT

網路 2: iptables -t nat -A POSTROUTING -s "10.2.1.0/24" -o "eth0" -j MASQUERADE iptables -A FORWARD -p tcp -s 10.2.1.0/24 -d 0.0.0.0/0 -j ACCEPT

\

更新 2:

進入隧道: # tcpdump -i tun0 listening on tun0, link-type RAW (Raw IP), capture size 65535 bytes 01:01:15.181262 IP ip-10-8-222-41.ec2.internal > ip-10-0-1-5.ec2.internal: ICMP echo request, id 28767, seq 1, length 64

出隧道: # tcpdump -i tun0 listening on tun0, link-type RAW (Raw IP), capture size 65535 bytes 01:03:44.304930 IP 10.8.222.41 > 10.0.1.5: ICMP echo request, id 28784, seq 1, length 64

切換介面 # tcpdump -i eth0 01:08:56.093291 IP 10.8.222.41 > 10.0.1.5: ICMP echo request, id 28785, seq 3, length 64

解決了!

tcpdump 讓我看到 iptables 轉發的數據包來源是 10.8.222.41/32

所以添加

iptables -t nat -A POSTROUTING -s "10.8.222.41/32" -o "eth0" -j MASQUERADE iptables -A FORWARD -p tcp -s 10.8.222.41/32 -d 0.0.0.0/0 -j ACCEPT

解決了問題!

現在兩個網關都可以 ping 對方網路中的伺服器。現在我需要弄清楚如何允許兩個網路中的所有電腦使用它,但這是一個不同的問題……

因此,從您的 ip route 輸出來看,它看起來像您的

eth0 at Network 1 is 10.0.1.0 with mask 255.255.255.224
eth0 at Network 2 is 10.2.1.0 with mask 255.255.255.0

因此,我將編輯 Network 1 機器上的 openvpn conf 以讀取:

route 10.2.1.0 255.255.255.0  #this is the remote network I want to see

和 Network 2 機器上的 openvpn conf 讀取:

route 10.0.1.0 255.255.255.224 #this is the remote network I want to see

這應該帶來正確的路線。另外,不用擔心在路由行的末尾添加 vpn_gateway 參數,它不是必需的。預設情況下,它會將​​隧道的另一端添加為網關。

引用自:https://serverfault.com/questions/664427