42

It is my first time using virtualbox and ubuntu (14.04), I am on a host Windows 7 OS.

I am trying to mount a shared folder that has files I need to access both in the virtualbox and on the windows OS. I have successfully mounted them using the vboxsf from the Guest Additions installed.

To mount I used the command sudo mount -t vboxsf <dir name in vbox> <directory in linux for example I used sudo mount -t vboxsf Test /home/user/Test

I found several ways of mounting the directories automatically upon startup using for example the /etc/rc.local method (here) where you modify said file appending the command to it (without sudo). Or by using the fstab method (here). I prefer the rc.local method personally.

Once mounted it has permissions dr-xr-xr-x however once mounted the directory is of root ownership and chown user /home/user/Test has no effect. This means I cannot make or change files in it as a normal user.

In the VirtualBox the directory to be shared is not set as read-only.

Is there a way to automatically mount the shared folder and assign ownership to my non root user?

Fiztban
  • 829
  • Excellent thread I found on the forum https://forums.virtualbox.org/viewtopic.php?f=3&t=79965 . I recommend anyone reading this post to read it. – www139 May 14 '18 at 17:43

3 Answers3

65

If you have the guest additions installed, use the VirtualBox menu:

Devices > Shared Folders...

Add the path, name and enable "Auto-mount" and "Make permanent" options.

Finally add your user to the group with:

sudo usermod -G vboxsf -a myusername

Logout and back again or reboot the machine to complete the process (thanks @Fo).

Groups are stored in /etc/group, according to the usermod man page.

Eliah Kagan
  • 119,820
Katu
  • 3,663
  • I tried this and was unable to access the shared folders even after confirming I had been added to the group. – Jake Feb 12 '16 at 06:57
  • @Jake Are your guest additions correctly installed? – Katu Feb 12 '16 at 09:48
  • 4
    need to log out and back after adding yourself to the group – Nelfo Jun 20 '16 at 23:03
  • I'm having issues on this too, it urks me to see root vboxsf in the symlinked files I created in my home dir for the mounted share, but the main problem is i get read problems too. Isn't it plausible that webfiles in my case that are set up for permissions such as 755 will now need 775, etc..? This is a no-go, and I need a solution to mount it to my user if so. The uid and gid in fstab wasnt working for me, maybe since its auto mounted by guest additions i gather, im really not sure why fstab wasn't working. When moving files i see this mv: cannot create symbolic link – Brian Thomas Aug 24 '17 at 18:08
  • @BrianThomas Your issues with symlinks seem different from the original question. You might want to consider creating a new question for them. – Katu Aug 24 '17 at 21:16
  • What are the security implications of doing that? I can imagine changing the mount gives less of an attack vector. – user136036 Mar 28 '21 at 14:57
24

Ah the curse of writing a question and then finding the answer immediately after.

I didn't use the full command suggested in this link

sudo mount -t vboxsf -o uid=$UID,gid=$GID share ~/host

So to add ownership and automatically mount in virtualbox via vboxsf in Ubuntu add to the /etc/rc.local file before the exit 0 line the command as follows:

mount -t vboxsf -o uid=1000,gid=1000 <folder name given in VirtualBox> /home/<user>/where/ever/you/want

Fiztban
  • 829
  • 2
    I might add that shared folders that are shared from NTFS might have some specific corner-case problems. You could work around it with losetup and chain mounting a file as a filesystem. – RobotHumans Jun 10 '14 at 21:35
  • 1
    Furthermore, commands to change ownership on the vboxsf-munted partition will not take effect. See http://superuser.com/questions/640027/why-cant-i-chown-a-virtualbox-shared-folder – Reinier Post Jun 09 '16 at 10:04
  • This solution "fixes" the mounting to a particular user id. Not sure if this is what the OP wanted. If different users are using this system, it will not work for all of them. – Devolus Apr 17 '19 at 10:03
3

Another alternative is to first configure the shared drives in Auto-mount mode like in the first part of @Katu's answer. This will allow to get the mount configurations of the shared drives by running mount. You'll get something like:

$ mount
...
vbox-shared-dir on /media/sf_vbox-shared-dir type vboxsf (rw,nodev,relatime,iocharset=utf8,uid=0,gid=998,dmode=0770,fmode=0770,tag=VBoxAutomounter)...

One can then use this information to update /etc/fstab/ with an additional line, replacing UID and GID accordingly and removing the tag option:

vbox-shared-dir   /media/sf_vbox-shared-dir  vboxsf rw,nodev,relatime,iocharset=utf8,uid=<UID>,gid=<GID>,dmode=0770,fmode=0770 0       0

After this you should be able to mount the volume automatically running the command:

# mount vbox-shared-dir

Then you just need to remove the Auto-mount option in the virtual box configuration because the volume gets mounted automatically during boot.