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.

@ds-motivo

This comment has been minimized.

Copy link

ds-motivo commented Sep 6, 2019

VirtualBox version: 5.2.6
Host: Win10
Guest: Ubuntu 16.04
Description: sudo mount -t vboxsf shared ~/shared works but "Make the mount folder persistent" instructions do not.

@rsyqvthv

This comment has been minimized.

Copy link

rsyqvthv commented Sep 11, 2019

VirtualBox version: 5.2.32
Host: Win10
Guest: Ubuntu server 18.04
Description: did exactly same with this instruction, but when I mount to shared folder, it won't give me the write permission, it shows purple, not highlighted with green in my console.

before mount :

drwxrwxr-x 2 john john 4096 Sep 11 07:14 shared

after mount:

dr-xr-xr-x 1 root root 65536 Sep 10 19:18 shared

I can't even chmod or chown.

any advice? thanks.

=======================
solved, upgrade virtualbox to 6.0.12, then it works without any adjustment.
so it's more likely having problem in old version of guest additions pack

@Lucetia

This comment has been minimized.

Copy link

Lucetia commented Oct 20, 2019

VirtualBox version: 6.0.12 r133076
Host: Windows 10
Guest: Ubuntu 18.04.3
Description:

I'm having issue with it being permanently mounted.

When I do the following:

shared /home/<username>/shared vboxsf defaults 0 0
I have replaced with lucetia and then on boot it says unable to mount /home/lucetia/shared . Then I have to comment out the line I added for Ubuntu to boot up. I even checked in the terminal and /home/lucetia/shared is accessible. Not sure if there's an additional step or something. I even tried replacing /shared with /vboxsf

If it helps I have my shared folder named: "shared" and my shared folder on virtualbox called "vboxsf" and I'm able to get it to mount temporarily, just not permanently.

@estorgio

This comment has been minimized.

Copy link
Owner Author

estorgio commented Oct 20, 2019

@Lucetia
Did you installed the Guest additions as instructed in the first section? Or it could be that you forgot to add the vboxsf line in /etc/modules.

@Lucetia

This comment has been minimized.

Copy link

Lucetia commented Oct 20, 2019

@Lucetia
Did you installed the Guest additions as instructed in the first section? Or it could be that you forgot to add the vboxsf line in /etc/modules.

Yup, followed everything in the guide and everything works. Just that one particular line causes it to error out on rebooting and commenting it out let's reboot fine and remount.

I do have the other line in the /etc/modules part

@estorgio

This comment has been minimized.

Copy link
Owner Author

estorgio commented Oct 20, 2019

@Lucetia
Can you post (preferably) the log of the boot process here? Or maybe just the screenshot of the error message during the boot process? Maybe we could get a clue from there.

@Lucetia

This comment has been minimized.

Copy link

Lucetia commented Oct 20, 2019

@estorgio

I was going to try to grab a copy of the log, but every time I try all I can get is when it boots correctly (after fixing the fstab thing). Granted my Ubuntu knowledge is like 0.00001% so I'm probably missing the obvious. When the error pops up during boot it's too fast to screenshot and it's just an error line saying can't mount and then everything else checks off [ OK ]

@estorgio

This comment has been minimized.

Copy link
Owner Author

estorgio commented Oct 20, 2019

@Lucetia
Try this to get a copy of a boot log: journalctl -b
https://askubuntu.com/a/821801

@Lucetia

This comment has been minimized.

Copy link

Lucetia commented Oct 20, 2019

@estorgio

Yeah I tried that earlier and it wasn't showing me the information, now it is ><

Here's the main snippet

Oct 19 23:46:27 lucetia-VirtualBox systemd[1060]: Started GNOME Terminal Server.
Oct 19 23:46:33 lucetia-VirtualBox sudo[1989]: lucetia : TTY=pts/0 ; PWD=/home/lucetia ; USER=root ; COMMAND=/bin/mount -t vboxsf vboxsf /home/lucetia/shared
Oct 19 23:46:33 lucetia-VirtualBox sudo[1989]: pam_unix(sudo:session): session opened for user root by (uid=0)
Oct 19 23:46:33 lucetia-VirtualBox kernel: vboxsf: SHFL_FN_MAP_FOLDER failed for '/home/lucetia/vboxsf': share not found
Oct 19 23:46:33 lucetia-VirtualBox kernel: vbsf_read_super_aux err=-6
Oct 19 23:46:33 lucetia-VirtualBox sudo[1989]: pam_unix(sudo:session): session closed for user root

The bold line is roughly what shows up in boot. Here's the pastebin of the whole log (since github doesn't support anything but image attachments).

https://pastebin.com/n0R4mWfC

@estorgio

This comment has been minimized.

Copy link
Owner Author

estorgio commented Oct 20, 2019

@Lucetia
From what I can understand from the logs it seems that the vboxsf module that comes with Guest Additions works just fine.

Oct 19 23:43:16 lucetia-VirtualBox kernel: vboxsf: g_fHostFeatures=0x8000000f g_fSfFeatures=0x1 g_uSfLastFunction=29
Oct 19 23:43:16 lucetia-VirtualBox kernel: vboxsf: Successfully loaded version 6.0.12
Oct 19 23:43:16 lucetia-VirtualBox kernel: vboxsf: Successfully loaded version 6.0.12 on 5.0.0-31-generic SMP mod_unload

The only problem is that it tries to look for a shared folder named shared in the VM but couldn't find it.

Oct 19 23:43:16 lucetia-VirtualBox kernel: Adding 2097148k swap on /swapfile.  Priority:-2 extents:6 across:2260988k FS
Oct 19 23:43:16 lucetia-VirtualBox kernel: vboxsf: SHFL_FN_MAP_FOLDER failed for 'shared': share not found

In Add Share step, did you used shared too?

sample

I think the first column in /etc/fstab should match what you've supplied in Folder Name field in Add Share prompt (bolded below).

shared /home/<username>/shared vboxsf defaults 0 0

@Lucetia

This comment has been minimized.

Copy link

Lucetia commented Oct 20, 2019

@estorgio

Changing the first shared to match the name of the folder worked perfectly. I knew somewhere shared had to be changed to match, but I never thought that spot as I thought it was part of some call command for that line.

@estorgio

This comment has been minimized.

Copy link
Owner Author

estorgio commented Oct 20, 2019

@Lucetia
Glad that worked out well for you.

@wargodz009

This comment has been minimized.

Copy link

wargodz009 commented Oct 22, 2019

works great. thank you very much!

@rajatgaur1

This comment has been minimized.

Copy link

rajatgaur1 commented Dec 19, 2019

Works like a charm. Thanks!!!

@noelpuru

This comment has been minimized.

Copy link

noelpuru commented Feb 6, 2020

On MacOS, I ticked the "auto mount" and "make permanent" and it worked, I can open it from the specified target location (/home/name/targetfolder). The only catch is, I have to add my user to the user group "vboxsf". So I don't need to do anything after installing the guest additions

Thanks for this guide!

@itsalb3rt

This comment has been minimized.

Copy link

itsalb3rt commented Feb 12, 2020

Thanks! work perfect

@empurium

This comment has been minimized.

Copy link

empurium commented Feb 18, 2020

Worked marvelously, thank you for this!

@pawols

This comment has been minimized.

Copy link

pawols commented Mar 11, 2020

Perfect guide! Thank you so much!

@abdes-zakari

This comment has been minimized.

Copy link

abdes-zakari commented Apr 9, 2020

Thanks it works (Y)

@ngjuping

This comment has been minimized.

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

This comment has been minimized.

Copy link

ngjuping commented Apr 9, 2020

BTW Thanks estorgio, your article really helped!

@Sa1ners

This comment has been minimized.

Copy link

Sa1ners commented May 22, 2020

Great help, many thanks.

@estorgio

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

bimal30 commented May 26, 2020

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

@smartickle

This comment has been minimized.

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

This comment has been minimized.

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.

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.