Cannot change permissions of folders within vagran

2019-01-10 05:52发布

When I ssh in to my vagrant vm, I can change permissions of files and folders above and outside the vagrant user folder, and for files within the vagrant user folder. But cannot change permissions for folders under the vagrant user folder. I have the same problem whether logged in as the vagrant user and root.

Is there some sort of restriction on changing permissions in the vagrant user's folder? The vagrant user folder is not shared with the host OS, but the capistrano deploy folder and the docRoot are.

Guest is CentOS 6, Host is OS X 10.7. Vagrant is 1.0.5. Virtualbox is 4.2.1.

7条回答
Summer. ? 凉城
2楼-- · 2019-01-10 06:00

Change the permissions form the host not the guest. VirtualBox disallows changing permissions on shared files form a guest os.

TLDR; The issue is not that the users on your guest don't have permissions to access your host files. The issue is the executing user of the virtaul box process on your host does not have permissions to write the files in the host. There are two sets of permissions. The guest permissions have to be set just like any other os. You also have to make sure that the virtual box process your guest os is running in has permissions to that folder. If that process only has read access the most any guest user will be able to do is read.

查看更多
太酷不给撩
3楼-- · 2019-01-10 06:03

In Vagrant 1.2.7, version 2 Vagrantfiles are used, so the syntax is slightly different than in previous answers. Underneath is what does the trick for me with CentOS 6.2. I find that using a relative path as the source works best in my situation. It points to the shared folder.

config.vm.synced_folder "./", "/vagrant", owner: 'vagrant', group: 'apache', extra: 'dmode=775,fmode=775'

As stated by Jamie, it still is necessary that you configure it before creation, so use a vagrant reload after you've edited your overriding Vagrantfile.

查看更多
成全新的幸福
4楼-- · 2019-01-10 06:04

Can't comment just yet, but to extend on MDeSilva's answer for Vagrant 1.7.2:

Might be obvious to some, but the group and owner should be in quotes.

group: "sync_group", owner: "sync_owner", mount_options: ['dmode=755, fmode=644']
查看更多
疯言疯语
5楼-- · 2019-01-10 06:11

VirtualBox doesn't allow changing the owner/permissions for synced folders.

You can change it in the Vagrant file (as answered by others).
Consider changing the owner instead of the group.
Consider also that - if done so that your server can write to files - your server is likely called www-data instead of httpd. Use ps aux | grep nginx [or apache / lighthttpd] to check.

There are some other options:

  • Change the owner of the program that is accessing the shared files instead of the synced folder.
    For example, if PHP needs to write to file, change the server and PHP to run as vagrant. [In Apache, that's done in httpd.conf. NGINX's user is set in nginx.conf, php-fpm's user is in php-fpm.conf or one of the files it includes.
    You need to change the permissions on the Apache lockfile (/var/lock/apache2) or PHP websocket file (/var/run/php5-fpm.sock)] and webserver.
  • Use a different VM (HyperV, VMware) instead of VirtualBox. Other VM's dont seem to have this restriction.
  • Use RSyn to sync files instead of using the default syncing.
    config.vm.synced_folder "/var/www/", type: "rsync"
  • Set all the permissions to 777. Normally this would disastrous and not even a suggestion. It's still a bad idea, but on a VM is possibly doable. Think twice before giving shared access though.
    config.vm.synced_folder "/var/www/", mount_options: ["dmode=777", "fmode=666"]

These answers are better described by Ryan Sechreset and Jeremy Kendall.

查看更多
Evening l夕情丶
6楼-- · 2019-01-10 06:12

For Vagrant 1.7.2 Edit Vagrant file like this,

group: sync_group, owner: sync_owner, mount_options: ['dmode=777', 'fmode=776']
查看更多
女痞
7楼-- · 2019-01-10 06:15

The format for shared folders changes across different versions of Vagrant. See Fabio's answer https://serverfault.com/questions/398414/vagrant-set-default-share-permissions

Vagrant version 1.3.1 and earlier

config.vm.share_folder "v-data", "/export", "/export", :owner => 'vagrant', :group => 'httpd', :extra => 'dmode=775,fmode=775'

Vagrant version 1.3.1, 1.3.2

In Vagrant 1.3.1 and later, the extra option has been replaced with mount_options that expects an array.

config.vm.share_folder "v-data", "/export", "/export", :owner => 'vagrant', :group => 'httpd', :mount_options => ['dmode=775', 'fmode=775']

Vagrant version >=1.3.3

In vagrant 1.3.3 it appears config.vm.share_folder has been replaced with config.vm.synced_folder.

config.vm.synced_folder "v-data", "/export", "/export", :owner => 'vagrant', :group => 'httpd', :mount_options => ['dmode=775', 'fmode=775']

查看更多
登录 后发表回答