Ubuntu

埠轉發 VBox 虛擬機

  • September 13, 2018

我有一個執行 VirtualBox 的 Ubuntu Server 14.04 和一些機器。這些機器安裝了 OpenStack Fuel,我無法更改 NIC 介面(兩個 hostonly 和一個 NAT)。

其中一台機器正在執行 Fuel Control 面板,但只能通過 hostonly 介面之一( 10.20.0.2 )訪問。

我的家庭網路是 192.168.25.x。主機(Ubuntu)外部是192.168.25.25。

現在我的家庭網路中有一台 Windows 機器,需要訪問執行在 VM(IP 10.20.0.2)中的 Fuel 面板。

我需要的是將來自硬體 192.168.25.25 介面的傳入轉發到 virtualbox hostonly 10.20.0.X VM 介面以達到 IP 10.20.0.2。

這是我的主機 ifconfig 顯示所有介面:

root@AKRAB:~# ifconfig
lo        Link encap:Loopback Local
         inet end.: 127.0.0.1  Masc:255.0.0.0
         endereço inet6: ::1/128 Escopo:Máquina
         UP LOOPBACK RUNNING  MTU:65536  Métrica:1
         pacotes RX:19685 erros:0 descartados:0 excesso:0 quadro:0
         Pacotes TX:19685 erros:0 descartados:0 excesso:0 portadora:0
         colisões:0 txqueuelen:0
         RX bytes:7674590 (7.6 MB) TX bytes:7674590 (7.6 MB)

vboxnet0  Link encap:Ethernet  Endereço de HW 0a:00:27:00:00:00
         inet end.: 10.20.0.1  Bcast:10.20.0.255  Masc:255.255.255.0
         endereço inet6: fe80::800:27ff:fe00:0/64 Escopo:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Métrica:1
         pacotes RX:0 erros:0 descartados:0 excesso:0 quadro:0
         Pacotes TX:167 erros:0 descartados:0 excesso:0 portadora:0
         colisões:0 txqueuelen:1000
         RX bytes:0 (0.0 B) TX bytes:22260 (22.2 KB)

vboxnet1  Link encap:Ethernet  Endereço de HW 0a:00:27:00:00:01
         inet end.: 172.16.0.254  Bcast:172.16.0.255  Masc:255.255.255.0
         endereço inet6: fe80::800:27ff:fe00:1/64 Escopo:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Métrica:1
         pacotes RX:0 erros:0 descartados:0 excesso:0 quadro:0
         Pacotes TX:437 erros:0 descartados:0 excesso:0 portadora:0
         colisões:0 txqueuelen:1000
         RX bytes:0 (0.0 B) TX bytes:137886 (137.8 KB)

vboxnet2  Link encap:Ethernet  Endereço de HW 0a:00:27:00:00:02
         inet end.: 172.16.1.1  Bcast:172.16.1.255  Masc:255.255.255.0
         endereço inet6: fe80::800:27ff:fe00:2/64 Escopo:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Métrica:1
         pacotes RX:0 erros:0 descartados:0 excesso:0 quadro:0
         Pacotes TX:464 erros:0 descartados:0 excesso:0 portadora:0
         colisões:0 txqueuelen:1000
         RX bytes:0 (0.0 B) TX bytes:150336 (150.3 KB)

wlan0     Link encap:Ethernet  Endereço de HW 00:13:46:94:18:c1
         inet end.: 192.168.25.25  Bcast:192.168.25.255  Masc:255.255.255.0
         endereço inet6: fe80::213:46ff:fe94:18c1/64 Escopo:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Métrica:1
         pacotes RX:2354945 erros:0 descartados:4 excesso:0 quadro:0
         Pacotes TX:1237088 erros:0 descartados:0 excesso:0 portadora:0
         colisões:0 txqueuelen:1000
         RX bytes:3455421823 (3.4 GB) TX bytes:103231994 (103.2 MB)

root@AKRAB:~#

你可以看到 wlan0 外部介面(我的家庭網路)和 vboxnet0 隱藏我想去的網路( 10.20.0.2 )。

所有這些地址都是靜態的,包括目標。我想在 VM 中訪問的埠號是 8443(Mirantis Fuel Dashboard)。

試過這個沒有成功:

root@AKRAB:~# iptables -I FORWARD -d 10.20.0.2 -m comment --comment "Accept to forward Fuel DashBoard traffic" -m tcp -p tcp --dport 8443 -j ACCEPT
root@AKRAB:~# iptables -t nat -I PREROUTING -m tcp -p tcp --dport 8443 -m comment --comment "redirect pkts to virtual machine" -j DNAT --to-destination 10.20.0.2:8443
root@AKRAB:~# iptables -t nat -I POSTROUTING -m comment --comment "NAT the src ip" -d 10.20.0.2 -o vboxnet0 -j MASQUERADE

結果:

root@AKRAB:~# iptables -nvL FORWARD
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target     prot opt in     out     source               destination
   0     0 ACCEPT     tcp  --  *      *       10.20.0.2            0.0.0.0/0            /* Accept to forward Fuel DashBoard return traffic */ tcp spt:8443
   0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            10.20.0.2            /* Accept to forward Fuel DashBoard traffic */ tcp dpt:8443

root@AKRAB:~# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 73 packets, 6145 bytes)
pkts bytes target     prot opt in     out     source               destination
  18   912 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8443 /* redirect pkts to virtual machine */ to:10.20.0.2:8443

Chain INPUT (policy ACCEPT 73 packets, 6145 bytes)
pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 759 packets, 47828 bytes)
pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 759 packets, 47828 bytes)
pkts bytes target     prot opt in     out     source               destination
   0     0 MASQUERADE  all  --  *      vboxnet0  0.0.0.0/0            10.20.0.2            /* NAT the src ip */

iptables 規則似乎沒問題,但可能您錯過了啟用 ip 轉發,請嘗試:

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

然後檢查 iptables 規則是否匹配:

iptables -t nat -nvL
iptables -nvL FORWARD

引用自:https://unix.stackexchange.com/questions/299459