
我有一個小型交換子網,其中有一些與 IP 通訊的設備。它們透過乙太網路連接到運行 Ubuntu 18.04 的 ODroid,該 ODroid 也在 OpenVPN 中。 (ODroid 透過 WiFi 連接到互聯網 atm,稍後將更改為 LTE)
我想從同樣位於 VPN 中的電腦處理在該子網路中的裝置上運行的服務。
+-----------------+ +------------------+ +--------------------+
|A Device | ethernet |ODroid | VPN |Controlling Computer|
|eth0: 192.168.2.2+----------+tun0: 10.8.0.5 +------+tun0: 10.8.0.2 |
+-----------------+ |eth0: 192.168.2.1 | +--------------------+
+------------------+
我嘗試將 iptables 與 nat 結合使用,使控制電腦能夠存取 ODroid 後面的裝置。
[on the ODroid]
sudo iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT
sudo iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
我還在 ODroid 上啟用了轉發
cat /proc/sys/net/ipv4/ip_forward
1
我還新增了到所有設備的靜態路由:
[on the ODroid] ip r
0.0.0.0/1 via 10.8.0.1 dev tun0
default via 192.168.0.1 dev wlan0 proto dhcp metric 600
10.8.0.0/24 dev tun0 proto kernel scope link src 10.8.0.5
85.214.205.172 via 192.168.0.1 dev wlan0
128.0.0.0/1 via 10.8.0.1 dev tun0
192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.106 metric 600
192.168.2.0/24 dev eth0 proto kernel scope link src 192.168.2.1
[on the controlling computer] ip r
0.0.0.0/1 via 10.8.0.1 dev tun0
default via 192.168.0.1 dev enp33s0 proto dhcp metric 100
10.8.0.0/24 dev tun0 proto kernel scope link src 10.8.0.2
[ip of my server] via 192.168.0.1 dev enp33s0
128.0.0.0/1 via 10.8.0.1 dev tun0
169.254.0.0/16 dev enp33s0 scope link metric 1000
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
192.168.0.0/24 dev enp33s0 proto kernel scope link src 192.168.0.105 metric 100
192.168.2.0/24 via 10.8.0.5 dev tun0
[on the device I want to connect to] ip r
default via 192.168.2.1 dev enp0s25 proto dhcp metric 100
10.8.0.0/24 via 192.168.2.1 dev enp0s25
192.168.0.0/24 via 192.168.2.1 dev enp0s25
192.168.2.0/24 dev enp0s25 proto kernel scope link src 192.168.2.109 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown
我可以從控制電腦和我想要連接的裝置 ping ODroid。
從具有我想要連接的服務的設備,我可以連接到控制計算機,因此該方向的 NAT 似乎有效。但我無法以相反的方式連接。
我嘗試過只使用其中之一
sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
但這沒有幫助。
有什麼建議麼?
先致謝