Edited at

winodwsでvagrant-hostsupdate導入後にvagrant upできなくなった失敗談と対処法。

More than 3 years have passed since last update.


この記事に含まれる内容。



  • vagrant plugin install vagrant-hostsupdaterの説明。

  • 上記のpluginを入れた事でvagrant upできない問題の発生。

  • 以上をふまえた際の解決策と考察


開発環境(ホスト)

ホストOS
windows8.1 64bit

vagrant
ver 1.81

Oracle VM VirtualBox
バージョン 5.0.12

git
ver 2.8.

アカウントの種類
標準ユーザ


結論

vagrant-hostsupdateを入れることでvagrant upした際にホストOSのhostsファイルに自動で書き込みを行う事が可能になるが、Windowsではhostsファイルの編集には管理者権限(Administrator)でなければ出来ない為、はじかれてしまい、結果エラー(Permission denied)が発生した。

Permission denied @ rb_sysopen - 

C:/WINDOWS/system32/drivers/etc/hosts (Errno::EACCES)

from C:/Users/chi/.vagrant.d/gems/gems/vagrant-hostsupdater-1 .0.2/lib/vagrant-hostsupdater/HostsUpdater.rb:98:in `open'
from C:/Users/chi/.vagrant.d/gems/gems/vagrant-hostsupdater-1 .0.2/lib/vagrant-hostsupdater/HostsUpdater.rb:98:in `addToHosts'
from C:/Users/chi/.vagrant.d/gems/gems/vagrant-hostsupdater-1 .0.2/lib/vagrant-hostsupdater/HostsUpdater.rb:47:in `addHostEntries'
from C:/Users/chi/.vagrant.d/gems/gems/vagrant-hostsupdater-1 .0.2/lib/vagrant-hostsupdater/Action/UpdateHosts.rb:17:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:95:in `block in finalize_action'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:95:in `block in finalize_action'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:95:in `block in finalize_action'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/check_guest_additions.rb:49:in `cal l'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/Users/chi/.vagrant.d/gems/gems/vagrant-vbguest-0.11.0 /lib/vagrant-vbguest/middleware.rb:31:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/customize.rb:40:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/wait_for_communicator.rb:71:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/customize.rb:40:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/boot.rb:18:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/customize.rb:40:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/sane_defaults.rb:38:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/set_hostname.rb:16:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/forward_ports.rb:31:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/network_fix_ipv6.rb:41:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/network.rb:122:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/clear_network_interfaces.rb:26:in ` call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/prepare_nfs_settings.rb:18:in `call '
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/synced_folders.rb:87:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/synced_folder_cleanup.rb:28:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/synced_folders/nfs/action_cleanup.rb:19:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/prepare_nfs_valid_ids.rb:12:in `cal l'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/handle_forwarded_port_collisions.rb:49:in `c all'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/prepare_forwarded_port_collision_pa rams.rb:30:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/env_set.rb:19:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/Users/chi/.vagrant.d/gems/gems/vagrant-omnibus-1.4.1/ lib/vagrant-omnibus/action/install_chef.rb:38:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/Users/chi/.vagrant.d/gems/gems/vagrant-cachier-1.2.1/ lib/vagrant-cachier/action/install_buckets.rb:14:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/provision.rb:80:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/Users/chi/.vagrant.d/gems/gems/vagrant-cachier-1.2.1/ lib/vagrant-cachier/action/configure_bucket_root.rb:20:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/clear_forwarded_ports.rb:15:in `cal l'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/set_name.rb:50:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/clean_machine_folder.rb:17:in `call '
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/check_accessible.rb:18:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:95:in `block in finalize_action'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builder.rb:116:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/runner.rb:66:in `block in run'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/util/busy.rb:19:in `busy'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/runner.rb:66:in `run'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/call.rb:53:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:95:in `block in finalize_action'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builder.rb:116:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/runner.rb:66:in `block in run'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/util/busy.rb:19:in `busy'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/runner.rb:66:in `run'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/call.rb:53:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:95:in `block in finalize_action'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builder.rb:116:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/runner.rb:66:in `block in run'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/util/busy.rb:19:in `busy'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/runner.rb:66:in `run'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/call.rb:53:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/box_check_outdated.rb:78:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/builtin/config_validate.rb:25:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/pl ugins/providers/virtualbox/action/check_virtualbox.rb:17:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagrant/action/warden.rb:34:in `call'
from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.1/li b/vagran


ことの始まり

借りているレンタルサーバ(ロリポップ!)には標準からWordPressをインストールしてくれる環境が整っており、どうせなら仮想環境を整えてからアップローダしようと思い、今朝からWordPressで色々見ていた。見ていく内にどうやらVagrantのboxとしてWordPressがセットになっているVCCWというものがあることを知り、早速いじって見る事に。

そこで以下のサイトを参考にして環境構築を始める。


VCCWによるWordPress用ローカル環境構築&ハマりやすいポイント


サイト通りにVCCWを導入するにあたって、まずはvagrant-hostsupdaterが必要である事を知りインストールする。


そもそもvagrant-hostsupdateとは?

公式より引用

Vagrant Host Manager


This plugin adds an entry to your /etc/hosts file on the host system.

On up, resume and reload commands, it tries to add the information, if its not already existant in your hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit the file.

On halt, destroy, and suspend, those entries will be removed again. By setting the config.hostsupdater.remove_on_suspend = false, suspend and halt will not remove them.


意訳


このプラグインは、あなたのホスト・システム上/etc/hostsファイルにエントリ(ゲストOSのホスト名とipアドレス)を追加します。立ち上げ(vagrant up)や再起動(vagrant resume)、再ロード(vagrant reload)すると、ホストOSのhostsファイルにエントリがない場合は情報を追加しようとします。追加する場合はファイル編集にsudoを使用するので管理者パスワードの入力を求められます。停止(vagrant halt)、破壊(vagrant vagrant destroy)、一時停止(vagrant suspend)でこれらのエントリが再び削除されます。 しかしconfig.hostsupdater.remove_on_suspendを設定することにより、vagrant haltvagrant suspendをしてもエントリが削除されないようにする事も可能です。


以上よりvagrant-hostsupdateとはvagrantのpluginの一種であり、VagrantのゲストOSを起動した際に、ホストOSのhostsファイルにゲストOSのホスト名・IPを追加するpluginであることが理解できます。

インストール手順

vagrant plugin install vagrant-hostsupdater

設定ファイル

VagrantfaileからIPアドレスとhostnameの設定が行えます。

基本的な使い方としてはホスト名とIPアドレスを以下のように設定して起動します。

config.vm.network "private_network", ip: "192.168.00.00"

config.vm.hostname = "vm-node"


使い方

vagrant up後にhostsファイルに設定したゲストOSのIPアドレスが書き込まれ、ホストOS上からSSHクライアントでssh vagrant@hogehoge.develで接続が出来る事が可能となります。

参考にしたサイト




エラー発生と対策。

上記に述べた通り、原因はPermission deniedであるため、Winodwsユーザであれば管理者権限に書き換えるか、ホストOSがLinuxであればsudo権限で実行すべきであると考える。

また自分の環境ではこのErrorはVCCWを導入するときにエラーが発生したが、既にあるvagran boxvagrant upする際にもエラーが発生した為、vagrant-hostsupdateを導入する競合リスクが大きいと考え、vagrant plgin listからアンインストールした。結果hostsファイルに直接書き込む事で無事VCCWが導入でき、他のvagrant boxも元通りvagrant upする事が出来た。


まとめ


  • 今回軽いWordPressを揃えるVCCWの環境構築をするだけで、1日がおわってしまった...。

  • サイトを信用してそのままやるのもいいが、なぜこれが必要なのか?と疑い自分で調べるべきだったと反省。

  • GWの一日目が環境構築で終わる虚しさ...

※追記 2016 4/30

同じErrorで捕まった例。

こちらはvagrant のversionによるエラーだそうです。

環境によっては色んな要因がありそう

Vagrantのプラグイン(vagrant-hostsupdater)のインストールにコケたことがあった。