Configure VirtualBox NAT as Network Adapter and Allow putty ssh Through Port Forwarding on Host Machine
Dr. George Jen
This solution is only for Linux VM or any Unix flavor VM that has ssh daemon already started upon boot. If not, you need to install openssh or other ssh daemon
1. Run Virtualbox app, make sure VM is powered off, select the VM in the Vertualbox.
Click setting icon, click network, and change network adapter from bridged adapter to NAT.
Some basic concept about network adapter shown in Virtualbox network settings:
What is bridged adapter?
Bridged adapter is to use the network adapter (such as wireless network interface of your notebook). If setting bridged adapter, VM will get the IP address from DHCP server, therefore, the IP address is dynamic. The problem with bridged adapter is you will need to change the IP address every time your host OS IP address is changed, such as you take your laptop computer to a new place that gets a new IP address from the wifi router because of DHCP.
The better option is NAT adapter.
What is Network Address Translation (NAT)?
NAT gives a virtual machine access to network resources using the host computer's IP address. In Virtualbox, it has IP address fixed at 10.0.2.15 or a fixed IP address. You can find out what the fixed IP address is by opening a terminal window in the VM, run ifconfig as root user.
VM with NAT as network adapter will be able to connect to host machine, and if host machine is connected to the internet, VM can connect to internet. The good thing is you do not need to set the IP address of in your VM, such as in /etc/hosts every time your host IP address is changed, because it stays with fixed 10.0.2.15 (or fixed IP address), no matter where you are.
However, you can not putty/ssh to VM with NAT by default.
2. Following steps are to configure to allow your host computer, such as Windows laptop to putty into the VM when it is up.
To enable putty/ssh connectivity to VM, you need to setup port forwarding:
Select network from setting
Change network adapter from Bridged adapter to NAT
Click Port Forwarding
On the port forwarding pop up window, click Add(+) icon
Enter port forwarding rule:
Host IP: empty (do not enter anything)
Host Port: 30 (you can enter other known unused port)
Guest IP: 10.0.2.15
Guest port: 22
Note: port 22 is the port ssh daemon (server) listens to on guest VM.
Click OK to close port forwarding window
Click OK on the parent Network window
3. Now start VM, wait until it has started (few minutes later)
Now start putty or ssh on host computer
Connect to localhost, but specify port 30, then click open
Click open, you are connected to VM by ssh
Hope this is helpful.