I have guest Ubuntu operating systems installed in Oracle VirtualBox. I'm using Ubuntu 10.04 as the host(base) operating system with static public IP addresses.
I can access the Internet on guest(VirtualBox) OS only if I select the NAT as network mode in VirtualBox settings and this is fine, but host(base) to guest(VirtualBox) networking is not working.
I want to connect my guest OS from host via ssh.
I need a step by step guide.
I wrote a HOWTO for Linux Journal regarding port forwarding within NAT in VirtualBox here: http://www.linuxjournal.com/content/tech-tip-port-forwarding-virtualbox-vboxmanage
Use the VBoxManage utility to forward an unused local port (like 2222) to port 22 of the virtual machine (assuming that you are using the default port on the VM), which will easily allow you to SSH into your virtual machine.
When you use NAT for your Guest instances, VirtualBox creates a separate "subnet" (10.x.x.x) within which the Guest instances operate. In order to access these instances, you need to set up Static Routes on the Host system. There are plenty of links / guides out there where people have done this.
Alternatively, you could use Bridged Networking instead of NAT. Bridged Networking will put the Guest instances in the same network as your Host operating system, thereby giving you direct access.
The reason is that in NAT mode VirtualBox effectively provides routing of Internet traffic to and from the virtual machine, whereas in bridged mode you have to do this yourself. In other words, you need to set up your host computer as an Internet gateway (with forwarding and SNAT or masquerading). In bridged mode your guest behaves as if it were a completely separate computer on a LAN behind the firewall (or a completely separate computer on the internet, but that will require a separate static IP address), which is sort of the whole point of running a virtual machine.
Since you probably only have one static public IP address, you need to bind another (internal) IP address to the host OS, either on the same interface or on a different one, and then either allow the guest to acquire an IP address on the same internal subnet from a DHCP server or configure it statically that way. Then you set up iptables on the host to do forwarding from the external IP to the internal LAN, plus the relevant rules for SNAT/MASQUERADE, and all should be fine.
NAT with port forwarding was not an option for me. I wanted VirtualBox to work like VMWare Workstation's NAT, where I can ssh/browse from my host to my guest using a consistent IP, and the guest can still get out to the internet. The way to do this is to configure your guest with two network adapters, one NAT and one Host-Only (vbox0). Full instructions can be found here:
http://christophermaier.name/blog/2010/09/01/host-only-networking-with-virtualbox