In VirtualBox, what is the best way to share a directory between an OS X host and Ubuntu guest?
- Host: Mac OS X 10.7.3
- Guest: Ubuntu 12.04
- Guest has a shared directory mounted via VirtualBox settings with Access=Full and Auto-Mount=Yes.
The problem with this setup is illustrated below. In my shared directory, I can't change the permissions at all (not a permissions denied error, but they just don't take effect).
Ubuntu 12.04 (guest):
% ls -l
total 0
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 1
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 10
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 2
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 3
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 4
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 5
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 6
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 7
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 8
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 9
Mac OS X 10.7.3 (host):
$ ls -l
total 0
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 1
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 10
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 2
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 3
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 4
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 5
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 6
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 7
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 8
-rw-r--r-- 1 <my-mac-user> staff 0 Apr 17 21:56 9
If I chmod on the guest, nothing changes:
% chmod +x 1 | ls -l 1 # guest
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 1
If I chmod on the host, it changes on the host but not on the guest:
$ chmod +x 1 | ls -l 1 # host
-rwxrwx--x 1 <my-mac-user> staff 0 Apr 17 21:56 1
% ls -l 1 # guest
-rwxrwx--- 1 root vboxsf 0 Apr 17 21:56 1
I've figured it out!
To reproduce:
From a terminal, run
umask && id
as the normal user to get something like this:sudo mkdir -p /media/sf_src # src is the shared directory
To mount the
src
directory as a test:To automatically mount it on login, edit
/etc/fstab
and add the following:The problem -- permission issues in vbox/ubuntu accessing a OSX shared drive
The goal is a simple way to to share directories between Mac and Ubuntu environments. Unfortunately the examples that I have seen so far seem to be a bit more complex than they actual need to be, and don't really explain what the real problem that needs to be solved. I'll attempt to to handle both of those issues here.
The environment here is a Mac running OSX 10.9.5, with Virtual Box 4.3.16 executing Ubuntu-14.04.1 with Guest extensions installed. September 2014 stuff.
I think that the entire problem here is that the uid of the directories on the Mac, and in Ubuntu must match — The default gid’s assigned for user and groups are different between OSX and Ubuntu, and that’s where the trouble lies.
To access a file, one must either own it, or be a member of the group that owns it. And as access is actually based on the id number of the group, not the group name, all that is necessary is to create a common group number on both sides, that the users belong to.
That’s exactly what the solution below does. Don’t be mislead by the length of what’s written, it’s actually very simple. There are just lots and lots of examples of what’s going on.
I’m going to be flipping between OSX and VBOX consoles here (mac and virtual-box/ubuntu) within this doc — make sure you understand which window your in.
Final note: The solution shown below is based on establishing a common group id between the OSX and Ubuntu environments, so that the file permissions work. There may be other, more modern solutions. This one is really simple and understandable, and runs on bare bones basic unadorned installations.
OSX: —————
Note that is was done on a fresh out of the box 10.9.5 Mac, with nothing on it, not connected to a corporate network, nothing fancy running on it other than stock software. This is as simple as it gets.
When I did the default install on the Mac, joe_public is the admin user, and his uid was set to 501.
Notice that the uid is 501 — this is the default first account id on OSX — nothing special
I created a few directories that I want to share on the Mac side — Note that I did not put them under my user directory for backup reasons.
VBOX: ——————
Simple default virtual box and ubuntu installation - again, joe_public is the default admin created when I installed ubuntu.
Please note once again, that the name space between the OSX and Ubuntu is completely different. There is absolutely no relationship between the two names here.
Created three mount points, using Virtual Box's Setting -> Shared Folders gui.
NOTE: I actually had to reboot my session for all these mount points to come up.
Notice that the gid for these is 999 — this is the vboxsf group.
This was assigned automagicly by Virtual Box version 4.3.16 for us. The vbox documentation shows ways how to change this if you mount the path manually through the command line, but who’s going to remember that — Simply take the defaults that the GUI forces on us..
But it doesn't work (expected at this point -- that's what we are trying to solve)
Note that at this point joe_public is not a member of that vboxsf group — and that’s going to be a problem until we fix it. FYI: These are the default groups assigned to the account when it is created.
So what we have at this point (we haven’t done anything yet to fix it)
We don’t want to change the gid of the user joe_public on either side, as that’s a pain in the ass on already installed systems, and doesn’t solve this for other users. The simplest solution is to make a matching group id — vboxsf — on the mac side, and make sure that joe_public is a member of it on both sides.
So, still on vbox/ubuntu, make joe_public a member of the 999 vboxsf group
I think I logged out of my account and back in again here after I did this.
OSX: —————
Now we need to create a vboxsf group on the mac. I doubt that the name actually makes a difference here — it’s the 999 group id that’s important. Please remember that the directory system name spaces (as well as user names) are different between the host and the VM operating systems. But just to make life sane, we all call it vboxsf on the mac. Same reasoning why joe_public is used a user name on both sides.
OSX doesn’t have a simple add group command like linux does — so use the dscl command to do this in multiple steps. Please see the mac os documentation for more details on this. Notice that we create the vboxsf group, and add joe_public to that group here.
So, at this point, we should have
The proof here is if it works — so that’s the next step
VBOX: ——————
cd into our directory and touch a file
Check that we created a file successfully.
OSX: —————
VBOX: ——————
It all appears to be working..
VBOX: —————— FINAL VERIFICATION
What we are checking out here is that this whole thing depend upon the user joe_public being a member of the vboxsf group — and the simplest way is simply removing joe_public from the group
Removing user joe_public from group vboxsf
Seeing if we can access it our directory -- and we can't, and this proves that is a group permission problem
Add user back into vboxsf
It works again !
VBOX: -- ONE MORE PROBLEM -- symbolic links in vbox -------
If you go into /media/sf_shared, you will find that symbolic links in shared directories simply don't work. This is a really big problem if you are trying to set up a full linux development environment on a shared drive.
By default, symbolic links aren't supported on virtual box shares. See below for explanations. Basically as I understand it, symbolic links are a security hole that were "fixed" in Virtual Box by disabling support for them in the 4.1.8 time frame (2011). I'm running 4.3.16 here...
https://www.virtualbox.org/ticket/10085
http://ahtik.com/blog/fixing-your-virtualbox-shared-folder-symlink-error/
Fortunately there is a back door to re-enable it, via the Host's VBoxManage command. Like always, please understand the security holes here that you may be opening. I'm on a stand-alone development machine, so this doesn't appear to be a problem.
OSX: ------------
Note: Ubuntu is my vm's name, and shared is the shared directory name.
You can get the vm name like this:
And the shared folders name, either via the Virtual Box gui, or
I rebooted the entire virtual box system here, I didn't figure out the minimum requirement for it to take.
Anyway, to test this, go back to you vbox window
VBOX: ---------
No error -- and to verify
OSX: ----------
And back on the mac side -- just to prove it all works
Please note, I have only tested this on a OSX host, and Ubuntu virtual box client. The references that I listed above seemed to indicate that there might be an issue running a Windows based host.
EXERCISE LEFT FOR THE STUDENT ———————
The benefit of the method listed above is that it can run on a stand-along machine, with no network access. But if you think about this, this name-verses-id issue has got to be a common problem between any heterogeneous computing environments.
What other solutions are available where solutions to that problem are available? — Things like Active Directory (a Microsoft product) and the like might be able to solve this. It would be interesting to obtain a collection of those solutions and compare there various features and tradeoffs.
Ever since VirtualBox v4.0, it's easy to solve these permissions issues! You don't need to worry about
mkdir
,mount
,chmod
,umask
, etc. To access your auto-mounted shared folders (which appear in/media
withsf_
prefixes by default), there's only one thing you need to do: Add your username to thevboxsf
group viasudo usermod -a -G vboxsf [username]
.For convenience, you may also want to create symbolic links to those shared folders within your
home
folder (e.g.,ln -s /media/sf_Stuff ~/Stuff
).Source: http://www.virtualbox.org/manual/ch04.html#sf_mount_auto
After you create a shared folder in Virtualbox settings, Ubuntu will automatically mount it for you next time you boot up the system (you will see an icon on your desktop).
But if you are not using root account, you will not have permission to access it. What you need to do is to add your user account to a user group called 'vboxsf':
Then logout and login again. You can access the shared folder now.
For me all I needed to do was:
That was enough for me to get access to the shared folders. The folder has a group of vboxsf, user only needs to be assigned to that group to get access. If you need to set more strict access or more controllable access, you might need to go through the steps of mounting it manually with right userid-s.
For me the fstab solution didn't work and caused the machine not to boot correctly.
I found this, I tested and it worked in Ubuntu and Xubuntu, just the automount feature didn't worked for me
I. Install Guest Additions
Firstly, it is necessary to install the VirtualBox’s Guest Additions to the host system:
Choose in the VitualBox’s menu: Devices -> Install Guest Additions…
It mounts a new ISO for the host system. A new CD appears in Ubuntu (or mount it). Run as root the VBoxLinuxAdditions-x86.run installation script (because it is an Intel based Mac) in a Terminal window:
Probably you will be asked to restart Ubuntu.
Installing the Guest Additions has many advantages such as adopting screen resolution (your window size is the system resolution, i.e. no scrollbars), easy mouse mode (no need to press the left command button to release the mouse cursor) and, what we are looking for, shared folders.
II. Create Shared Folder
The goal is to access a shared folder, what is an easier way than to use sshd or Samba.
Choose in the VitualBox’s menu: Devices -> Shared Folders...
III. Mount Shared Folder
Mount the created shared folder into a folder with permissions for your user. Let’s open a Terminal window on Ubuntu and:
Create a folder where will be the shared folder mounted. (Skip this step if have already a folder for this.)
e.g. (in /tmp)
Get your user uid:
The result will be something like:
so uid of my user is 1000. Mount the shared folder SHARED_FOLDER_NAME into folder MOUNTED_FOLDER with ownership for user with uid USER_ID as root:
e.g.
Specifying the uid parameter enables writing to the shared folder for your user. The mounted folder MOUNTED_FOLDER (e.g. /tmp/share) contains files from the shared folder on your Mac harddrive. It is possible to write to the share folder as well. Optionally, a shared folder can be automatically mounted on the virtual machine start. Add the following line to the /etc/fstab file (as root, e.g. sudo vim /etc/fstab):
e.g.
where the uid and gid values can be find in the output of the id command (see the step 2 for detailed example). The shared folder will be available also after a restart of the virtual machine.
Source: http://www.valibuk.net/2009/03/accessing-mac-os-x-harddrive-from-ubuntu-in-virtualbox-via-shared-folders/
Host OS: macOS
VM (guest) OS: Ubuntu Server 16.04.5 LTS
VirtualBox v5.2.18
Both host OS and guest OS must have same user (further in the text: username).
Stage 1: Install VirtualBox Guest Additions:
1.1. Locate the VirtualBox Guest Additions,
1.2. Start the VM
1.3. Click the CD icon in the bottom right task bar
1.4. Select "Choose disk image..."" and search for the VBoxGuestAdditions.iso
1.5. In the guest terminal type (you can also do this from the host terminal if you SSH into it):
Stage 2: Shared Folders Setup:
2.1. Create rules in VM:
Settings > Shared Folders
Add new port forwarding rule
green button in the top right of the window.Auto-mount
andMake Permanent
options2.2. To mount shared folder on
/opt
you must createshared_folder_dir
subfolder and set appropriate permissions to it:2.3. Add username to the vboxsf group:
2.4. Reboot VM to apply changes:
Stage 3: Auto mounting
host_folder
into/opt/shared_folder_dir
:3.1. Change VM's /etc/rc.local:
and place following right above
exit 0
:3.2. Reboot VM to apply changes:
See also
From https://askubuntu.com/a/171357/476506
Above command will give access to shared folder for "user".