I use Linux in Virtual Machines all the time. Most of the time I setup the VM with a bridged network adapter and SSH. This allows the VM to access the Internet and allows logging into the shell remotely using SSH. Sometimes it's not possible to get an IP address using bridged networking, which means you'll need to use NAT instead.

When the VM is behind a NATed network, it's not possible to SSH into it unless you forward a TCP port (say 2222) from the host to the guest port 22 using Port Forwarding on the network interface. This usually works, but for some reason it stopped working in the VM I'm using, so I used another method, SSH tunneling.

SSH tunneling works by opening an SSH connection from the guest (i.e. VM) to the host with the -R option, which does port forwarding for the session. For instance issuing this command:

ssh -R 2222:localhost:22 username@hostname

Will forward TCP port 2222 on the host to TCP port 22 on the guest (i.e the VM). Given that sshd is listening on port 22 on the guest, it is now possible to ssh from the host to the guest using this command:

ssh -p 2222 username@vm

In fact, you can forward port 22 instead and avoid the need to specify -p 2222, thusly:

ssh -R 22:localhost:22 username@hostname

and access the VM using ssh as normal, i.e.

ssh username@vm
Why Would You Do This?
There are few reasons, mostly because the Linux console is very basic. It does not support scrolling or copy/paste. A terminal emulator is needed for this. On Windows this is a Cygwin shell, on Linux it's a Terminal or xterm program.
How Does NAT Networking Work in a VM?
Networking is slightly different between popular VM software such as VirtualBox, VMware and Parallels. However, the concepts are the same. VirtualBox for instance has 3 types of virtual network adapters:
  • bridged
  • NAT
  • host-only
NAT on VirtualBox has a built-in DHCP server that will provide IP address 10.0.2.15 to the guest, the gateway is 10.0.2.1 and the host is 10.0.2.2.

Oddly enough, ipconfig does not show an interface on the host for the Virtual NIC with IP address 10.0.2.2. Instead, VirtualBox uses the host-only network interface, 192.168.56.1. Therefore to access the guest via NAT port forwarding the packets will be routed through 192.168.56.1 then 10.0.2.2, 10.0.2.1 and finally 10.0.2.15.