Skip to content

Instantly share code, notes, and snippets.

@potikanond
Last active August 28, 2018 06:38
Show Gist options
  • Save potikanond/263f70e8b27d16248a15186ea083b99a to your computer and use it in GitHub Desktop.
Save potikanond/263f70e8b27d16248a15186ea083b99a to your computer and use it in GitHub Desktop.
How to access VirtualBox shared folder from Ubuntu Server VM

How to access VirtualBox shared folder from Ubuntu VM

Sometimes we might want to share a folder on the VirtualBox host to an Ubuntu Server VM. To be able to do that, there are a couple steps needed to be done.

Reference:

https://websiteforstudents.com/installing-virtualbox-guest-additions-ubuntu-17-04-servers/

Insert Guest Additions CD image

Start the Ubuntu Server VM and navigate to Device=>Insert Guest Additions CD image ... menu.

Next, logon to the server and run the following commands to update the system and install g++ compiler. Note: before doing this step, the DNS resolver has to be fixed.

$ sudo apt update
$ sudo apt install g++

Create a mount point inside the Ubuntu Server

After installing the packages, mount the Guest Tools by running the commands below. This will create the /media/cdrom directory and then mount the Guest Additions CD image to the directory.

$ sudo mkdir -p /media/cdrom
$ sudo mount /dev/cdrom /media/cdrom

Install the Guest Additions Tools

After mounting the image, go into the /media/cdrom directory and run the VBoxLinuxAdditions.run script to install the tools. This process normally compiles and install some kernel modules and tools on your Ubuntu.

$ cd /media/cdrom
$ sh VBoxLinuxAdditions.run

Reboot the system and the next time the system boots up, the shared folder will be mounted in the /media. For example, if the shared folder named tutorial-docker (in VirtualBox) then the shared folder will be mounted in /media/sf_tutorial-docker.

You can check all the mount points using the command below:

$ mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
...
tutorial-docker on /media/sf_tutorial-docker type vboxsf (rw,nodev,relatime)

If we list files in the /media/sf_tutorial-docker, we will see that the owner and group of all the files (and directory) inside the directory become root:vboxsf.

$ ls -al
total 940
drwxrwx--- 1 root vboxsf    224 Aug 28 00:20 .
drwxr-xr-x 4 root root     4096 Aug 26 02:55 ..
drwxrwx--- 1 root vboxsf    384 Aug 26 11:36 lab1
drwxrwx--- 1 root vboxsf   1088 Aug 22 01:48 lab2

This may raise a problem when building a docker image from data files (*.html,...) located in this directory. Because of the root:vboxsf, the data files copied to docker image may have wrong owner:group and permission which leads to unexpected behavior, for example, forbidden access to nginx website.

It is recommended to manually mount the VirtualBox shared folder to Ubuntu VM.

Manually mount shared folder on Ubuntu server

If we do not like the automatically generated mount point, we can manually create a mount point and mount a shared folder to the mount point.

Example, to mount a shared folder tutorial-docker on the VirtualBox host to ~/docker directory, run the following command

$ mkdir ~/docker
$ mount -t vboxsf tutorial-docker ~/docker

Now both VirtualBox host and the Ubuntu VM will share the same storage location.

If we list files in the ~/docker, we will see that the owner and group of all the files (and directory) inside the directory become root:root.

# ls -al
total 940
drwxr-xr-x 1 root     root        256 Aug 28 06:25 .
drwxr-xr-x 7 dpotikan dpotikan   4096 Aug 28 02:48 ..
drwxr-xr-x 1 root     root        384 Aug 26 11:36 lab1
drwxr-xr-x 1 root     root       1088 Aug 22 01:48 lab2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment