Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mounting VirtualBox shared folders on Ubuntu Server 16.04 LTS

Update 28 July 2019: An updated version of this guide for Ubuntu Server 18.04 LTS is now available. Feel free to check it out.

Update 23 May 2020: This guide is ALREADY OUTDATED and might no longer work with new versions of Ubuntu and VirtualBox. Please consider switching to the updated guide instead. I will no longer respond to the replies to this gist. Thank you.

Mounting VirtualBox shared folders on Ubuntu Server 16.04 LTS

This guide will walk you through steps on how to setup a VirtualBox shared folder inside your Ubuntu Server guest. Tested on Ubuntu Server 16.04.3 LTS (Xenial Xerus)

Steps:

  1. Open VirtualBox

  2. Right-click your VM, then click Settings

  3. Go to Shared Folders section

  4. Add a new shared folder

  5. On Add Share prompt, select the Folder Path in your host that you want to be accessible inside your VM.

  6. In the Folder Name field, type shared

  7. Uncheck Read-only and Auto-mount, and check Make Permanent

  8. Start your VM

  9. Once your VM is up and running, go to Devices menu -> Insert Guest Additions CD image menu

  10. Use the following command to mount the CD:

sudo mount /dev/cdrom /media/cdrom
  1. Install dependencies for VirtualBox guest additions:
sudo apt-get update
sudo apt-get install build-essential linux-headers-`uname -r`
  1. Run installation script for the guest additions:
sudo /media/cdrom/./VBoxLinuxAdditions.run
  1. Reboot VM
sudo shutdown -r now
  1. Create "shared" directory in your home
mkdir ~/shared
  1. Mount the shared folder from the host to your ~/shared directory
sudo mount -t vboxsf shared ~/shared
  1. The host folder should now be accessible inside the VM.
cd ~/shared

Make the mount folder persistent

This directory mount we just made is temporary and it will disappear on next reboot. To make this permanent, we'll set it so that it will mount our ~/shared directory on system startup

  1. Edit fstab file in /etc directory
sudo nano /etc/fstab
  1. Add the following line to fstab (separated by tabs) and press Ctrl+O to Save.
shared	/home/<username>/shared	vboxsf	defaults	0	0
  1. Edit modules
sudo nano /etc/modules
  1. Add the following line to /etc/modules and save
vboxsf
  1. Reboot the vm and log-in again
shutdown -r now
  1. Go to your home directory and check to see if the file is highlighted in green.
cd ~
ls

If it is then congratulations! You successfully linked the directory within your vm with your host folder.

Bonus:

How to point apache's web directory to our folder in the host.

  1. Remove apache's old html directory (WARNING! Backup your data if necessary)
sudo rm -rf /var/www/html	
  1. Add a symbolic link in its place
sudo ln -s ~/shared /var/www/html
@fursserg

This comment has been minimized.

Copy link

@fursserg fursserg commented Jun 15, 2018

Some systems may need option comment=systemd.automount in their fstab entry (source).

@ssy341

This comment has been minimized.

Copy link

@ssy341 ssy341 commented Dec 7, 2018

hi,
this guide is a right way,Many thanks! I found so many ways, but all can't work except this.
below code can mount folder as root user

sudo mount -t vboxsf shared ~/shared

but we hope mount folder as ubuntu user or other, try this:

sudo mount -t vboxsf -o uid=1000,gid=1000 shared ~/shared

ref:https://ubuntuforums.org/showthread.php?t=1398340#yui-gen40
ps: through id commond get uid and gid

@natrod

This comment has been minimized.

Copy link

@natrod natrod commented Feb 5, 2019

Great writeup, however if you plan on sharing your apache's web directory through this method, i assume you would face permission issues since you would not be unable to change the group of the contents inside the shared folder. Do correct me if I'm wrong here please.

@binary-person

This comment has been minimized.

Copy link

@binary-person binary-person commented Feb 14, 2019

Nice job, only the mounting of the cdrom is a bit wrong. First, you should create a /media/cdrom if the user doesn't have it already sudo mkdir /media/cdrom. The command should be sudo mount -t iso9660 /dev/cdrom /media/cdrom not sudo mount /dev/cdrom /media/cdrom (Reference: https://docs.oracle.com/cd/E10926_01/doc/owb.101/b12150/appbcdmount.htm). Nice job otherwise!

@estorgio

This comment has been minimized.

Copy link
Owner Author

@estorgio estorgio commented Mar 5, 2019

Hi everyone. I'm really surprised that my gist was getting some user engagement (comments, stars, forks, etc). To be honest I wrote this just for my own reference and I didn't expect it would rank well on Google and get some traffic.

Took me this long to notice since I usually don't get notifications when somebody comments on my gist.

@fursserg @ssy341
While I was making this I used a fresh new install of Ubuntu Server 16.04.3 LTS with default VM settings on VirtualBox (with Windows 7 SP1 as host) so if you're using a different setup from mine then chances are you might get some different results.

@natrod
As far as I can remember I didn't encountered any issues with Apache with regards to file permissions. It could be an issue if you're using linux as host. I used Win 7 while I was testing this so that could be a factor.

@scheng123
Thank you very much for the tip. I was just about to upgrade my guest to Ubuntu 18.04 LTS and I found out that CD-ROM mounting no longer works as expected. Maybe I should make a new version of this guide for Ubuntu 18.04 LTS when I have the time.

@timothy

This comment has been minimized.

Copy link

@timothy timothy commented May 21, 2019

I am using VertualBox 6 and Ubuntu 18.04. I do not see a checkbox that says "Make Permanent" as stated in step 7. On step 10 I get "mount: /media/cdrom: mount point does not exist." error, and on step 15 I get "/sbin/mount.vboxsf: mounting failed with the error: Protocol error" error. Let me know if anyone knows what went wrong. Thanks!

@sagrawal-idrc

This comment has been minimized.

Copy link

@sagrawal-idrc sagrawal-idrc commented May 31, 2019

Host: Windows Server 2012 R2
Guest: Ubuntu Server 18.04 LTS

I had to additionally install following to get the shared folder mounted:

sudo apt-get install virtualbox-guest-utils virtualbox-guest-dkms

After rebooting, the shared folder appears under /media; vboxadd-service.service still fails for me though.

@nuistzhou

This comment has been minimized.

Copy link

@nuistzhou nuistzhou commented Jun 18, 2019

Well, according to the chosen answer here, you could just skip steps 9 - 13 if the guest OS is Ubuntu, this works well when host OS is either Windows or Mac OS.

@sliawatimena

This comment has been minimized.

Copy link

@sliawatimena sliawatimena commented Jul 25, 2019

Dear all,

Host: Ubuntu 18.04 LTS
Guest: Windows 10 Pro
VirtualBox 6.0.10

After I issue this command twice
$ sudo apt-get install virtualbox-guest-utils virtualbox-guest-dkms
I got make it permanent tick box. However in my Windows E: is a cdrom drive, not a harddisk as expected.

How to correct this? Thank you very much in advance.

Warmest Regards,
Suryadi

@estorgio

This comment has been minimized.

Copy link
Owner Author

@estorgio estorgio commented Jul 28, 2019

Hi everyone,

I just posted an updated version of this guide for Ubuntu Server 18.04 LTS. Feel free to check it out.
https://gist.github.com/estorgio/0c76e29c0439e683caca694f338d4003

@sagrawal-idrc @sliawatimena
This guide only works with Ubuntu 16.04 LTS. If you're using the newer Ubuntu 18.04 LTS then please see the updated version of the guide.

@xaus-ted

This comment has been minimized.

Copy link

@xaus-ted xaus-ted commented Sep 16, 2019

sütü seven kamyoncu <3

@wewark

This comment has been minimized.

Copy link

@wewark wewark commented Mar 12, 2020

  1. Add the following line to fstab (separated by tabs) and press Ctrl+O to Save.
    shared /home/<username>/shared vboxsf defaults 0 0

Doing so lead to the machine booting in emergency mode, It loads normally when I remove it.

@maxphere

This comment has been minimized.

Copy link

@maxphere maxphere commented Mar 18, 2020

my ubuntu VM cannot start after running the steps of "Make the mount folder persistent". How to recover back?

@mortezasabihi

This comment has been minimized.

Copy link

@mortezasabihi mortezasabihi commented Apr 1, 2020

image

if i save like this, i get an error after reboot. what is wrong?

@hdoggiedawg

This comment has been minimized.

Copy link

@hdoggiedawg hdoggiedawg commented Apr 2, 2020

Thanks! I had forgotten about installing the Guest Additions from CD. After step 9, I didn't have to do anything else, as VirtualBox 6.0.16 automatically ran the installation script (VBoxLinuxAdditions.run) and mounted the shared folder. Caveat: Mac host, Ubuntu Desktop 16.04.1 guest.

It doesn't seem to use the /etc/fstab file, as there was no entry for the shared folder. But the mount command shows
/home/justin/mydocs on /home/justin/mydocs type vboxsf (rw,nodev,relatime,iocharset=utf8,uid=0,gid=999,dmode=0770,fmode=0770,tag=VBoxAutomounter)

@hdoggiedawg

This comment has been minimized.

Copy link

@hdoggiedawg hdoggiedawg commented Apr 2, 2020

Spoke too soon. It seems there's an extra step related to permissions, for some reason (see https://www.howtogeek.com/187703/how-to-access-folders-on-your-host-machine-from-an-ubuntu-virtual-machine-in-virtualbox/). The mount folder is owned by root:vboxsf and has permissions 770, so you have to add yourself to the vboxsf group.
sudo adduser $USER vboxsf
and reboot.

@yxsongbo

This comment has been minimized.

Copy link

@yxsongbo yxsongbo commented Apr 3, 2020

the text file can't write by gedit ,but can write by LibreWriter

@abdes-zakari

This comment has been minimized.

Copy link

@abdes-zakari abdes-zakari commented Apr 9, 2020

After running this command : sudo mount -t vboxsf shared ~/shared
i get this error :

/sbin/mount.vboxsf: mounting failed with the error: No such device or address

@benjaoming

This comment has been minimized.

Copy link

@benjaoming benjaoming commented May 18, 2020

@abdes-zakari

I had the same issue on 16.04 after successfully installing Guest Additions .iso, not sure why it didn't start. Instead of installing the Guest Additions .iso, I used packages from Ubuntu repositories and it worked:

sudo apt-get install virtualbox-guest-utils virtualbox-guest-dkms
sudo service virtualbox-guest-utils start
@tonyp7

This comment has been minimized.

Copy link

@tonyp7 tonyp7 commented May 21, 2020

I prefer to add it as a inside crontab -e:

@reboot mount -t vboxsf shared home/user/shared

So you dont mess with fstab

@Perl99

This comment has been minimized.

Copy link

@Perl99 Perl99 commented May 23, 2020

Note that your VirtualBox might be too old for the Ubuntu version. I have installed 20.04 on VirtualBox 5.2.x and the mounting failed. It started working after upgrading to 6.0

@AlexLX4773H

This comment has been minimized.

Copy link

@AlexLX4773H AlexLX4773H commented Jul 12, 2020

Thank you very much for this , am using VirtualBox 6.1 and Ubuntu 20.04, and it works great.

@Danielgb23

This comment has been minimized.

Copy link

@Danielgb23 Danielgb23 commented Oct 3, 2020

This breaks my ubuntu VM

@Danielgb23

This comment has been minimized.

Copy link

@Danielgb23 Danielgb23 commented Oct 4, 2020

This breaks my ubuntu VM

It's because I was using
shared /home//shared vboxsf defaults 0 0
with "shared" instead of the name of my folder in the host

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.