VirtualBox - Sharing a host folder on Ubuntu guest

Dec. 23, 2013

I have been using VirtualBox to run Ubuntu Server on my Windows PC over the last couple of weeks. The more time I spend with it, the more I find the need to get myself acclimatized with the good old command line method to interact with your OS. Though I'm a pretty fast learner and have spent quite a bit of time on Linux and its variants in the past, the sheer number of commands that one has to learn to manage the large array of interfaces in present-day OS is astounding.

For example, I wanted to share a folder from my Windows host to the Ubuntu guest. How do I go about doing it? How do I mount a USB stick on guest OS?

Luckily, answers to most of these questions are only a few clicks away with the help of Google. However, since I expect to carry out some of these tasks quite often in the future, I'm going to try and record things that I discover and re-learn in various blog posts, so as to act as a reference for myself. This post talks about how to go about accessing a Host folder that is shared through VirtualBox in the Ubuntu guest through command line.

1. Install Guest Additions Package

For this, you have to first install the Guest Additions package that comes with VB. It's rather straightforward if you have the Ubuntu GUI installed -- just select the supplied .iso image as your CD drive content and voila, the autorun-like mechanism pops. But on a OS without the GUI, you need to mount it yourself. This is how:

mount -o loop /dev/sr0 /media/cdrom
Of course this will succeed only after the Guest Additions CD has been inserted into the guest's CD drive. /media/cdrom is typically created when Ubuntu is installed. If not use a different folder path (one that exists) or create one to your liking and use the path to it.
Once successfully mounted, run the *VBoxLinuxAdditions.run* from /*media/cdrom* (you need root privileges, so either use *sudo* or *su* as root). This could take a little while as Guest Additions module has to be built.

2. Mount the Shared Folder

Once this is done, issue the following command to mount the shared folder
sudo mount -t vboxsf temp /mnt/winshare/temp
*temp* here is the folder that's shared from Windows and */mnt/winshare/temp* is the folder in the Ubuntu guest where this folder is mapped to. If */mnt/winshare/temp* does not exist, create it first. And temp must have been shared from **VirtualBox → Settings → Shared Folders**.