I have Ubuntu installed in Virtualbox. I want to mount my VirtualBox shared folder in Ubuntu automatically when I log in Ubuntu. I put the following line in my ~./bashrc and ~/.bash_profile:

sudo mount -t vboxsf windows_share /media/windows_share

where windows_share is the name I created with Virtualbox. But everytime I start my Ubuntu, it asks me for passwd since it needs sudo. Is there anyway to automatically mount Windows share without entering password every time I log in?

up vote 91 down vote accepted

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
    How do you change the mount directory and mount prefix? Do you run some terminal commands in the HOST OS or the GUEST OS? – CMCDragonkai Jan 18 '14 at 12:12
  • 11
    This feature requires that the "virtualbox-guest-utils" service has started, and on Ubuntu 14.04 this service starts later than the filesystems are mounted. This is the reason for "device not found" error when mounting the shares through fstab – kolypto May 13 '14 at 14:58
  • 1
    @garromark, my only idea is to create an upstart script, which is quite simple. Another option is to put the same fstab entry, but add "noauto" so you just mount it manually. – kolypto May 19 '14 at 21:56
  • 4
    @kolypto, thanks for getting back to me. I actually found two competing solutions, only one of which worked for me: Option 1) (Worked for me), is, as you said, to put noauto in the fstab options and then to later mount typically in a startup script (such as .profile), Option 2) the main issue being that vboxsf isn't loaded prior to fstab running, append vboxsf to the file /etc/modules, asking the kernal to load the module prior to fstab being run. Maybe this will help someone else. – garromark May 20 '14 at 22:11
  • 4
    please remember to add your user to the vboxsf group. You may refer to askubuntu.com/questions/79565/add-user-to-existing-group – chinloong Dec 25 '15 at 4:01
  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

  • 4
    This is the ONLY way I have found to work, tried RC.Local with the regular mount command, tried FSTAB, tried Crontab w/ script. Huge Thanks!! – FreeSoftwareServers Sep 5 '15 at 9:17
  • 2
    This worked for me too (opposed to the accepted answer, not quite sure why). So a big thank you from me! :) – Daniel Lisik Sep 26 '15 at 0:46
  • 4
    Worked for me. I edited /etc/rc.local with this mount -t vboxsf [-o OPTIONS] sharename mountpoint. – neurite Oct 26 '15 at 18:56
  • 1
    For the sake of arguments and to reflect virtualbox case: i used to do this upon start sudo mount -t vboxsf -o uid=$UID,gid=$(id -g) windows_share ~/shared/mount_point and ended up putting the following in /etc/rc.local to have it work: mount -t vboxsf windows_share /home/dev/shared/mount_point where dev is my user, FYI /etc/fstab also works! – MediaVince Dec 31 '16 at 16:06
  • 1
    Your answer has worked for me, but the directories get mounted as root. I changed the mount command in rc.local script to include my user id (2000): mount.vboxsf -o rw,uid=1000 /home/mwittie/Dropbox Dropbox vboxsf. PS If anyone is looking for a tutorial on how to turn on rc.local on Ubuntu 17.04 this has worked for me. PPS I did not need to include vboxsf in /etc/modules. – Mike Wittie Jul 24 '17 at 23:50

After an exhausting morning trying all the above in Ubutntu 16.04 running in Virtualbox 5.0.20 unsuccessfully (particularly disappointed that the rc.local solution didn't work), it worked by:

  1. Registering from Shared Folders menu of Virtualbox GUI the required directory but NOT automounting it or permanent mounting from Virtualbox. Otherwise the host dir is mounted by root and it's a pain to access by non-root users even from the admin group.

  2. adding simple entry in fstab:

    [VirtuablBoxNameOfMount] /media/[guestOSuser]/[mountSubdir]    vboxsf   rw, noauto   0     1
    

    Note noauto option - otherwise boot loader fails as has been noted.

  3. Add corresponding line to /etc/sudoers as follows by using command visudo from within guest OS:

    ALL ALL = NOPASSWD: /bin/mount /media/[guestOSuser]/[mountSubdir]/
    

This will allow non-root processes to specifically mount this (as fstab can't mount with 'user' option ...)

  1. Add corresponding line to .profile of user:

    sudo mount /media/[guestOSuser]/[mountSubdir]/
    

Now the selected host subdir is ready-mounted for the selected user upon login!

  • Did you ever find a workaround for noauto? – Jörgen Sigvardsson Nov 29 '16 at 7:42
  • This is the only solution that worked for me. But in addition, I also had to add "vboxsf" to /etc/modules to make sure that .profile didn't run the mount commands before the vboxsf is ready. – huyz Dec 10 '16 at 16:24
  • instead of using sudo, add 'user' to the fstab option and any user can mount the filesystem – Lars Nordin May 23 '17 at 12:22
  • see pclosmag.com/html/issues/200709/page07.html for info on updating fstab, the comments here were only partially helpful. The fourth column in fstab is a comma sep list of options add user (uid=xxx) to that list e.g. noauto,uid=1000,gid=1000 – qodeninja Jul 3 '17 at 22:58
  • There is a blank between rw, noauto which results in a parse error. without the blank the sample worked for me – weberjn Dec 17 '17 at 12:27

I tried the rc.local solution but couldn't get it to work.
However I discovered the problem seems to be related to the folder you run the command from (no idea why). So I added a line to change the directory to my home folder before the mount command, and now it works.

So, my windows share is called Dropbox, my mountpoint is /home/jamie/Dropbox, my user name is jamie, this is what I put in rc.local:

cd /home/jamie
mount.vboxsf /home/jamie/Dropbox Dropbox vboxsf
exit 0
  • Brilliant! thanks. After struggling with the other solutions this worked first time! – Norbert Norbertson Mar 23 '17 at 10:46
  • I had to add a sleep 2 before mounting the device. – Adriano P Mar 20 at 18:16

I do it of a very similar mode at was proposed above but these script create the required and mount or unmount the shared folder with the following script:

#!/bin/bash
#
# Mount automatically even shared folder on startup and unmount it at shutdown.
#
# VirtualBox (c) 2015 by Oracle Systems Inc.
#
####

# Check user privileges.
if [[ $EUID -ne 0 ]]; then
    echo -e "This script must run at ROOT user!" \
        "\nPlease, use 'sudo', 'visudo' or any other to run it."
    exit 1
fi

# Check paramas from caller.
if [[ $# -eq 0 ]]; then
    echo -e "Auto-Mount selected shared folder of VirtualBox machine." \
        "\nUsage:" \
        "\n    VBoxShared <drive_one> <drive_two> <...>"
    exit 2
fi

declare EVENT=          # This set the ACTION: -m OR -u
declare -a DRIVES=()

# Processing each param:
for arg in "$@"; do
    case "$arg" in
        "-m"|"--mount")
            if [[ -z ${EVENT} ]]; then
                EVENT=-m
            else
                exit 318        # parameters at conflict!
            fi
            ;;

        "-u"|"--umount")
            if [[ -z ${EVENT} ]]; then
                EVENT=-u
            else
                exit 318        # parameters at conflict!
            fi
            ;;

        *)
            DRIVES=("${DRIVES[@]}" "${arg}")
            ;;
    esac
done
unset arg

[[ -z ${EVENT} ]] && exit 1             # ERROR: No se ha establecido la acción a realizar.
[[ "${#DRIVES[@]}" -gt 0 ]] || exit 1   # ERROR: No se han indicado las unidades a manejar.

# Process each shared folder stored on '${DRIVES}' array
for drive in "${DRIVES[@]}"; do
    DEST="/media/sf_${drive}"

    case "${EVENT}" in
        "-m")
            [[ -d ${DEST} ]] || (mkdir ${DEST} && chown root:vboxsf ${DEST} && chmod 770 ${DEST})
            mount -t vboxsf ${drive} ${DEST}
            ;;

        "-u")
            if [[ `df --output=target | grep "${DEST}"` > /dev/null ]]; then
                umount -f ${DEST}
                rm -rf "${DEST}"
            fi
            ;;
    esac
    unset DEST
done
unset drive

unset EVENT
unset DRIVES
exit 0

Save it as /opt/.scripts/VBoxShared.sh.

Make sure that this can be runned. On shell type:

sudo chmod a+x /opt/.scripts/VBoxShared.sh

Now, we add a line that run this script on rc.local:

sudo nano /etc/rc.local

and we add these line before last line (exit 0):

. /opt/.scripts/VBoxShared.sh --mount <SharedFolder1> [<SharedFolder2> <SharedFolder3> ...]

Save (CtrlO) and close it (CtrlX)

At this point, we mount automatically all shared folder listed on <SharedFolder> at startup.

For unmount it, we only need type:

sudo nano /etc/rc6.d/K99-vboxsf-umount.sh

#!/bin/bash

. /opt/.scripts/VBoxShared --umount <SharedFolder1> [<SharedFolder2> <SharedFolder3> ...]

exit 0

Save (CtrlO) and close (CtrlX)

sudo chmod a+x /etc/rc6.d/K99-vboxsf-auto.sh

And that's all!

Here is a working solution.

As root (I.E. sudo su) Go to home folder (cd ~) and create a cron file:
vi cronjobs
Add the following
@reboot sleep 15; mount -t vboxsf app /mnt/app

Save file

Note: replace app with your shared folder name and /mnt/app where you want to mount it. In this case I created the folder app under mount (mkdir app) first.

To enable your cron as root (for above filename)
crontab cronjobs

Make sure cron is active:
crontab -l

reboot and it will be mounted. 15 second sleep allows enough time for everything to be ready for the mount.

  • rc.local in the accepted answer was removed in a recent release of Ubuntu. This alternative solution works :) – shonky linux user Jun 15 at 8:19

I recently encountered this thread when, after updating to Ubuntu LTS-18 (and making no changes whatever to VirtualBox, and after reinstalling the extensions and blah-de-blah), auto-mount stopped working. The sf_xxx directories were present in /media/ but none of them were actually mounted.

Attempts to mount them in /etc/fstab (as suggested by VirtualBox's own documentation) didn't work: the boot failed into "emergency mode," even when I had modified the /etc/modules file.

What did eventually work – although I regard it as a stinkin' hack – is the crontab trick described above.

To this day, I have no idea "what broke."

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.