Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mounting VirtualBox shared folders on Ubuntu Server 18.04 LTS (Bionic Beaver)

Mounting VirtualBox shared folders on Ubuntu Server 18.04 LTS (Bionic Beaver)

This guide will walk you through the steps on how to setup a VirtualBox shared folder inside your Ubuntu Server guest.

Prerequisites

This guide assumes that you are using the following setup:

You could still make this guide work with other setups (possibly with some modifications to the commands and whatnot). But if you want to do it the way I did then please feel free to use my setup above.

Initial Steps

  • Open VirtualBox

    screenshot

  • Right-click your VM, then click Settings

    screenshot

  • Go to Shared Folders section

    screenshot

  • Add a new shared folder

    screenshot

  • On Add Share prompt, select the Folder Path in your host that you want to be accessible inside your VM. Type shared for the Folder Name. Make sure that Read-only and Auto-mount are unchecked and Mount point is blank. Then click OK.

    screenshot

  • Start your VM

    screenshot

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

    screenshot

  • Use the following command to mount the CD

    sudo mkdir /media/cdrom
    sudo mount -t iso9660 /dev/cdrom /media/cdrom
    
  • Install dependencies for VirtualBox guest additions

    sudo apt-get update
    sudo apt-get install -y build-essential linux-headers-`uname -r`
    
  • Run the installation script for the guest additions. Wait until the installation completes.

    sudo /media/cdrom/./VBoxLinuxAdditions.run
    
  • Reboot VM

    sudo shutdown -r now
    
  • Create shared directory in your home

    mkdir ~/shared
    
  • Mount the shared folder from the host to your ~/shared directory

    sudo mount -t vboxsf shared ~/shared
    
  • 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

  • Edit fstab file in /etc directory

    sudo nano /etc/fstab
    
  • Add the following line to fstab (separated by tabs). Make sure to replace <username> with your username. Save the file.

    shared	/home/<username>/shared	vboxsf	defaults	0	0
    
  • Edit modules

    sudo nano /etc/modules
    
  • Add the following line to /etc/modules and save

    vboxsf
    
  • Reboot the VM and log-in again

    sudo shutdown -r now
    
  • Go to your home directory and check to see if the directory is highlighted in green.

    screenshot

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

Bonus: Using shared folders as Apache root directory

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

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

Note: This setup works fine with Windows hosts. But if you are using Linux or Mac as the host then you may have to set appropriate file permissions on your host directory with chmod in order to make it work.

Still not working?

If you've followed the steps above and it still doesn't work, please let me know by posting a comment below. Please use the following format and be sure to be as detailed as possible so that I can have enough information to help you out.

VirtualBox version: <insert VirtualBox version here>
Host: <OS name> <version>
Guest: <OS name> <version>
Description: <detailed description of the problem>

Do note that I'm mostly a Windows user and I'm not that well versed with Linux but I will do my best to help you out.

@pawols
Copy link

pawols commented Mar 11, 2020

Perfect guide! Thank you so much!

@abdes-zakari
Copy link

abdes-zakari commented Apr 9, 2020

Thanks it works (Y)

@ngjuping
Copy link

ngjuping commented Apr 9, 2020

Some extra notes:

  1. If you can't insert Guest Additions CD Image (Error dialog box popped out), try "Devices->Optical Drives->Remove Disk From ....", and then load it again.
  2. The command:
    sudo apt-get install -y build-essential linux-headers-`uname -r` , PLEASE DO NOT CHANGE THE uname -r part, it is an embedded command.
  3. The command in (2) is VERY time-consuming. If it fails at some point in time, just run it again, it will continue the download.

In case you lost your Guest Additions CD Image or you want to get the one that suits your current version of VirtualBox, get your iso from this link:
http://download.virtualbox.org/virtualbox/
and you might need to rename your iso file.

*Notice the path and expected name of the iso file!
image

@ngjuping
Copy link

ngjuping commented Apr 9, 2020

BTW Thanks estorgio, your article really helped!

@Sa1ners
Copy link

Sa1ners commented May 22, 2020

Great help, many thanks.

@estorgio
Copy link
Author

estorgio commented May 23, 2020

@ngjuping
Thanks for the extra notes. That really helps.

I also would like to add that some users are installing Guest Additions from the official repositories using apt or apt-get. I don't think this is a good idea since Guest Additions offered by official repositories might not match with the VirtualBox version you are using. By opting to use the Guest Additions that comes with your VirtualBox installation, you can be sure that the Guest Additions version installed on your guest OS exactly matches with the VirtualBox version installed on your host OS.

@bimal30
Copy link

bimal30 commented May 26, 2020

@estorgio Thanks. I was able to get a shared folder working fine in virtual box.

@smartickle
Copy link

smartickle commented Jun 5, 2020

@estorgio thank you for your article been using your help, anyway why not use the article as tree branch? so just select branch version 16.04 or 18.04, it will help a lot and also it will be good if the url for this article is clear not hash

@rsyqvthv okay i got it, for those in need for special chmod and chown,

in /etc/fstab
shared /home/<username>/shared vboxsf rw,exec,uid=<youruserid>,gid=<yourusergroupid>,dmode=755,fmode=644 0 0
example :
modul /odoo/custom/addons vboxsf rw,exec,uid=112,gid=115,dmode=755,fmode=644 0 0
dmode = directory mode
fmode = file mode

after that as usual shutdown -r now

if you check stat /home/<username>/shared it will show you the right ownership with your custom user and group also this apply to the folder and also child files or folders

how to find your user id and your group id or user group id, check this: https://kb.iu.edu/d/adwf (i'm lazy to write it LoL)

Note 1:
auto-mount and make permanent doesn't work using multi folder setting, just tested and only work on Transient folder (or maybe I just don't know how to use 'mount points') anyway i just don't thick those two and multi folder sharing worked fine

Write multi line for multi Shared Folders:

modul11	/odoo11/custom/addons	vboxsf	rw,exec,uid=112,gid=115,dmode=755,fmode=644	0	0
modul12	/odoo12/custom/addons	vboxsf	rw,exec,uid=113,gid=116,dmode=755,fmode=644	0	0

i'm using multiple odoo version and it must have their own user:group (defaults, you can customized it, but better leave it as is)
so i need
user name odoo
group name odoo
or at least for best practice not root:root

Note 2:
again something is not right,
virtual box: Version 6.1.6 r137129 (Qt5.6.2)
host: windows 10
guest: ubuntu 18.04.4
if you edit Shared Folder settings while the terminal is on, for some unknown reason the saved path of Transient Folders is gone when you power down terminal (never happen on latest v5.x and v6.0 vbox).
so have to save while terminal is off and you can tick the auto mount option to use Machine Folders mode

@rpinto73
Copy link

rpinto73 commented Jun 29, 2020

Thanks @estorgio! Just adding a note that helped me with an error.

I kept getting an error of "Unable to insert the virtual optical disk ... into the machine" when I was trying to do Devices-->Insert Guest Additions. I found this thread in the Ubuntu community which helped me fix that part. https://askubuntu.com/a/960324

Then I followed your directions and was able to create two permanent shared folders. Thanks again.

@robfr77
Copy link

robfr77 commented Jul 7, 2020

VirtualBox version: 6.1.8r137981
Host: Microsoft Windows 10 Home 10.0.18363 N/A Build 18363
Guest: Ubuntu Server (64-bit) 18.04
Description: This is the second instance of this error. I followed the documentation exactly in this article except I did not manually mount and install the guest additions CD, so that is my guess for the source of the issue. The VM boots in emergency mode, stating that /sbin/mount.vboxsf: mounting failed with the error: No such device. The shared folder is not being located. I was not doing much of anything other than LAMP setup. Taking a further look at the last couple of comments.

@smartickle
Copy link

smartickle commented Jul 14, 2020

@robfr77 for me using win 10 and also Ubuntu Server (64-bit) 18.04.4, i follow the instruction but always tick the Auto-mount and make permanent folder option but mount point is blank (never care to use this).

You need to define "manually mount" because doing this is manual:
apt install virtualbox-guest-additions-iso

and this also manual:
if you download vbox usually it is included in C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso so you can mount in vbox settings -> storage ->IDE

but that only mounting as inserting cd to pc not mounting cd to mount point (mount the filesystem read/write to proceed) maybe this can be clear:
https://askubuntu.com/questions/22743/how-do-i-install-guest-additions-in-a-virtualbox-vm

you still have to do

mount /dev/cdrom /mnt
cd /mnt
./VBoxLinuxAdditions.run

other than that i don't know how to install guest additions (there is extracting the iso, but never done that), after mount you can install ./VBoxLinuxAdditions.run
and after following all instruction (from estorgio article and also tick the folder option) and is completed you can manually unmount the .iso from vbox setting or from terminal

@asampadeh
Copy link

asampadeh commented Sep 3, 2020

I did install:
VirtualBox 6.1.12
VirtualBox 6.1.12 Oracle VM VirtualBox Extension Pack
ubuntu-20.04.1-desktop-amd64

Mounting shared folder is really a breeze.

I follow the step above:

  • Add Share: choose the folder in windows, and assign folder name "shared" as mentioned.

  • Start VM

  • Go to Devices | Insert Guest Additions CD image

  • Click Run when it asks to install VBox_GA_6.1.12

  • Once finish, just restart VM

After ubuntu is back:

  • Open terminal
  • mkdir ~/shared
  • sudo mount -t vboxsf shared ~/shared

That's all !

Thanks @estorgio

@ebobcat316
Copy link

ebobcat316 commented Oct 1, 2020

Thank you Kabayan! Malupit! @estorgio

works like a charm! in my Virtualbox 6.1 + Ubuntu 20.04.1 LTS

image

@Akshaychdev
Copy link

Akshaychdev commented Nov 8, 2020

Thanks @estorgio @asampadeh, it is very easy now, I'm on VB 6.1.8 and Ubuntu 20.04.1, and didn't got a VB extension pack installed, did the similar steps as @asampadeh mentioned, it just creates a shared mount folder and does the job, also check the option of "make it permanent" when creating the shared folder first.

@lhoupert
Copy link

lhoupert commented Feb 4, 2021

In my case the shared folder is mounted as root. I had modify:

Mount the shared folder from the host to your ~/shared directory

sudo mount -t vboxsf shared ~/shared

by:
sudo mount -t vboxsf shared ~/shared -o uid=1000,gid=1001
(Credits to https://askubuntu.com/a/978965)

@shalombentenzingnamchyo

Thank you.

@AbdelazizSharaf001
Copy link

AbdelazizSharaf001 commented Mar 12, 2021

@estorgio
Guest: Ubuntu-Server 20.04.2 LTS
Description: folders is still need to mount after reboot even after editing /etc/fstab and /etc/modules

@JanzenLiu
Copy link

JanzenLiu commented Mar 31, 2021

thorough and super helpful!

@rodriwasabi
Copy link

rodriwasabi commented May 27, 2021

Guest: Ubuntu Server 20.04.2 LTS
VirtualBox version: 6.1.22

  • Install VBox guest extensions as described
  • Create a shared folder as:
    • ReadOnly: Unchecked
    • Automount: Checked
    • mount path: filled (don't leave it blank)
    • Make permanent: Checked

Reboot machine. There is no need to mount path or touch /etc/fstab & /etc/modules directories

@AbdelazizSharaf001
Copy link

AbdelazizSharaf001 commented May 28, 2021

@estorgio
Guest: Ubuntu-Server 20.04.2 LTS
Description: folders is still need to mount after reboot even after editing /etc/fstab and /etc/modules

I had the same issue when I realized that I'm not writing the actual pass
do not write /home/<username>/shared and write the actual pass with the username if under home
those worked:
/home/ubuntu/shared
/mnt/shared

@doobran
Copy link

doobran commented Jun 6, 2021

"unable to locate package linux-headers-uname-r"

If you have an error when running:
"sudo apt-get install -y build-essential linux-headers-uname -r"

Do this instead:
"sudo apt-get install -y build-essential linux-headers-$(uname -r)"

@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?

@tomsasson-fibernet
Copy link

tomsasson-fibernet commented Aug 29, 2021

VirtualBox version: Version 6.1.26 r145957 (Qt5.6.2)
Host: Windows 10 Version 21H1 (OS Build 19043.1165)
Guest: ubuntu 20.04 LTS
Description:
I succeeded mounting "shared" folder like in the description, but then I wanted to mount another folder.
I followed the same steps, but I receive a message:
/sbin/mount.vboxsf: shared folder '/home/fibernet/UHE01-RX' was not found (check VM settings / spelling)

This is the configuration in the VirtualBox Settings:
image

And this is how the directories are shown on the Ubuntu machine
image

Then what am I missing out? Can't I share another folder of my choice?

EDIT: I want to note that the persistent solution worked, after reboot I had it all shared, only the local commands created issues.

@ST1LLY
Copy link

ST1LLY commented Oct 27, 2021

@tomsasson-fibernet
Hi, I've faced the same issue.

VirtualBox: 6.1.28 r147628 (Qt5.6.2)
Host: Windows 10 Home 10.0.19042
Guest: Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-89-generic x86_64)

/sbin/mount.vboxsf: shared folder '/home/user/some-folder' was not found (check VM settings / spelling)

This answer helped me.

@anhhaibkhn
Copy link

anhhaibkhn commented Nov 26, 2021

It works !!! @estorgio
Thanks for saving my day.

@JWingUtk
Copy link

JWingUtk commented Apr 21, 2022

This guide saved me! No kidding. Been trying to get it to work for HOURS.

My only remark is the following portion did not work in my 20.04 LTS Ubuntu VM.

sudo mkdir /media/cdrom sudo mount -t iso9660 /dev/cdrom /media/cdrom

It mentioned that it would not allow the disk to be written to the directory as it was read only.

This wasn't a real issue as adding the guest additions CD prompted the system to request to run it automatically.

@laurencefass
Copy link

laurencefass commented Apr 28, 2022

Ive installed the extensions and Im using virtualbox 6

when i try: sudo mount -t vboxsf vboxshare~/vboxshare

i see message: "mount: /root/vboxshare: mount point does not exist"

if i manually created /root/vboxshare and rerun the mount command i see:

"mount: /root/vboxshare: mount(2) system call failed: No such file or directory"

@wb6vpm
Copy link

wb6vpm commented Aug 23, 2022

I actually need the opposite of this!

Host: Ubuntu 22.04 LTS (CLI only, no GUI available) running VirtualBox 6.1.34
VM: Windows 10 Pro

Anyone have any pointers to where I can find a walkthrough for this?

@AbdelazizSharaf001
Copy link

AbdelazizSharaf001 commented Aug 23, 2022

@wb6vpm : what exactly you mean by this word ?

I actually need the opposite of this!

what do you need exactly ?

@AbdelazizSharaf001
Copy link

AbdelazizSharaf001 commented Aug 23, 2022

@wb6vpm : if you mean reverse direction sharing then you should see this answer https://superuser.com/a/243768/1050329

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