106

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?

0

11 Answers 11

130

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.

15
  • 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? Commented Jan 18, 2014 at 12:12
  • 15
    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
    Commented May 13, 2014 at 14:58
  • 2
    @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
    Commented May 19, 2014 at 21:56
  • 5
    @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
    Commented May 20, 2014 at 22:11
  • 7
    please remember to add your user to the vboxsf group. You may refer to askubuntu.com/questions/79565/add-user-to-existing-group
    – chinloong
    Commented Dec 25, 2015 at 4:01
28
  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

8
  • 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!! Commented Sep 5, 2015 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! :) Commented Sep 26, 2015 at 0:46
  • 4
    Worked for me. I edited /etc/rc.local with this mount -t vboxsf [-o OPTIONS] sharename mountpoint.
    – neurite
    Commented Oct 26, 2015 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
    Commented Dec 31, 2016 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. Commented Jul 24, 2017 at 23:50
8

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:

[Unit]
Description=VirtualBox shared "src" folder

[Mount]
What=src
Where=/my_mount/src_host 
Type=vboxsf
Options=defaults,noauto,uid=1000,gid=1000

and my_mount-src_host.automount:

[Unit]
Description=Auto mount shared "src" folder

[Automount]
Where=/my_mount/src_host
DirectoryMode=0775

[Install]
WantedBy=multi-user.target

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.

3
  • 1
    On Ubuntu 18.04, your first solution with comment=systemd.automount option works also without the Automount VBox checked. I struggled for days before finding your solution, Thanks!
    – HubertL
    Commented Feb 2, 2019 at 1:08
  • 1
    Good to hear - I've updated my answer to reflect your finding.
    – Pierz
    Commented Feb 2, 2019 at 10:14
  • 1
    This one worked on Debian 9, the other ones didn't.
    – cslotty
    Commented Oct 10, 2019 at 7:52
6

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!

5
  • Did you ever find a workaround for noauto? Commented Nov 29, 2016 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
    Commented Dec 10, 2016 at 16:24
  • instead of using sudo, add 'user' to the fstab option and any user can mount the filesystem Commented May 23, 2017 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
    Commented Jul 3, 2017 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
    Commented Dec 17, 2017 at 12:27
3

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
2
  • Brilliant! thanks. After struggling with the other solutions this worked first time! Commented Mar 23, 2017 at 10:46
  • I had to add a sleep 2 before mounting the device.
    – Adriano P
    Commented Mar 20, 2018 at 18:16
2

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!

2

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.

1
  • rc.local in the accepted answer was removed in a recent release of Ubuntu. This alternative solution works :) Commented Jun 15, 2018 at 8:19
1

(In my case, my host OS is Mac OS X and my guest OS is ubuntu)

None of the above solutions, and the solutions mentioned here and here worked for me. There was a problem with all of them.

Here is what I finally did to solve the issue:

1- I created a shared folder in VirtualBox UI, pointing to a folder named VMShares in my Mac OS, naming it wd

2- Then I installed Ubuntu Guest Addition tools (restart required)

3- Then I made a folder in my guest OS as the mount point (in my case the name was /home/fashid/host)

4- Then I ran:

sudo VBoxControl sharedfolder list

This was the command to be ensured that the share is available to the guest OS, meanwhile you still need to mount it in your guest OS to make it actually available.

It will show something like:

Shared Folder mappings (1):
01 - VMShares

This is the trick! It is showing the actual name you need to put in below command to actually mount it and make it available in your guest OS:

sudo mount -t vboxsf VMShares /home/farshid/myshares

Did you realize the point? I did not use wd anywhere later. In step 3, I needed to pickup the actual (host) folder name instead of the arbitrary name I asigned in GUI dialog box.

Via above steps, my issue was solved.

0

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."

0

I was having an issue where I could see the shared folder, but there were no files in it. So I did a hack similar to what was shown above:

I made sure that my user was in the correct group(s), and that there was an entry in fstab for mounting the share, and that the permissions were set correctly, and that auto-mount was on in VirtualBox settings, but still no files to be seen.
So I opened up the Startup Applications app in the Ubuntu 18.04 GUI and created a task that simply ran "sudo mount -a" right at startup. For whatever reason the shared folder was not being mounted correctly when fstab was automatically parsed at boot, so re-mounting everything seemed to fix the problem. Now I can see the files in the share.

0

I tried all of the solutions here and none worked.

What worked was to install supervisor and run a python script.

install supervisor

apt-get install supervisor

python script (mine was at /home/ubuntu/shared_folders.py)

import subprocess
import os
import time

shared_folder = '/home/ubuntu/shared'

file_count = len(os.listdir(shared_folder))

mnt_command = 'mount -t vboxsf -o rw,uid=1000,gid=1000 shared-folder ' + shared_folder
if file_count == 0:
        # mount
        subprocess.Popen(mnt_command, shell=True)

time.sleep(3600)

create config file for supervisor

nano /etc/supervisor/conf.d/sharedfolders.conf

[program:shared_folders] command=python shared_folders.py directory=/home/ubuntu process_name=%(program_name)s_%(process_num)s numprocs=1 numprocs_start=0 autostart=true autorestart=true startsecs=1 startretries=3 exitcodes=0,2 stopsignal=TERM stopwaitsecs=10 ;user=pavelp redirect_stderr=true stdout_logfile=/var/log/supervisor/qlistener-stdout.log stdout_logfile_maxbytes=50MB stdout_logfile_backups=10 stdout_capture_maxbytes=0 stdout_events_enabled=false stderr_logfile=/var/log/supervisor/qlistener-stderr.log stderr_logfile_maxbytes=50MB stderr_logfile_backups=10 stderr_capture_maxbytes=0 stderr_events_enabled=false environment=APPLICATION_ENV=development serverurl=AUTO

open supervisorctl

sudo supervisorctl

read configuration

reread

add configuration

add shared_folders

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .