devopsgroup-io / vagrant-hostmanager

:pencil: A Vagrant plugin that manages hosts files within a multi-machine environment.
Mozilla Public License 2.0
1.46k stars 146 forks source link

Reports "Permission denied" but /etc/hosts is updated only when "sudo vagrant up" is used, not as normal user. #212

Open dglinder opened 8 years ago

dglinder commented 8 years ago

Platform: OSX 10.11.6 (El Capitan), Vagrant 1.8.5,

I have setup my account ("dan") in the /etc/sudoers so it can modify the /etc/hosts file:

Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp /Users/dan/.vagrant.d/tmp/hosts.local /etc/hosts
dan ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE

When I run a simple vagrant up on this Vagrantfile:

Vagrant.configure("2") do |config|
  config.hostmanager.enabled = true
  config.hostmanager.manage_host = true
  config.vm.box = "centos/6"
  config.vm.hostname = "myhost"
end

The run fails:

==> default: Updating /etc/hosts file on active guest machines...
==> default: Updating /etc/hosts file on host machine (password may be required)...
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
/opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:1392:in `initialize': Permission denied @ rb_sysopen - /Users/dan/.vagrant.d/tmp/hosts.local (Errno::EACCES)
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:1392:in `open'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:1392:in `block in copy_file'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:1391:in `open'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:1391:in `copy_file'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:485:in `copy_file'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:402:in `block in cp'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:1570:in `block in fu_each_src_dest'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:1586:in `fu_each_src_dest0'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:1568:in `fu_each_src_dest'
           from /opt/vagrant/embedded/lib/ruby/2.2.0/fileutils.rb:401:in `cp'
           from /Users/dan/.vagrant.d/gems/gems/vagrant-hostmanager-1.8.5/lib/vagrant-hostmanager/hosts_file/updater.rb:68:in `update_host'
           from /Users/dan/.vagrant.d/gems/gems/vagrant-hostmanager-1.8.5/lib/vagrant-hostmanager/action/update_all.rb:46:in `call'
           from /opt/vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/action/warden.rb:34:in `call'
...much more output deleted...
        from /opt/vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/environment.rb:561:in `lock'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/machine.rb:186:in `call'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/machine.rb:186:in `action'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/batch_action.rb:82:in `block (2 levels) in run'

If I manually run sudo /bin/cp /Users/dan/.vagrant.d/tmp/hosts.local /etc/hosts as my dan user the /etc/hosts file is updated from the generated hosts.local.

ghost commented 7 years ago

In my case on Windows 10 i was looking in error that was in cmder. there was permission denied for C:/Users/Alex/.vagrant.d/tmp/hosts.local, i checked with ls -all C:/Users/Alex/.vagrant.d/tmp there was two files: -r--r--r-- 1 Alex 0 3466 2017-01-06 10:59 hosts.local -rw-rw-rw- 1 Alex 0 2220 2017-01-06 11:31 hosts.youwe-vm i removed falg "Read Only" for hosts.local it solved my issue