Vagrant: Is it possible to share files and alter permissions inside the guest

nfsvagrantvirtualbox

I have been banging my head against a wall for a week and a half trying to work out how to properly share files between a host and guest using Vagrant and VirtualBox.

What I need to achieve is an auto-provisioning box that downloads our codebase from github. The codebase permissions need to vary from file to file (PHP files, shell scripts, tmp folders, log folders, etc). The codebase files also need to be accessible from the host box for editing.

So far I have tried normal virtualbox sharing, NFS sharing, NFS sharing with bindFS. None of these seem to allow changing the individual file permissions.

This seems to be an absolute showstopper for Vagrant. I honestly do not understand how Vagrant is useful for the purpose of sharing development environments.

Does anyone know how to properly set this up? Is it even possible?

For reference:

  • host OS : Ubuntu 12.04
  • guest OS : Debian 6 (squeeze)
  • vagrant : 1.2.2
  • VirtualBox : 4.2.12

Best Answer

There are several partial answers here, but I'd like to make it much easier for people finding this question, so that they do not have to experiment with various ways of combining those partial answers into one.

This is what I use, and it is working well for me. Since it's difficult or impossible to use italics inside a <code> block, I've put the bits you'll want to change inside <angled brackets>.

config.vm.synced_folder ".", "/var/www/vhosts/<project_name>", 
    id: "project",
    owner: <username>,
    group: <group_name>,
    mount_options: ["dmode=775,fmode=664"]

Or possibly (depending on the version of CIFS/SMB you're using):

    mount_options: ["dir_mode=775,file_mode=664"]

Without the owner and group, Vagrant will use the value of config.ssh.username (when the box was booted) as the owner and group. By using a configuration like the one above, you can control what owner and group are assigned to the shared folder, since logging into the VM and trying to use chgrp and chown does not work.

Without the mount_options, you'll have no way to control what rights are given to the owner and group (and others). Even though this is a VM, and you could argue that it's okay to use 777 rights for these, I don't recommend it since it's a bad habit, and if you're not careful you're liable to carry it over into production, weakening your production security. But obviously you can change the 775 and 664 that I used to whatever you want. If you don't know what those mean, Google "Linux file permissions".

You may (out of habit) insert a space after the comma that separates the mount_options array. Don't. It won't work with a comma.

The id can be anything you want.