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

懵懂的女人 提交于 2019-11-27 19:00:07
ayckoster

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"

@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

You can allow changing the ownership inside the guest:

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

Following up on @StephenKing and @aycokoster awesome tips, I had a use-case for mounting another directory read-only.

I added

config.vm.share_folder "foo", "/guest/path", "/host/path", :extra => 'ro'

and

# discard exit status because chown `id -u vagrant`:`id -g vagrant` /host/path is okay

vagrant up || true 

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!