Tuesday, November 16, 2010

NAT Port forwarding in VirtualBox (Windows Host)

I normally like to use a Bridged connection when I use a virtual machine with VirtualBox, but sometimes I need to use NAT networking. Using NAT makes it a bit harder to interact with the virtual machine if the virtual machine is running any servers (like an OpenSSH server, and HTTP server, etc).

I recently created an Ubuntu server with NAT networking (under a Windows host) and wanted to connect to the server with Putty. The setup is a little tricky, but the VirtualBox manual was a huge help.

In this example, I am going to map port 2222 on the host system to port 22 (the standard port for SSH) on the guest system.
  1. On the host system (Windows) open a command prompt
    • (Start Menu, "Run", enter cmd in the box and hit "OK")
  2. Browse to the location of the VirtualBox executable
    • For example: cd "C:\Program Files\Oracle\VirtualBox"
  3. Enter the following command:
    • VBoxManage.exe modifyvm "My VM name" --natpf1 "guestssh,tcp,,2222,,22"
    • (Make sure to change "My VM name" to the actual name of your VM that you are modifying
  4. If the virtual machine is running, shut it down.
  5. Shut down the VirtualBox gui if it is running
  6. Start your virtual machine
  7. Open putty, and connect to localhost at port 2222 (this will get forwarded to port 22 on the guest)
  8. You should be in business!

EDIT: I forgot to mention how to verify that your settings were successful:

In the same command window, enter the following command:
VBoxManage.exe showvminfo "My VM name" | grep NIC

The output should look something like this:
NIC 1: MAC: 0800274DB294, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0
NIC 1 Settings: MTU: 0, Socket( send: 64, receive: 64), TCP Window( send:64, receive: 64)
NIC 1 Rule(0): name = guestssh, protocol = tcp, host ip = , host port = 2222, guest ip = , guest port = 22
NIC 2: disabled
NIC 3: disabled
NIC 4: disabled
NIC 5: disabled
NIC 6: disabled
NIC 7: disabled
NIC 8: disabled

(Note the rule that was added is highlighted)

2 comments:

  1. It worked perfectly in my environment!
    Thanks for the useful information.
    André

    ReplyDelete
  2. Thank you. This command ;

    VBoxManage.exe modifyvm "lolo" --natpf1 "rule1,tcp,,2222,,80"

    worked perfectly for me. I modified it to forward to the apache server I'm running on the VM.

    ReplyDelete