Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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にするとこの現象は起きませんでした

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした