81
68

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vagrant + centos7 でprivate_networkで設定したIPに接続ができない

Last updated at Posted at 2016-12-29

MacBook Pro 13" (late2016)を新調した際にはまったのでメモ

##環境

  • macOS Sierra 10.12.2
  • vagrant 1.9.1
  • VirtualBox 5.1.12
Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.network :private_network, ip: "192.168.33.10"
end

##構築結果

$ vagrant up
~省略
$ ping 192.168.33.10
PING 192.168.33.10 (192.168.33.10): 56 data bytes
Request timeout for icmp_seq 0

つながらない

##(CASE1) eth1にアドレスがないケース

$ vagrant ssh -c "ip a"
~省略
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 08:00:27:83:b9:c8 brd ff:ff:ff:ff:ff:ff

###解決

$ vagrant ssh -c "sudo systemctl restart network.service"
$ ping 192.168.33.10 # => OK

ネットワークをリスタートするとeth1に192.168.33.10のアドレスが割り当てられた

##(CASE2) eth1に192.168.56.xxが割り当てられているケース

$ vagrant ssh -c "ip a"
~省略
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:bf:9d:c2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global dynamic eth1
       valid_lft 1082sec preferred_lft 1082sec

何故に192.168.56.101?
192.168.56.101にpingも通らず

###解決

$ vagrant ssh
$ ls -l /etc/sysconfig/network-scripts/ifcfg*
-rw-r--r--. 1 root root  86 12月 15 11:14 /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r--. 1 root root 254  9月 12 10:47 /etc/sysconfig/network-scripts/ifcfg-lo

eth1のインターフェース設定ファイルがないので手動で書く

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-eth1
ifcfg-eth1
#VAGRANT-BEGIN
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.33.10
NETMASK=255.255.255.0
DEVICE=eth1
PEERDNS=no
#VAGRANT-END
$ sudo chown vagrant:vagrant /etc/sysconfig/network-scripts/ifcfg-eth1 #一応
$ sudo systemctl restart network.service
$ ip a
~省略
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:bf:9d:c2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.33.10/24 brd 192.168.33.255 scope global eth1
       valid_lft forever preferred_lft forever

eth1に192.168.33.10が割り当てられた

##最後に

$ vagrant reload
$ ping 192.168.33.10 # => Request timeout

再度立ち上げるとネットワークがリセットされてしまう(事があった)ので

Vagrantfileにおまじない追加

Vagrantfile
config.vm.provision "shell", run: "always", inline: "systemctl restart network.service"

なんかバグっぽいからvagrantのバージョンアップで直りそうな気がするんですけどね
ちなみにboxをcentos/6にするとこの現象は起きませんでした

81
68
3

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
81
68

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?