Centos7 with private network lost fixed IP

2019-04-29 01:31发布

I've a network issue with a centos7 vagrant box. When I setup a private network, with fixed IP, this one is well binding first, but get lost after some seconds/minutes.

 vagrant up
 ==> default: Forcing shutdown of VM...
 ==> default: Destroying VM and associated drives...
 ==> default: Running cleanup tasks for 'shell' provisioner...
 Bringing machine 'default' up with 'virtualbox' provider...
 ==> default: Importing base box 'sfeirbenelux/centos7'...
 ==> default: Matching MAC address for NAT networking...
 ==> default: Checking if box 'sfeirbenelux/centos7' is up to date...
 ==> default: Setting the name of the VM: poc_docker
 ==> default: Fixed port collision for 22 => 2222. Now on port 2200.
 ==> default: Clearing any previously set network interfaces...
 ==> default: Preparing network interfaces based on configuration...
     default: Adapter 1: nat
     default: Adapter 2: hostonly
 ==> default: Forwarding ports...
     default: 22 => 2200 (adapter 1)
 ==> default: Running 'pre-boot' VM customizations...
 ==> default: Booting VM...
 ==> default: Waiting for machine to boot. This may take a few minutes...
     default: SSH address: 127.0.0.1:2200
     default: SSH username: vagrant
     default: SSH auth method: private key
     default: Warning: Connection timeout. Retrying...
     default:
     default: Vagrant insecure key detected. Vagrant will automatically replace
     default: this with a newly generated keypair for better security.
     default:
     default: Inserting generated public key within guest...
     default: Removing insecure key from the guest if its present...
     default: Key inserted! Disconnecting and reconnecting using new SSH key...
 ==> default: Machine booted and ready!
 ==> default: Checking for guest additions in VM...
     default: The guest additions on this VM do not match the installed version of
     default: VirtualBox! In most cases this is fine, but in rare cases it can
     default: prevent things such as shared folders from working properly. If you see
     default: shared folder errors, please make sure the guest additions within the
     default: virtual machine match the version of VirtualBox you have installed on
     default: your host and reload your VM.
     default:
     default: Guest Additions Version: 5.0.2
     default: VirtualBox Version: 4.3
 ==> default: Setting hostname...
 ==> default: Configuring and enabling network interfaces...
 ==> default: Running provisioner: shell...
     default: Running: inline script
 ==> default: net.ipv4.ip_forward = 1
 ==> default: Setting up swapspace version 1, size = 2097148 KiB
 ==> default: no label, UUID=170723d8-1782-4915-a877-4d9874ac7396
 ➜  install git:(master) ✗ ping 192.168.100.20
 PING 192.168.100.20 (192.168.100.20): 56 data bytes
 64 bytes from 192.168.100.20: icmp_seq=0 ttl=64 time=0.390 ms
 64 bytes from 192.168.100.20: icmp_seq=1 ttl=64 time=0.252 ms
 64 bytes from 192.168.100.20: icmp_seq=2 ttl=64 time=0.461 ms
 ^C
 --- 192.168.100.20 ping statistics ---
 3 packets transmitted, 3 packets received, 0.0% packet loss
 round-trip min/avg/max/stddev = 0.252/0.368/0.461/0.087 ms
 ➜  install git:(master) ✗ ping 192.168.100.20
 PING 192.168.100.20 (192.168.100.20): 56 data bytes
 64 bytes from 192.168.100.20: icmp_seq=0 ttl=64 time=0.273 ms
 Request timeout for icmp_seq 1
 Request timeout for icmp_seq 2
 Request timeout for icmp_seq 3
 Request timeout for icmp_seq 4

With this vagrant file :

vagrant.configure(2) do |config|

 config.vm.box = "centos7"
 config.vm.hostname = "docker-registry"
 config.vm.box_url = "https://github.com/holms/vagrant-centos7-box/releases/download/7.1.1503.001/CentOS-7.1.1503-x86_64-netboot.box"

 #config.vm.network "public_network"
 config.vm.network "private_network", ip: "192.168.100.20"
 #config.vm.synced_folder ".", "/vagrant", type: "nfs"

 # VirtualBox
 config.vm.provider :virtualbox do |vb|
   vb.name = "poc_docker"
   vb.memory = 3072
   vb.cpus = 2
   vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
   vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
 end


end

When I connect on the box, it seems that the ip is lost.

sudo /sbin/ifup eth1

Fix the problem temporary, but the IP get losts sometime again. Rebooting the box (vagrant halt && vagrant up) seem to fix the problem permanently. I've try to create a custom centos 7 box with Packer ( https://atlas.hashicorp.com/sfeirbenelux/boxes/centos7), and I reproduce the issue too.

I suppose that's a Centos 7 specific issue ? Do you already have the same problem ? Is there a way to fix this by adding a vagrant command ?

My environment :

OS X Yosemite ➜ ~ vagrant --version Vagrant 1.7.2 ➜ ~ VBoxManage --version 4.3.26r98988

3条回答
We Are One
2楼-- · 2019-04-29 01:58

The fix is mentioned in: https://github.com/mitchellh/vagrant/pull/8148

In /opt/vagrant/embedded/gems/gems/vagrant-1.9.1/plugins/guests/redhat/cap/configure_networks.rb,

add /sbin/ifup '#{network[:device]}' right after nmcli c reload || true.

查看更多
ら.Afraid
3楼-- · 2019-04-29 02:03

There seems to be a similar issue in newer Vagrant versions (1.9.1).

A pull request that fixes the issue was already merged (not released yet):

https://github.com/mitchellh/vagrant/pull/8148

If you want to apply that fix you can try this (sudo may be required, depending on the file permissions):

  1. Locate you local Vagrant installation (on Fedora it is /opt/vagrant/)
  2. Move to the folder of the vagrant gem:

    cd embedded/gems/gems/vagrant-1.9.1
    
  3. Initialize a new Git Repository and add the GitHub remote:

    git init
    git remote add origin https://github.com/mitchellh/vagrant.git
    git fetch --all
    
  4. Checkout a new branch and reset it to your current release (e.g. 1.9.1).

    If you are unsure if you have local changes do not use --hard and do a git diff / git checkout after the reset:

    git checkout -b hotfixes
    git reset --hard v1.9.1
    
  5. Now apply the hotfix:

    git cherry-pick 96611341a96d7d19fdade5556a110b22c6add22b
    

After that a vagrant reload should bring up the local network just fine.

查看更多
疯言疯语
4楼-- · 2019-04-29 02:07

It seems that's a problem due to DHCP configuration ( issue : https://github.com/mitchellh/vagrant/issues/5590 )

the network service doesn't reload NetworkManager, so it will continue to wait for dhcp lease .. and after dhcp timeout, the fixed ip is removed.

Adding a shell provision script with

sudo nmcli connection reload
sudo systemctl restart network.service

Fixes the problem

EDIT: vagrant 1.7.3+ fixes the bug too ;)

查看更多
登录 后发表回答