restart networking: debian

I am trying to restart the networking on a debian box if the /etc/network/interfaces file is out of sync (i.e. first time setting up this server).

I have tried

  • name: restart networking
    service: name=networking state=restarted

But it just hangs.

I have tried

  • name: restart networking
    shell: shutdown now -r
    async: 120
    poll: 5

but the next task doesn’t run because it errs out at:
fatal: [192.168.11.123] => SSH encountered an unknown error during the connection.

How do others restart networking?

I can assure you that “shutdown now -r” is not how you restart networking. That will restart you entire machine, which would lead to that fatal error message you’re seeing. As for the first method you mention that is hanging – are you sure the settings you have assigned networking are valid? Why are you restarting the network?

  • James

why? I am dealing with servers with several NIC’s and dozens of virtual IP’s

I realize restarting the client isn’t the ideal solution, but tried it to see if it would work. The interfaces file is fine, as all interfaces come up on reboot just fine.

Debian networking is somewhat complicated. The networking init script and networking upstart jobs don’t do what most expect. Those scripts and their underlying utilities only configure what they know about from the interfaces file. If you remove something, or assign an IP to another interface, it won’t deconfigure the previous configuration, it will only apply the newly provided configurations.

Example:

  1. 10.10.10.10 on eth0
  2. Edit /etc/network/interfaces and remove 10.10.10.10 from eth0 and assign to eth1
  3. Restart networking

The result is that you now have 10.10.10.10 on eth0 and eth1, because they don’t fully deconfigure the interface before applying the configuration of the interfaces file.

And it is not exactly straight forward to get it to completely deconfigure all interfaces and then reconfigure on start up.

I have a network init script for Debian/Ubuntu that mimics the functionality of the RHEL network init script at https://github.com/sivel/scripts/blob/master/ubuntu/network-compat.sh

Your other options are to use the ip command to do all of your work after modifying /etc/network/interfaces, but that could be complicated, without knowing exactly what changed in the interfaces file.

ansible -s -K -m service -a “name=networking state=restarted” testmachines , works for me as long as the ip im using to access the box has not changed (wheezy and pangolin).

That script looks great! On Debian 7, the ifup (and ifdown and ifquery) commands don’t have a -e option, so this errs out.

Strangely enough, this is basically what I tried before attempting to put it into my playbook. I just tried again and waited about five minutes before determining that the interfaces were never going to come up. From the machine, I can ifup eth0 (the control interface) and ansible continues along happily. However, it doesn’t seem to bring the interfaces back up directly from ansible.

BTW, the client is running Debian 7.

I should mention that I have also tried simply:

shell: ifdown eth0; ifup eth0

to no avail. Perhaps putting these commands into a shell script, copying the script to the client and then running it would do that trick?

Simple fix:

contents of etc.network.restartall

#!/bin/bash

ifdown -a
ifup -a

I should add that the switch -a, only affects interfaces marked as auto in /etc/network/interfaces.