140

I am using Ubuntu on Virtual Box and I have a folder which is shared between the host (Windows) and the VM (Ubuntu). When I open any file in the share folder in Ubuntu, I can not change it as its owner is set to root.

How can I change the ownership to myself?

Here is the output of ls -l :

-rwxrwxrwx 1 root root 0 2012-10-05 19:17 BuildNotes.txt

The output of df is:

m@m-Linux:~/Desktop/vbox_shared$ df
Filesystem   1K-blocks      Used Available Use% Mounted on
/dev/sda1     29640780  10209652  17925440  37% /
none            509032       260    508772   1% /dev
none            513252       168    513084   1% /dev/shm
none            513252        88    513164   1% /var/run
none            513252         0    513252   0% /var/lock
none            513252         0    513252   0% /lib/init/rw
Ubuntu       214153212  31893804 182259408  15% /media/sf_Ubuntu
/dev/sr0         53914     53914         0 100% /media/VBOXADDITIONS_4.2.0_80737
Ubuntu       214153212  31893804 182259408  15% /home/m/Desktop/vbox_shared

The options in VM is automount and the readoly is not checked.

Tried to use /media/sf_Ubuntu, but getting permission error:

m@m-Linux:/media$ ls -l 
total 10
drwxrwx--- 1 root vboxsf 4096 2012-10-23 15:35 sf_Ubuntu
drwxrwx--- 2 root vboxsf 4096 2012-10-21 23:41 sf_vbox_shared
dr-xr-xr-x 6 m    m      2048 2012-09-13 07:19 VBOXADDITIONS_4.2.0_80737
m@m-Linux:/media$ cd sf_Ubuntu/
bash: cd: sf_Ubuntu/: Permission denied
m@m-Linux:/media$ cd sf_vbox_shared/
bash: cd: sf_vbox_shared/: Permission denied

Please note that I am in the group vboxsf:

m@m-Linux:~$ id
uid=1000(m) gid=1000(m) groups=4(adm),20(dialout),24(cdrom),46(plugdev),105(lpadmin),119(admin),122(sambashare),1000(m),1001(vboxsf)
user654019
  • 2,277
  • 4
  • 21
  • 20
  • How did you mount the shared folder? Are you letting it be automounted by the VirtualBox guest utils (i.e. /media/sf_something) or did you mount it manually using the mount command? – EightBitTony Oct 23 '12 at 14:45
  • Also, can you describe how you have configured the share under VirtualBox in Windows? There are three options for the share, read only, automount and permanent, which options have you ticked. – EightBitTony Oct 23 '12 at 14:48
  • Please post updates to your question - not as comments. Please include the output of `df` and the command you used to manually mount the filesystem. – EightBitTony Oct 23 '12 at 14:53
  • Thank you - you will see there is a /media/sf_Ubuntu filesystem. That's your shared folder, you don't need to manually mount it. I suggest you umount the /home/m/Desktop thing, and just use /media/sf_Ubuntu which works as per my answer below. – EightBitTony Oct 23 '12 at 14:57
  • @Tony: I can not access this folder. Please see my original question which I add more information – user654019 Oct 23 '12 at 15:26
  • Add your username to the vboxsf group, as per my answer. If you have added your user, then you need to log off and log on again to have it take effect. – EightBitTony Oct 23 '12 at 19:50
  • Have you installed guest additions? For the record, ownership while auto mount is on is set as user root, group vboxsf. I was able to manually mount my folder as squid (vm proxy project). As far as "normal" usage goes this works. –  Nov 08 '12 at 13:47

10 Answers10

205

The regular way of getting access to the files now, is to allow VirtualBox to automount the shared folder (which will make it show up under /media/sf_directory_name) and then to add your regular Ubuntu user to the vboxsf group (as root #).

# usermod -aG vboxsf <youruser>

By default, without manual action, the mounts look like this,

drwxrwx--- 1 root vboxsf 40960 Oct 23 10:42 sf_<name>

so the vboxsf group has full access. By adding your user to that group, you gain full access. So you wouldn't worry about changing their permissions (which don't make sense on the Windows host), you just give yourself access.

In this specific case, this is the automounted Shared Folder,

Ubuntu               214153212  31893804 182259408  15% /media/sf_Ubuntu

and it is that directory that should be used to access to the Shared Folder, by putting the local user into the vboxsf group. If you want a 'better' link under your user's home directory, you could always create a symbolic link.

ln -s /media/sf_Ubuntu /home/m/Desktop/vbox_shared

You will need to reboot your VM for these changes to take effect

If you manually mount the shared folder, then you need to use the relevant options on the mount command to set the folder with the right ownership (i.e. the gid, uid and umask options to mount). This is because the Host OS doesn't support the same permission system as Linux, so VirtualBox has no way of knowing who should own the files.

However, I strongly recommend just configuring the shared folder to be auto-mounted (it's a setting on the Shared Folder configuration in VirtualBox itself).


For the avoidance of doubt, I do not believe you can change permissions normally anyway, on that filesystem if it's mounted in the regular way,
tony@jabba:/media/sf_name$ ls -l tst.txt
-rwxrwx--- 1 root vboxsf 2283 Apr  4  2012 tst.txt
tony@jabba:/media/sf_name$ sudo chown tony tst.txt
[sudo] password for tony: 
tony@jabba:/media/sf_name$ ls -l tst.txt
-rwxrwx--- 1 root vboxsf 2283 Apr  4  2012 tst.txt
tony@jabba:/media/sf_name$ 
EightBitTony
  • 20,963
  • 4
  • 61
  • 62
  • 2
    When I tried this, I got the message that user is lready in group, but when I am trying to change a file on share folder, I am getting error when I want to save it. The error is: Could not save the file /home/m/Desktop/vbox_shared/test.tst Unexpected error: error renaming temporary file: Text file busy. – user654019 Oct 23 '12 at 14:40
  • That's a different problem, potentially not due to permissions. Can you create a new file, or modify a different file. Hang on - /home/m/Desktop/vbox_shared? Have you manually mounted the shared folder? That's not the default path. – EightBitTony Oct 23 '12 at 14:41
  • Checking system, I found that I only have permission to create or delete files and not read/write permision. How can I give it to myself? – user654019 Oct 23 '12 at 14:43
  • Please add the output from `ls -l` of the relevant files to your question. – EightBitTony Oct 23 '12 at 14:44
  • @Tony:yes I manually mount it. How can I automount it?I checked the automount in VM setting (also giving full access) – user654019 Oct 23 '12 at 14:47
  • Then it's probably already auto-mounted. What does `df` show you (paste it into your *question* not as another comment, please) – EightBitTony Oct 23 '12 at 14:48
  • @Tony: Here is the output of l -l : -rwxrwxrwx 1 root root 0 2012-10-05 19:17 BuildNotes.txt – user654019 Oct 23 '12 at 14:51
  • ["The best thing to do is to add your normal user account to the vboxsf group. You can do that by either directly editing the /etc/group file and adding your account at the end of the group definition like so (replacing goyuix with your user account): `vboxsf:x:1001:goyuix`"](http://superuser.com/questions/231055/ubuntu-on-oracle-virtualbox-shared-folders/236849#236849). – BuZZ-dEE Jul 24 '13 at 06:55
  • [That solved the problem](http://unix.stackexchange.com/questions/52667/file-permission-issues-with-shared-folders-under-virtual-box-ubuntu-guest-wind#comment125073_52670), because [I got the message that I already in the group.](http://unix.stackexchange.com/questions/52667/file-permission-issues-with-shared-folders-under-virtual-box-ubuntu-guest-wind#comment73710_52670) if I use `useradd -G vboxsf `. – BuZZ-dEE Jul 24 '13 at 06:56
  • Hi Tony, I have a similar yet not identical [file permissions problem here](http://unix.stackexchange.com/questions/164588/in-a-virtualbox-vm-how-do-i-set-the-filesystem-permissions), I was wondering whether you could help. basically I can access the files all correctly but trying to change a child files permissions is ignored. Any advice/ideas? (I am in the vboxsf group) – Mike H-R Oct 28 '14 at 11:56
  • You can not change the permissions. See the end of the answer above. – EightBitTony Oct 28 '14 at 17:08
  • the inability to maintain the same permissions as the host file system, is not that a problem? especially if the shared folder is tracked in git, then they are recognised as new changes in the vm? also, i would not want 755 on text files. `diff --git a/a.txt b/a.txt old mode 100644 new mode 100755` – rag Nov 20 '14 at 08:46
  • This is the setup for a Windows host. Since the Windows host doesn't understand UNIX permissions, I'm not sure why anyone expects them to line up. It's likely different with UNIX hosts. – EightBitTony Nov 20 '14 at 10:39
  • 11
    Seems reboot is needed to make it take effect. – Eric Oct 30 '15 at 17:17
  • For me it was enough to logout & login, as suggested here: http://stackoverflow.com/questions/26740113/virtualbox-shared-folder-permissions – Serge Rogatch Aug 17 '16 at 18:20
  • 7
    YOU DESERVE A BEER FOR THIS ANSWER!!! I couldn't get this to work at all until I came across this. Can't believe this isn't out there more. I wish I could upvote it more than once!! – Tensigh Oct 19 '16 at 04:51
  • I had a similar issue and the following helped: https://drupal.stackexchange.com/questions/226156/can-i-change-the-location-of-the-cached-twig-templates#comment331475_226167 – therobyouknow Sep 22 '18 at 12:49
  • 1
    I've created an account on Stack Exchange just to say thank you, saved my day – Lucas Coelho Apr 13 '20 at 14:31
  • @SergeRogatch And if you don't want to logout, `newgrp vboxsf` seems to work. – gmatht Jun 13 '20 at 05:47
  • @EightBitTony my host machine is a mac (catalina) and my guest is ubuntu 21.04. VirtualBox version 6.1.12. When I use auto mount - somehow the shared folder is mounted under the `root` group and not `vboxsf`. One note, the shared folder is an external drive on the host machine, and is on NTFS. Is there anything can be done? – Genry May 17 '21 at 12:50
6

Enable full read,write access to a shared folder on a Windows host from an Ubuntu guest for a non-root Linux user

This step will enable you to access a certain directory on your Windows host machine from the Linux guest terminal. In this example the name of the share from the OVB perspective will be vshare ( which is the default ) , the full directory path to the Windows OS ( the host OS ) will be "C:\var\", and the full file path to access it from the guest vm will be "/vagrant". Finally, the name of the user to enable the full read/write access will be "you".

 # how-to add a shared folder on the host
 VBoxManage sharedfolder add "host-name" -name "vshare" -hostpath "C:\var" -automount

1. Install the Guest Additions prerequisites

Install the Guest Additions prerequisites by issuing the following command:

 sudo apt-get install -y build-essential make gcc  linux-headers-$(uname -r) 
 linux-headers-generic make linux-source  linux-generic linux-signed-generic

2. Install the Guest Additions

Do not use the .iso file to download and run the installer from there - it will simply not work !!!

sudo apt-get install virtualbox-guest-dkms 

3. Automount the Share on Boot

Configure the share directory to be automounted on vm boot by adding the following lines to the end of your /etc/fstab file ( note the "vshare" conf , and the uid=10001 ):

/media/sf_vshare /vagrant vboxsf bind,uid=10001,rw,umask=0000 0 0
# eof file: /etc/fstab 

4. Add yourself to the vboxsf group

# mount
sudo mount -a

sudo usermod -G vboxsf -a you 

5. Reboot and verify

Reboot the vm and login via ssh to verify the file sharing.

# ssh to the vm
ssh you@host-name

# check as yourself that you have access
find /vagrant
Yordan Georgiev
  • 269
  • 2
  • 4
3

I just have had the same problem with my Win-Ubuntu shared folder, and the way I solved it was adding my user into the vboxsf group, editing the /etc/group file. Hope this works for you. Down here you can find a short scheme of how I edited the file

(edit /etc/group)
...
...
...
pulse:x:119:
pulse-access:x:120:
utempter:x:121:
rtkit:x:122:
saned:x:123:
openquake:x:1000:
sambashare:x:124:openquake,luisa
vboxsf:x:999:openquake,luisa   ##This is the line I add my user
postgres:x:125:
rabbitmq:x:126:
celery:x:500:
luisa:x:1001:

Luisa.

  • 2
    You should never edit /etc/group directly, use the appropriate tools for this job (in this case `usermod -aG `). – raimue Nov 05 '15 at 00:18
  • The main, if not only, reason why this is recommended is because if you accidentally make a mistake manually updating a file like /etc/group, it can invalidate the DB and essentially lock you out. – Spencer Williams Nov 12 '15 at 20:07
2

I had the same problem and I solved installing the latest version of VirtualBox Guest Additions; in my case was upgrading from 4.3.8 to 4.3.10.

When installation was finished, the /media/sf_name folder had no more "root" as group owner, but "vboxsf" as correct.

Stefano
  • 121
  • 3
1

If you have the luxury of controlling the options to the mount command (for instance if mounting manually or using /etc/fstab) you can use the -o uid=771 option to the mount commmand.

An entry in /etc/fstab would look like

scanresults    /data/scanresults    vboxsf    uid=771    0 0
Mutant Bob
  • 129
  • 6
1

Have you tried sudo?

$sudo chown username filename
Extn3389
  • 27
  • 2
0

Virtual box additions have to be installed again after you upgrade your guest system. The problem, I think, is when you upgrading some components in ubuntu, some components changed, after installing vb-additions, sf_forlder_name will be accessible. My problem solved by this way. Pisu's solution works.

nicky
  • 11
  • 2
  • In my experience virtual box guest additions only have to be upgraded when the host VirtualBox system is upgraded. Not when any software on the client changes. What software on the client did you upgrade to make this necessary? – Anthon May 18 '14 at 07:13
  • 1
    No, nicky is right. You have to re-install the Guest Additions if you update your guest OS kernel. – Spencer Williams Nov 12 '15 at 20:08
0

In my case it did not resolve to add myself to the vboxsf directory. I finally find out that the path to the auto-mounted directory was the problem :

/mnt/host/sf_win

Only /mnt was existing on the guest. But the mount did actually work, and with the permission for the group. But no operation was allowed without sudoing.

Modifying the mount point to /mnt/sf_win resolved my issue.

0

As discussed in this post, the problem with vboxsf automounts like /media/sf_Ubuntu is that

You will find permission issues using the automount feature as they are handled differently in removable media by the OS.

and solution is to

Remove the automount and use a standard mount instead with the options flag and set you required permissions there.

That is, you can mount the shared folder to a location (e.g. /Ubuntu) while using appropriate options to set the uid and gid of the mount to your unix user (from your id results).

sudo mount -t vboxsf -o rw,uid=1000,gid=1000 Ubuntu /Ubuntu

, or equivalently, you can add an entry as follows in /etc/fstab:

Ubuntu    /Ubuntu    vboxsf  rw,uid=1000,gid=1000   0 0

This way, the mounted folder will be under your Ubuntu user name/group.

tinlyx
  • 536
  • 1
  • 5
  • 22
-2
  • Unmount shares as root
  • Make required changes with chown and chmod
  • reboot and checkout the shares are with proper rights
polemon
  • 11,133
  • 11
  • 69
  • 111
andrey
  • 1