Can I modify the ownership for a shared folder in vagrant?

22,667

Solution 1

As @StephenKing suggests you can change the options of the whole directory.

The relevant function is not documented but the source tells us:

# File 'lib/vagrant/config/vm.rb', line 53

def share_folder(name, guestpath, hostpath, opts=nil)
  @shared_folders[name] = {
    :guestpath => guestpath.to_s,
    :hostpath => hostpath.to_s,
    :create => false,
    :owner => nil,
    :group => nil,
    :nfs   => false,
    :transient => false,
    :extra => nil
  }.merge(opts || {})
end 

Basically you can set group, owner and acl for the whole folder which is way better than setting everything to world writable on the host. I have not found any method to change the ownership of a nested directory.

Here is a quickfix:

config.vm.share_folder "v-wordpress", "/var/www/wordpress", "/host/path", :owner => "www-data", :group => "www-data"

Solution 2

@john-syrinek

in 1.2+

config.vm.synced_folder "src/", "/srv/website",
  owner: "root", group: "root"

http://docs.vagrantup.com/v2/synced-folders/basic_usage.html

Solution 3

You can allow changing the ownership inside the guest:

config.vm.share_folder "foo", "/guest/path", "/host/path", {:extra => 'dmode=777,fmode=777'}

Solution 4

As the other answers have pointed out you should probably set the correct owner and group using the owner and group configuration options.

However, sometimes that won't work (for example when the target user is only created later on during provision). In these cases, you can remount the share:

sudo mount -t vboxsf -o uid=`id -u www-data`,gid=`id -g www-data` /path/to/share /path/to/share
Share:
22,667
ayckoster
Author by

ayckoster

Updated on January 24, 2020

Comments

  • ayckoster
    ayckoster over 4 years

    I use vagrant and chef to develop my own blog in a virtual machine. To have easy access to the wordpress folder I created a shared folder.

    Basically the wordpress folder is on my host and gets mounted as shared folder in /var/www/wordpress in the VM. The configuration is similar to:

    config.vm.share_folder "foo", "/guest/path", "/host/path"
    

    My problem is that the ownership in my VM is always vagrant:vagrant even if I change it on my host. Ownership changes in the VM get ignored.

    I cannot use chown to set the ownership of the upload directory to www-data:www-data.

    It is possible to use chmod and change the access restrictions to 777, but this is a really ugly hack.

    Here is what I actually want. Is this possible?:

    • Development: Access to the shared folder from my host.
    • Access Restriction: On the VM all files and folders should have proper and secure ownership and access restrictions.
  • Johntron
    Johntron almost 11 years
    Any idea how to do this with vagrant 1.2+? I believe the relevant file (for Virtualbox) is now plugins/providers/virtualbox/action/share_folders.rb.
  • MatthewLee
    MatthewLee about 10 years
    one thing worth noting is if you want to change permissions on the default synced_folder you need to use the syntax from this answer, stackoverflow.com/a/18390884/704647, which is: config.vm.synced_folder "./", "/vagrant", owner: 'root', group: 'root'
  • Kevin Meredith
    Kevin Meredith almost 10 years
    using Vagrant 1.5.4, this approach didn't work for me. I added the above to my .kitchen.yml, modified my custom_app cookbook to call sudo chown -R kevin /my/path/share. Then I destroyed and provisioned my box again. yet vagrant still owns /my/path/share. Additionally, I tried the above command manually in the VM, yet the owner is still `vagrant.
  • Pierre de LESPINAY
    Pierre de LESPINAY about 9 years
    Multiple shared_folders on common intersecting directories seem to take into account the last definition. So for nested directories, you would simply add the directive (with owner: & group:) after
  • Alwin Kesler
    Alwin Kesler about 8 years
    Using 1.7.4 I need to change this to config.vm.synced_folder ..., {:mount_options => [dmode=777, fmode=777]
  • Kirkland
    Kirkland almost 8 years
    For NFS mounted drives, use config.vm.synced_folder
  • Kirkland
    Kirkland almost 8 years
    To make your vagrant box run a little bit more like a production server would, run dmode=775,fmode=664 instead of 777. Now when you deploy you should run into one less issue, because you're not depending on global write permissions.