I have some Ubuntu virtual machines for do my little labs, and to execute them I use VirtualBox, one good and Open Source virtualization platform.
One of the first things we do after install a new Ubuntu is enable ssh access to them, due is far easy for me to work on terminal windows from my host operating system and not in the virtual console.
To enable ssh is necessary to install the sshd daemon, this can be accomplished with this commands:
sudo apt-get install openssh-server sudo service ssh status
If you have assigned a “Guest only” ethernet adapter to you virtual server, and all works as expected (the VirtualBox Network DHCP assigns an IP to the the virtual machine), you can connect to your new virtual environment with your preferred ssh terminal.
But you may need internet access on your virtual machine and decide to use a “NAT” ethernet adapter, in this case the IP assigned by VirtualBox to you guest operation system is for a subnet not existent on your host computer, and you are unable to connect.
To make it work, in addition to the sshd daemon you need to “route” the relevant ports from the host operating system to the virtual machine. We can do this from the “Port Forwarding” inside the “Advanced” network devices configuration of your virtual machine or using command line (in both cases with the virtual machine stopped).
In this example I redirect (using command line approach) the local port 3022 on my host to the port 22 on my virtual machine, only for TCP protocol, and calling this rule “sshRule” I no use the port 22 on my host due I also have a sshd daemon on my host and uses this port.
/usr/bin/VBoxManage modifyvm "Ubuntu Virtual" --natpf1 "sshRule,tcp,,3022,,22"
After start again the virtual machine I can connect to them with this command:
ssh -p3022 firstname.lastname@example.org
Note the necessary “-p3022” parameter to change the “default” port used by ssh client.
For the ones who need redirect any other port or group of ports, the parameters for the modifyvm are this:
/usr/bin/VBoxManage modifyvm "VirtualMachineName" --natpf<1-N> [<name>],tcp|udp,[<hostip>],<hostport>,[<guestip>], <guestport>