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:
Vagrant automatically syncs files to and from the guest machine. This way you can edit files locally and run them in your virtual development environment. By default, Vagrant shares your project directory (the one containing the Vagrantfile) to the /vagrant directory in your guest machine.
Using SCP. Another way to copy files and folders from host to guest in Vagrant is to use to SCP. Firstly, make sure you have scp installed on your host. If you don't have it, you can install the openssh-clients package if you're using Linux or install Cygwin if you're using Windows.
By default, Vagrant stores its boxes after downloading in the directory defined by VAGRANT_HOME environment variable. As a result, On Mac OS X and Linux: ~/. vagrant.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With