Accessing a VirtualBox Machine with NAT

The default way a VirtualBox machine connects to the network is using NAT. Since it’s connected to a private network, you can connect to the outside world but other machines cannot contact you (this holds true even for the host machine). NAT is the easiest and most lightweight way of connecting your virtual machine to the network. VirtualBox also supports others ways of connecting to the network such as a bridge adaptor.

My current setup: I have one virtual machine running Ubuntu 9.04 where I have all my development tools and languages installed (ruby, php, python etc). I run VirtualBox 3.0.8 on my MacOS laptop. This way I can be as close to the server environment as possible while building my applications. I run Apache, Passenger, PHP, MySQL on the guest machine and just forward 22 (ssh), 80 (http) and 3306 (mysql) to MacOS this way I can use apps like MySQL workbench to play with my database or Textmate to code.

The easiest way to get access the guest machine is by using “port forwarding”.

VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/Protocol' TCP
VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/GuestPort' 80
VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/Apache/HostPort' 8080
  • Ubuntu Development’ is the name of my guest machine
  • Apache is the name of the service that I want to access
  • TCP is the protocol that is being used
  • 80 is the port that the service is running on (guest operating system)
  • 8080 is the port on the host operating system that will pass on all packets.

Similarly I setup a couple more to make my development easier

VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/MySQL/Protocol' TCP
VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/MySQL/GuestPort' 3306
VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/MySQL/HostPort' 3306

This one will map Mysql Server

VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/SSH/Protocol' TCP
VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/SSH/GuestPort' 22
VBoxManage setextradata 'Ubuntu Development' 'VBoxInternal/Devices/pcnet/0/LUN#0/Config/SSH/HostPort' 2222

And this SSH.

NAT Limitations

  • ICMP protocol is limited; ping relies on ICMP to work.
  • UDP broadcasts are not reliable.
  • Only TCP and UDP protocols are supported

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options