Code, Computers & Random Junk

SSH Into Your Virtualhost Guest

A few months ago, I installed and setup Fedora 23 in VirtualbBox. I need to have one setup since I use the “Fedora Cinnamon Spin” on my nephews computers. A great GNU/Linux distribution, and easy for them to learn, and easy for me to manage/maintain from here. That also gives me the opportunity to build, install test a few scripts and programs in my installation before I add them to their computers.

While having their computers hooked up to my (local) network, it’s also very convenient to be able to access and copy files between my computer, my Fedora install in Virtualbox and their computers. To do that in a smooth and nice way - one need to have ssh access to the guest in Virtualbox. Way better that setup shared folders etc.


So, first you need a key. If you don’t already have one, create a new. I’ll use ed25519 in the example…

ssh-keygen -t ed25519 -o -a 100 -f ~/.ssh/id_vbox_ed25519 -C "$(whoami)@$(hostname)-$(date "+%F")"

And add it:

ssh-add ~/.ssh/id_vbox_ed25519

# On OS X you use -M to add it to the keychain.
ssh-add -M ~/.ssh/id_vbox_ed25519

It’s fairly easy to setup the clipboard feature between host and guest, so open the public key in an editor and copy the content and pate it into ~/.ssh/authorized_keys, and change the port to 2222 (example) on the guest. I assume you’ve installed and [setup SSH][setupssh] properly in your guest.

On the host - your computer. Set up an SSH alias… In ~/.ssh/config add something like this at the end of the file.

Host VBox
    User foobar
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_vbox_ed25519
    Port 2222

Now you can use ssh VBox instead of the usual longer line to login - in this case: ssh -p 2222 foobar@, or when using other programs using ssh.

But first we need to make VirtualBox work. It can be done in the GUI, but this is easier.

VBoxManage modifyvm "NameOfGuest" --natpf1 "guestssh,tcp,,2222,,22"

Read more in the Virtualbox documentation.

Test you connection…

ssh Vbox


So, to use it effectively - setup you other machines with simular SSH alises etc. Then you can use them with ssh, scp or rsync for example.

Examples using scp… It’s just like using cp, but over ssh. You need to add -r to copy directories etc.

scp <source> alias:<destination>
scp <source> VBox:<destination>
scp Alias1:<source> Alias2:<destination>

After the colon you’re at your home folder. So, to add a file to ~/Desktop:

scp file.txt VBox:Desktop/  # or…
scp file.txt VBox:Desktop/new_name.txt

Copying a folder:

scp -r Pictures/* VBox:Pictures/

And the othe way around is of course:

scp -r VBox:Pictures/* Pictures/

To copy the files into the directory you’re at…

scp -r VBox:file.txt .

Now, the great use I’ve had is to copy from the Virtualbox host directly to their computers - without going through my computer. So by using the 2 (3) aliases it’ll look something like:

# From VBox host to another computer

scp -r VBox:Pictures/ Nephew1:Pictures/*

Saves a lot of time and steps. And of course - if copying frequently… One saves a lot of unnecessary writes to the SSD, if using one on the host you work from. +1

Happy hacking…