How to mount a VirtualBox shared folder at startup?

To always mount a Virtual Box "shared folder" on booting an Ubuntu guest we have two options. It is up to personal preference which option works best in our setting.

1. Mount with fstab

To mount a shared folder using the vboxsf filesystem provided with Guest Additions we first need to make sure prerequisites are met. Then we may put the following line in our etc/fstab file:

<name_of_share>   /path/to/mountpoint   vboxsf   <options>  0   0

Replace name_of_share and /path/to/mountpoint with your individual setup (the directory for the mountpoint has to be created first). See the manpage for mount <options>. One possibility is to mount with defaults, or to give specific mount options (e.g. rw, suid, exec, auto, users).

On some systems the vboxsf kernel module is not yet loaded at the time fstab is read on boot. It may then help to append the vboxsf kernel module to /etc/modules.

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

2. Mount with Virtual Box "automatic mounting":

In recent releases of Virtual Box we can also automatically mount shared folders on creation:

enter image description here

After a reboot of the guest this shared folder will be mounted to the guest directory /media/<username>/sf_<name_of_share> accessible to all users who had been made member of the group vboxsf.

  1. Edit /etc/rc.local

    sudo -H gedit /etc/rc.local
  2. Before exit 0 type:

    mount.vboxsf windows_share /media/windows_share vboxsf
  3. Save

  4. (Optional) Create a shortcut to the desktop or home folder:

    ln -s /media/windows_share /home/freddy/Desktop

In order to boot without errors like pressing S to skip mount or press M to manually repair you may have to delete your entry in fstab

For newer systemd based systems you need alternative approaches - the simplest being one mentioned in another answer to another question - which basically says that you need to add a special comment option to the /etc/fstab entry:

src     /my_mount/src_host  vboxsf  auto,rw,comment=systemd.automount 0 0

However for the above to work on some systems you need to check the 'Auto-mount' box in VirtualBox's Shared Folders->Add dialogue, which means you can end up with a few duplicate mounts of the directory.

For a cleaner mount - without duplicate directories nor the need for 'Auto-mount' - you need to use systemd's mount and automount directives. To do so create two entries in /usr/lib/systemd/system/ named after your desired mount point e.g. to match the fstab mount point above they would be named my_mount-src_host.mount and contain:

Description=VirtualBox shared "src" folder


and my_mount-src_host.automount:

Description=Auto mount shared "src" folder



Then they need enabling:

sudo systemctl enable  my_mount-src_host.automount
sudo systemctl enable  my_mount-src_host.mount

They will now mount on boot. If you want mount them immediately (provided the Shared Folders have been created) you can do so:

sudo systemctl start  my_mount-src_host.mount

Note if you have directories with odd names or dashes (-) in them then use systemd-escape to find the appropriately escaped name.