In a previous post I have commented about the bridge mode on VirtualBox. Now, I will explain how to access your virtual machine using NAT network mode.
Before start your virtual machine, we have to set these configurations:
$ VBoxManage setextradata diablo "VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/GuestPort" 22
$ VBoxManage setextradata diablo "VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/HostPort" 9090
$ VBoxManage setextradata diablo "VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/Protocol" TCP
- diablo is the name of the virtual machine
- e1000 is the network card of the virtual machine (it can be pcnet – usually when host machine is not a genuine Intel)
- ssh is the name of the service (you can choose any name)
- 22 is the port of the service inside the virtual machine
- 9090 is the port that will be binded in host machine
- TCP is the protocol used by the service (SSH)
PS.: It’s not necessary to be root to apply these configurations.
Let’s see if the virtual machine is configured correctly:
$ VBoxManage getextradata diablo enumerate
Key: GUI/AutoresizeGuest, Value: on Key: GUI/Fullscreen, Value: off Key: GUI/LastCloseAction, Value: powerOff Key: GUI/LastWindowPostion, Value: 56,35,720,451 Key: GUI/MiniToolBarAlignment, Value: bottom Key: GUI/MiniToolBarAutoHide, Value: on Key: GUI/SaveMountedAtRuntime, Value: yes Key: GUI/Seamless, Value: off Key: GUI/ShowMiniToolBar, Value: yes Key: VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/GuestPort, Value: 22 Key: VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/HostPort, Value: 9090 Key: VBoxInternal/Devices/e1000/0/LUN#0/Config/ssh/Protocol, Value: TCP
It seems OK. Now start your virtual machine and run the command bellow in host machine:
$ netstat -anl | grep 9090
tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN
How you see, when the virtual machine is started, the VirtualBox binds the GuestPort forwarding all connections to HostPort inside the virtual machine.
To access the virtual machine just run the command bellow :
$ ssh -p 9090 localhost
If you wish access some service of the host machine through the virtual machine (assuming you are using NAT mode), just access it by the IP address 10.0.2.2.
My 2 cents.





