NAT Port Forwarding in VirtualBox

20,119

Solution 1

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.

Solution 2

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.

Share:
20,119
Tanny
Author by

Tanny

Updated on September 18, 2022

Comments

  • Tanny
    Tanny over 1 year

    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.

    • NickW
      NickW about 10 years
      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.
    • Tanny
      Tanny about 10 years
      @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.
    • NickW
      NickW about 10 years
      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.
  • Tanny
    Tanny about 10 years
    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
  • mschenk74
    mschenk74 over 9 years
    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.
  • Ron Maupin
    Ron Maupin over 6 years
    @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.
  • Tanny
    Tanny over 6 years
    @RonMaupin Thank you for pointing that out, my good sir.. It was lazy from my part... I wish I could edit the comment now.. :)
  • TAMIM HAIDER
    TAMIM HAIDER over 3 years
    Then which URL should be used to hit the URL of a guest machine from host