patorashのブログ

方向性はまだない

続・WindowsのVagrantでフォルダ共有でハマったのでメモ

先日、WindowsVagrantのフォルダ共有でハマったのでメモ、という記事を書きました。

patorash.hatenablog.com

この時に、SMBでフォルダ共有をする際にipconfigで取得したIPをsmb_hostに設定すると書いていたのですが、PCを再起動したらこのIPが変わってしまいました😰PCを再起動する度にipconfigをしてsmb_hostを書き換えるのは現実的ではないな…と思ったので、回避策を探したのですが、全然見つかりませんでした。そもそもsmb_hostを設定していない例ばっかりが出てくるのです。これはおかしい…。

PS > vagrant reload
==> default: Attempting graceful shutdown of VM...
    default: Configuring the VM...
==> default: Starting the machine...
==> default: Waiting for the machine to report its IP address...
    default: Timeout: 120 seconds
    default: IP: fe80::215:5dff:fe8f:1b2a
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: fe80::215:5dff:fe8f:1b2a:22
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!

Vagrant requires administrator access for pruning SMB shares and
may request access to complete removal of stale shares.
==> default: Preparing SMB shared folders...

Vagrant requires administrator access to create SMB shares and
may request access to complete setup of configured shares.
==> default: Mounting SMB shared folders...
We couldn't detect an IP address that was routable to this
machine from the guest machine! Please verify networking is properly
setup in the guest machine and that it is able to access this
host.

As another option, you can manually specify an IP for the machine
to mount from using the `smb_host` option to the synced folder.

仕方がないのでホスト側のIPを取得する処理をVagrantfileに書く、というアプローチを取ろうとしたのですが、インターフェースの情報は取れているのに、IPは取れない。そもそもそれが出来ていたらVagrantもできてるやろ…と後で思いましたが、以下のを試していました。

qiita.com

これをやってもAddrオブジェクトがnilを返してダメでした。

その後、CentOSにしてみる、SSHして固定IPを設定するなど試しましたが、全部ダメ😥。ゲスト側からホスト側のIPにpingを打っても音沙汰なし。しかし、ホスト側からゲスト側のIPにはpingが通りました。ここで再び、ホスト側(Windows)のネットワークの設定を疑いました。

結論からいうと、Vagrantで作られたDefault Switchのネットワークがデフォルトでパブリックになっていることが原因でした。パブリックの場合、他のPCなどから自分のPCが見えないようにする設定がされています。そのため、ゲスト側からホスト側が見えなかったのでしょう。Default SwitchのネットワークはPCの中だけだし、プライベートに変更しました。

カスペルスキーの設定にはなりますが、以下のように設定していきます。

f:id:patorash:20190413120548p:plain
設定画面を開く

f:id:patorash:20190421031215p:plain
ネットワークをクリックする

f:id:patorash:20190421031221p:plain
Vagrantで選択したネットワークがパブリックだったらプライベートに変更する

これで、SMBの設定ではsmb_hostを省略できるようになりました👌設定は以下の通り。

# フォルダ共有設定
config.vm.synced_folder ".",
  "/vagrant",
  create: true,
  type: "smb",
  mount_options: ["vers=3.0"],
  smb_username: '**********' # Windowsのログインユーザー名
  smb_password: '**********', # Windowsのログインパスワード

教訓

ググっても解決策がない場合、FireWallとネットワークの公開範囲設定を疑おう👊