6

I have enabled two adapters in my VirtualBox (running Ubuntu 12.10 Server Edition), with types as: Host-only and NAT.
My main motive is to be able to connect two VMs running on two different hosts (physical machines) on a port (say 22). I know the answer is Port Forwarding, but it is too confusing. Well I have tried with Bridged mode, there too from outside my machine it is not able to connect to the virtual machine.

Edit: Well I wouldn't suggest Bridged Network, as it takes up IP addresses from the network, hence would prefer a NAT-ing technique.

3
  • 2
    Host-Only is not what you want. You want NAT or Bridged mode. How you enable traffic from the Host to the Guest is up to you. It won't be easy without Port forwarding in NAT mode. Bridged mode means that the Guest will need an IP in the same subnet as the host machine. HTH.
    – NickW
    Mar 4, 2014 at 12:45
  • @NickW: you are very right.....I'm currently trying to do that only, port forwarding with NAT.....but I'm not able to understand what IP/ports to specify in the rule? And also should I mention the rule in the VirtualBox network or in the host? Please help.
    – Tanny
    Mar 4, 2014 at 17:50
  • Well, you say <host.ip>:<port> <-> <guest.ip>.<port> so your rule would be 1.2.3.4:22` maps to 192.168.1.10:22 or something similar, you just forward the ports, you can even forward from a different port to port 22 internally, if you google search, for your host OS, there will be explanations on how to do it.
    – NickW
    Mar 4, 2014 at 17:55

2 Answers 2

5

I figured out the solution after much headbanging (although I figured it out some while ago, replying a bit late). The solution is to use NAT technique with port forwarding. All you have to do is, in the network setting of the vms in VirtualBox add Port Forwarding rules. There are no restriction on the value of the ports and they could be anything as long as there are no conflicting ports and it is advisable not to use certain port numbers.

For setting the rules: Host IP is the IP of your main machine and Guest IP is the NAT IP of the VM. Any request coming to your host on that port would directly be redirected to the guest ip on the port you have mentioned.

NAT port forwarding table

Here the first IP refers to my host machine, i.e. the one on which the VirtualBox is installed. And the second IP refers to the guest machine, i.e. the one which I need to start the server or connect to.

4
  • Then which URL should be used to hit the URL of a guest machine from host Oct 9, 2020 at 9:11
  • Looking at the screenshot I suppose it is either: 1. http://127.0.0.1:8085 ; 2. http://<VM adapter's IP>:8085 (on Windows you can get adapter's IP via Control panel > Network and sharing center > Change adapter settings > open menu of "VirtualBox Host-Only Ethernet Adapter" > open properties > Details > IPv4 address. For me it was like 192.168.x.x.)
    – Kyo
    Feb 4, 2023 at 16:22
  • For me port forwarding works even if I don't specify Host IP and Guest IP in adapter settings. Also when facing a problems with port forwarding I suggest to try disabling firewall in Windows, then restart Virtual Box and VM. Also when VM is running, in Windows open CMD and execute netstat -ab to verify that ports specified in VBox NAT adapter settings is opened successfully. If ports have opened successfully, you will see LISTENING status and VirtualBoxVM.exe process near the port in response from CMD.
    – Kyo
    Feb 4, 2023 at 18:31
  • Also I found out, that VM will be available in local network by IP of your PC's (not VM's) main network adapter + forwarded port number. For me it was like http://10.x.x.x:8085
    – Kyo
    Feb 18, 2023 at 8:56
4

I would advise you to use Bridged mode. This will make your VM having their own IP in same network as the physical host, and behave like any physical machine on same network. Make sure both networks (from each host VM) can connect, and firewall are open on VM to communicate.

4
  • 2
    Actually this method I want to keep it for the last......as in this method, it will actually eat up the IPs.....otherwise Bridged mode is pretty straight forward to implement. Thanks for replying
    – Tanny
    Mar 4, 2014 at 17:51
  • 1
    Be careful when using Bridged mode in environments with augmented security policies: Since the bridge interface uses a different MAC and not the original one of the physical interface the host may be blocked from the network since the unknown MAC might be interpreted as an intruder to the network.
    – mschenk74
    Jan 18, 2015 at 18:30
  • @Tanny, "it will actually eat up the IPs" That doesn't actually mean anything. How do you eat up Internet Protocols (IPs). I think you mean IP addresses. IP means Internet Protocol, and there are only two current IPs: IPv4 and IPv6.
    – Ron Maupin
    Dec 10, 2017 at 4:28
  • @RonMaupin Thank you for pointing that out, my good sir.. It was lazy from my part... I wish I could edit the comment now.. :)
    – Tanny
    Dec 13, 2017 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .