Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save estorgio/1d679f962e8209f8a9232f7593683265 to your computer and use it in GitHub Desktop.
Save estorgio/1d679f962e8209f8a9232f7593683265 to your computer and use it in GitHub Desktop.
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
@HansKappert
Copy link

HansKappert commented Mar 8, 2021

  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.

Did you substitute with the name of your user account?

@vijaybnotion
Copy link

https://www.youtube.com/watch?v=9-teQnZ8LEY
Follow this beautiful tutorial from youtube. (this is part 10, please watch part 9 as well)

@saurav-chandra
Copy link

saurav-chandra commented Jul 7, 2021

VirtualBox version: 6.1.22 r144080 (Qt5.6.2)
Host: Windows 10 Enterprise 1909
Guest: ubuntu 16.04 LTS
Description:
followed all the steps, it is working in other ubuntu versions, but not with the one i have
I am getting error in the mount step itself....
on running this command: sudo mount -t vboxsf ShareVM ~/Desktop/ShareVM
getting error: /sbin/mount.vboxsf: mounting failed with the error: Invalid argument
any idea what to do?

@Koi-foo
Copy link

Koi-foo commented Jul 17, 2021

VirtualBox version: 6.1.22 r144080 (Qt5.6.2)
Host: Windows 10 Enterprise 1909
Guest: ubuntu 16.04 LTS
Description:
followed all the steps, it is working in other ubuntu versions, but not with the one i have
I am getting error in the mount step itself....
on running this command: sudo mount -t vboxsf ShareVM ~/Desktop/ShareVM
getting error: /sbin/mount.vboxsf: mounting failed with the error: Invalid argument
any idea what to do?

You are mounted in root space.
Use the full path from the root, for example /home/USER/ShareVM. The ShareVM directory must exist. If you plan to upload files, the user must be granted permissions.

@stefanlduplessis
Copy link

Worked for me to the T. Thank you so much for a comprehensive and easy to follow guide.

@masadcv
Copy link

masadcv commented May 19, 2022

Host: Ubuntu 20
Guest: Ubuntu 22

This worked perfectly - Many thanks for the guidance!

@alaa-shubbak
Copy link

how did you solve it?
I faced the same, my VM is broken and i don't know how to fix it.
should i try to delete it and reinstall it again?!

@Danielgb23
Copy link

Danielgb23 commented Aug 23, 2022 via email

@psravi
Copy link

psravi commented Aug 25, 2022

This worked perfectly.
Host: Ubuntu 22.04 LTS
Guest: Windows 10 Pro
VirtualBox 6.1.36

@berbawy
Copy link

berbawy commented Jan 3, 2023

Brilliant !!
These steps solved the common mounting issue I face using vagrant

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment