LoginSignup
54
57

More than 5 years have passed since last update.

Vagrantでpublic network設定時に固定IPを使う(外部アクセス可能版)

Last updated at Posted at 2015-03-18

環境

  • Vagrant 1.3.5
  • VirtualBox 4.3.12
  • CentOS 6.5

設定

Vagrantfileのconfig.vm.networkの設定を以下の通りにする。

Vagrantfile
  config.vm.network :public_network, :auto_config => false
  config.vm.provision :shell, :path => "bootstrap.sh"

Boxのフォルダに以下の内容のshellスクリプトを置く(Vagrantfileと同階層のところね)

bootstrap.sh
#/bin/sh

sudo ifconfig eth1 192.168.0.10 netmask 255.255.255.0 up
sudo ip route del default gw 10.0.2.2 eth0
sudo ip route add default gw 192.168.0.1 eth1

いろいろ情報が散乱してたので、自分でまとめてみた。


なんでこんなめんどくさいことを?

通常、public network設定でIP固定にしたい場合は以下の内容で十分だった。

Vagrantfile
config.vm.network :public_network, ip: "192.168.0.10"

でもこれだとApacheなどでWebサーバーを立てた時、ホストPC以外からアクセスができないという問題にぶちあたる。(publicなのに!)
どーもVagrantのBox起動時にroute設定が勝手にされちゃうようで、default routeの設定がおかしくなる。
なのでbootstrap.shでそこを上書きしちゃおうという内容ですね。

54
57
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
57