Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm running a Centos virtual machine on a laptop using a bridged network connection to avoid double-NAT issues, with the address assigned via DHCP. This works fine until I move the laptop to another network, at which point the VM doesn't notice that it's on a different network and continues to use the old IP address, resulting in a loss of connectivity.

Currently I manage this by manually running ifdown and ifup to reset the network connection, but I'd prefer an automatic way to do this. Suggestions?

Notes:

  • All networks have DHCP servers.
  • The VM correctly obtains an IP address lease on each network when bringing up the interface.
  • I cannot change settings on the DHCP servers.
  • The networks are in different IP address ranges.
share|improve this question
    
I think this would require cooperation from the host system to do reliably. What's the host OS? –  Gilles Sep 13 '12 at 23:54
    
@Gilles Host is OS X running VMware Fusion 5.0 –  Old Pro Sep 15 '12 at 3:16
1  
I think you must have been kept pause VM. You have to do every time shutdown and up interface or restart VM. –  K.K Patel Mar 13 '13 at 12:34

4 Answers 4

This is a suggested workaround that might work for you.

Try to fiddle the lease time provided by the DHCP server on the client side using dhcp-eval (example is right there). Set the value lower than the time changing between networks.

For example, if it takes you 30 minutes usually to get from office to home, setting it to 20 minutes should make your virtual machine have the lease invalidated as soon as you get home.

share|improve this answer
    
I don't see how dhcp-eval would help, but that did lead me to think perhaps I can use supersede option dhcp-lease-time 1200 –  Old Pro Sep 15 '12 at 3:16
    
Nope, that didn't work either. I set send dhcp-lease-time 1200; supersede option dhcp-lease-time 1200; but even a day later after the lease has expired the DHCP client did not automatically acquire a new IP address. Possibly because of the way VMware updates the system clock when resuming a paused VM. –  Old Pro Sep 16 '12 at 19:48

If you set up a second "host-only" network connection with your host, you can watch its primary network connection(s) to see when an IP address changes. Something like...

while sleep 60 ; do
    new_ip="`ssh host_ip ifconfig en1 | grep 'inet ' | awk '{print $2}'`"
    if [ "$new_ip" != "$old_ip" ] ; then
        ifdown eth0 ; ifup eth0
    fi
    old_ip="$new_ip"
done

Or, if you're more familiar with OS X than I am, you could probably hook the networking subsystem to ssh from the host to the guest instead, and avoid the once-per-minute polling.

This does require you to be able to ssh between host and guest without a password, though. You might use a passwordless key that's only allowed to run a script that runs ifconfig en1.

share|improve this answer

Sounds like this is more of an issue with whatever you're using to virtualize (VMWare or VirtualBox or whatever) where it's not telling the virtual machine that the connection has been reset and since the virtual machine still has the DHCP lease from the first network, it doesn't know it needs to get a new one. Rather than trying to play around with the virtual machine, see if whatever you're using to virtualize has a one-click button or something where you can enable/disable the virtual NIC on the virtual machine so that when it's reset it's forced to grab a new IP.

share|improve this answer

Edit your hosts /etc/network/interfaces:

auto eth0
  post-up VBoxManage controlvm nameofyourboxhere setlinkstate1 on
  post-down VBoxManage controlvm nameofyourboxhere setlinkstate1 off

Should do exactly what you want! Can, of course, be improved, but I think I showed you the way.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.