virtualbox port forwarding does not work

5

1

My Ubuntu is the guest OS running on a windows host. NAT is used and port forwarding has been added. All access to host on port 3000 should be forwarded to guest on the same port.

But that port can be only accessed on guest machine but not accessible from host machine.

With bridged network setting, there is no trouble. But my concern is that when I work offline(no internet connection) with my host machine, I can't access the guest via that port number any more. I've tried to disable the connection on host, then can't connect the port 3000.

What might be wrong with my installation and default NAT configuration(v4.3.20)?

canoe

Posted 2014-12-02T09:33:27.717

Reputation: 171

similar issue is when having nested VM running on Windows, that is VM ubuntu running VM ubuntu - normal portforwarding suddenly does not work – serup – 2017-02-15T17:21:41.427

Answers

2

Another method is continue use one NAT adapter but use "127.0.0.1" as the port forwarding host IP. The port forwarding rule will be something like (you might add one for UDP also)

Protocol : TCP, Host IP : 127.0.0.1, Host port : 3000, Guest IP : "guest ip", Guest port : 3000

Then you can use 127.0.0.1:3000 to access the guest.

The loopback interface IP 127.0.0.1 is always online even the physical network is disconnect.

Eric Leung

Posted 2014-12-02T09:33:27.717

Reputation: 302

My trouble is when I use NAT, port 3000 is not accessible from host. access from guest machine to the same port has no issue. – canoe – 2014-12-04T05:36:00.980

2You can verify VirtualBox port forwarding function by check output of "netstat -an -p tcp". If you see "127.0.0.1:3000 0.0.0.0:0", then it is setup correctly. If it is the case, you might want to check Ubuntu firewall setting if it allow incoming port 3000 from network interface. Access from guest machine itself often use loopback interface that is always allow. – Eric Leung – 2014-12-04T13:32:59.810

1OK. Adapter 1 is enabled and attached to 'NAT', the name field is grayed out and empty. Is this correct? now the guest OS has no connection to internet. – canoe – 2014-12-04T13:57:07.257

When selected "NAT", the name field grayed out and empty is correct. When click advanced, is the cable connected ? What entry in Port Forwarding ? – Eric Leung – 2014-12-04T14:55:22.520

Cable is connected. When TCP/HostIP, GuestIP unset/ HostPort=GuestPort=3000/ doesn't work. When HostIP is changed to 127.0.0.1. It still doesn't work. No internet connection from guest and port 3000 is not shown on host machine, but can be seen on guest. – canoe – 2014-12-06T04:47:33.643

Need to see why guest can't access Internet, otherwise port forwarding never work. In guest, what IP/gateway it have ? What is the output of "ip route" ? – Eric Leung – 2014-12-07T03:03:33.587

default via 10.0.2.2 dev eth0
10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15
10.0.3.0/24 dev lxcbr0 proto kernel scope link src 10.0.3.1
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.40
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
– canoe – 2014-12-14T09:02:23.540

Other than normal eth0/1 interfaces it have lxc and bridge interfaces also (probably because LXC installed). Did those eth0/1 interfaces are part of the bridge ? What is the output of brctl show ? If it is, you might need to disable to bridge iptables by sysctl –w net.bridge.bridge-nf-call-iptables=0 – Eric Leung – 2014-12-16T13:20:22.397

From the output it seems eth0/1 are not part of the bridge. bridge name bridge id STP enabled interfaces\n lxcbr0 8000.000000000000 no\n virbr0 8000.000000000000 yes\n I don't use LXC myself maybe it is installed by other software that needs it. – canoe – 2014-12-28T13:09:06.743

this worked for me with a ubuntu VM nested ubuntu VM on a windows - running jenkins with portforwarding 127.0.0.1 8080 --> 8080 ; first adapter was NAT and second set to Host-only Adapter vboxnet0 ; this portforwarding done in nested VM – serup – 2017-02-15T20:35:35.517

0

You have to add 2 adapters inorder to that. 1 adapter for NAT & another one for Host. With the Host Only Adapter your Host and Guest OS can communicate. With NAT the Guest connects to the internet.

1)Set 1 Host Only Adapter & 1 NAT adapter
2)Enable Virtual Box Network Adapter in your Host Machine.
3)Ping both Host and the Guest and ensure both are connected. 
4)Now Create Port Forwarding on the Host Only Adapter.

Now it will work

Anbu

Posted 2014-12-02T09:33:27.717

Reputation: 875

I am blocked at step 3). and can't ping the gateway 10.0.2.2 on the guest - "the destination host is unreachable". – canoe – 2014-12-02T10:07:07.877

Ping the Host Only Adapter's IP address not the NAT's one. What are the IP addresses that are configured to NAT & Host Only adapter? And your Host's Virtual Box Adapter's IP address – Anbu – 2014-12-02T10:08:22.303

NAT CIDR:10.0.2.0/24, Host-Only adapter: 172.16.2.1/255.240.0.0, host's virtual box ip: 192.168.1.101/255.255.255.0. The ping from guest to host-only adapter and virtual box host returns network unreachable. – canoe – 2014-12-02T11:01:59.460

you have to change the IP address of the Host Only's Adapter in Guest OS. Change it to 192.168.1.50 / 255.255.255.0 . Then Ping the both. I will work – Anbu – 2014-12-02T11:19:51.263

Changed. Then I reboot the guest linux. Something wired happens. Now guest eth0 has only an IPv6 ip, the previous v4 ip is gone. Guest IPs - eth0: fe80::a00:27ff:fe94:e29a/64, lo:127.0.0.1, lxcbr0: 10.0.3.1/24 virbr0: 192.168.122.1/24. – canoe – 2014-12-02T11:40:01.290

You dont have to reboot the Linux. check whether the Adapaters are connected. Or You have to manually connect in the Virtual Box settings – Anbu – 2014-12-02T11:48:47.493

The virtual box(host-only adapter) status shows cable connected. – canoe – 2014-12-02T12:22:58.070

Identify the name of Host only Adapter, whether it is eth1 or eth0. Then open the Terminal in linux and run ifdown eth1 && ifup eth1. replace "eth1" if your adapter is eth0. – Anbu – 2014-12-02T12:37:56.203

There is no such name on windows which has the host-only adapter. On linux, it is eth0. ifup eth0 takes about 10 minutes in linux to finish and has output like "DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 7 (xid=0x64e25def). No DHCPOFFERS received. No working leases in persistent database - sleeping." Do I need to configure the dhcp server in virtual box? – canoe – 2014-12-02T12:54:31.370