How to access a shared folder in VirtualBox?

Access to shared folders in Virtual Box

Command line

By default, VirtualBox shared folders are created with read/write permission for the guest. This can be done from the command line on the host with:

VBoxManage sharedfolder add "VM name" --name sharename --hostpath "C:\test"

By adding the option --readonly we can restrict these for read-only access. Use the --transient option if you only want the shares to appear in the present session but not persistent for following sessions. There are some limitations for shared folders (see this question for details). If prerequisites are met we may mount these shared folders manually by running the following commands in the guest:

mkdir /home/<user>/vboxshare
sudo mount -t vboxsf -o uid=1000,gid=1000 sharename /home/<user>/vboxshare  

Of course, we can also use different mount options to mount as read/only or mount with read access only to root.

Auto-Mount through Virtual Box Manager

In case we enabled auto-mounting on creating a shared folder from the Virtual Box Manager those shared folders will automatically be mounted in the guest with mount point /media/sf_<name_of_folder>. To have access to these folders users in the guest need to be a member of the group vboxsf.

sudo usermod -aG vboxsf userName

The guest will need to restart to have the new group added.

Source and further reading: Virtual Box User Manual


Actually there is an easy way to do that:

  1. Install the extension pack for VirtualBox.
  2. Restart your virtual machine
  3. Install Guest Additions in your guest Ubuntu
  • You can mount the ISO which is on /media or press Left Control+D
  1. Reboot

  2. Try to access /media/sf_your_shared_folder_name. If you still don't have access, that means you don't belong to the vboxsf group. Run this command in the virtual machine:

     sudo adduser your_vm_username vboxsf
    
  3. Log out and log in again to apply changes of adduser. If you still can not access the folder, try rebooting.

  4. If you still can not see the shared folder, you have to mount it. You can activate automount for the shared folder in the options of VirtualBox Manager.

  5. Restart again.


First, please make sure you have installed the Guest Additions

  1. Start your VM

  2. Devices > Insert Guest Additions CD image... Insert Guest Additions CD image...

  3. Mount the CD:

    sudo mount /dev/cdrom /media/cdrom
    
  4. Install the necessary packages:

    sudo apt-get install make gcc linux-headers-$(uname -r)
    
  5. Install:

    sudo /media/cdrom/VBoxLinuxAdditions.run
    

Second, add your user to the group 'vboxsf':

~$ echo $USER; 
ahmed
~$ sudo usermod -a -G vboxsf ahmed

Reboot

Know that the label of your shared folder is lpi (for example):

Edit shared folder

Prepend sf_ to the label. Then, you will find your shared folder under /media/sf_lpi

Finally, you can also create a link to your home. For example:

ln -s /media/sf_lpi /home/ahmed/lpi

:)


Add the shared folder to the virtual machine using vBox graphical interface Make sure to select automount and make permanent

Login to the virtual machine using a root account

Check vboxsf group exists

~$ grep vboxsf /etc/group
vboxsf:x:125:

Check user is not already in vboxsf group

~$ id nilo
uid=1000(nilo) gid=1000(nilo) groups=1000(nilo),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmin),124(sambashare)

Add user nilo to vboxsf group

~$ sudo usermod -a -G vboxsf nilo

Check again user groups

~$ id nilo
uid=1000(nilo) gid=1000(nilo) groups=1000(nilo),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmin),124(sambashare),125(vboxsf)

Reboot and login as nilo

Shared folder is now accesible in /media/sf_dropbox (dropbox is the name I gave to the share)