Vagrantで固定IPのVMを作ってみたら超簡単だった
VagrantのVMに他のマシンから接続したかったのでブリッジ接続で固定IPにしてみました。
本当に超簡単。 まずはいつも通り
$ vagrant box add centos http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.3-x86_64-v20130101.box
$ vagrant init centos
してから
Vagrantのドキュメントの通りにVagrantFileに
Vagrant::Config.run do |config|
config.vm.network :bridged
end
を追加します。
設定後にvagrant upすると
$ vagrant up
default] VM already created. Booting if it's not already running...
[default] Clearing any previously set forwarded ports...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Available bridged network interfaces:
1) en1: AirMac (AirPort)
2) en0: Ethernet
What interface should the network bridge to?
と、どのNICをブリッジ接続するか聞かれます。
ここでは1のAirMacを選択して
What interface should the network bridge to? 1
[default] Preparing network interfaces based on configuration...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.
Guest Additions Version: 4.2.6
VirtualBox Version: 4.2.16
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- v-root: /vagrant
という具合に起動します。
起動したのでVMに入ってみて
$ vagrant ssh
NICを確認するとeth1が追加されています。
[vagrant@localhost ~]$ ifconfig -a
eth0 Link encap:Ethernet HWaddr **:**:**:**:**:**
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fef7:f048/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1714 errors:0 dropped:0 overruns:0 frame:0
TX packets:942 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:147291 (143.8 KiB) TX bytes:120029 (117.2 KiB)
eth1 Link encap:Ethernet HWaddr **:**:**:**:**:**
inet addr:192.168.0.7 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe27:a6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:314 errors:0 dropped:0 overruns:0 frame:0
TX packets:84 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:57350 (56.0 KiB) TX bytes:10918 (10.6 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
ルーティングテーブルもOK。
[vagrant@localhost ~]$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.2.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1
link-local * 255.255.0.0 U 1002 0 0 eth0
link-local * 255.255.0.0 U 1003 0 0 eth1
default web.setup 0.0.0.0 UG 0 0 0 eth1
という具合にeth1がちゃんとブリッジ接続してます。
静的IP(固定IP)設定は普通にifcfg-eth1を編集すればOK。
eth1の設定を静的IPに設定して
[vagrant@localhost ~]$ sudo vi /etc/sysconfig/network-scripts/ifcfg-eth1
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
#BOOTPROTO=dhcp
ONBOOT=yes
DEVICE=eth1
#VAGRANT-END
# Use static IP
BOOTPROTO=static
IPADDR=192.168.0.27
PREFIX=24
GATEWAY=192.160.0.1
DNS1=192.168.0.1
ネットワークを再起動。
[vagrant@localhost ~]$ sudo /etc/init.d/network restart
Shutting down interface eth0: [ OK ]
Shutting down interface eth1: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0:
Determining IP information for eth0... done.
[ OK ]
Bringing up interface eth1: [ OK ]
eth1のIPアドレスを確認するとちゃんと192.168.0.27になっています。
[vagrant@localhost ~]$ ifconfig -a
eth0 Link encap:Ethernet HWaddr **:**:**:**:**:**
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fef7:f048/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2231 errors:0 dropped:0 overruns:0 frame:0
TX packets:1222 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:189405 (184.9 KiB) TX bytes:153603 (150.0 KiB)
eth1 Link encap:Ethernet HWaddr **:**:**:**:**:**
inet addr:192.168.0.27 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe27:a6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:333 errors:0 dropped:0 overruns:0 frame:0
TX packets:104 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:60813 (59.3 KiB) TX bytes:12044 (11.7 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2 errors:0 dropped:0 overruns:0 frame:0
TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:198 (198.0 b) TX bytes:198 (198.0 b)
ルーティングテーブルも問題なし。
[vagrant@localhost ~]$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.2.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1
link-local * 255.255.0.0 U 1002 0 0 eth0
link-local * 255.255.0.0 U 1003 0 0 eth1
default 10.0.2.2 0.0.0.0 UG 0 0 0 eth0
と、何もトラブルなく設定完了してしまいました。