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?


  • 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.
  • I think this would require cooperation from the host system to do reliably. What's the host OS? Sep 13, 2012 at 23:54
  • @Gilles Host is OS X running VMware Fusion 5.0
    – Old Pro
    Sep 15, 2012 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.
    – KK Patel
    Mar 13, 2013 at 12:34

5 Answers 5


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.


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.

  • 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, 2012 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, 2012 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

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.


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.


Here is a (perhaps overly) simple script for you CentOS VM that will do what your looking for. I recommend Placing this script in /usr/local/sbin, since it's not a system script, and it's run by root.

# nettest.sh
# Get the gateway address from the routing table
gateway=$(netstat -rn | awk '/^ {print $2}')

# Try and ping the gateway
ping -c 1 ${gateway}
# If the ping succeeds, exit.
if [ $? == 0 ]; then
# Otherwise take down and bring up the network interface
    ifdown eth0
    ifup eth0

Once that is done, place this entry in either roots crontab file or in /etc/cron.d/nettest.cron, and you'll be all set:

# Call nettest.sh every 5 minutes
*/5 * * * * /usr/local/sbin/nettest.sh 2>&1> /dev/null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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