How to successfully restart a network without reboot over SSH?

488,307

Solution 1

Turns out this is a "feature". The only supported way of restarting an interface in Ubuntu Server is sudo ifdown eth0 && sudo ifup eth0

https://bugs.launchpad.net/ubuntu/+source/ifupdown/+bug/1301015

Solution 2

ifdown, ifup didn't work for me (likely SSH connection timeout before the second command). What did work was:

sudo service network-manager restart

This was on a 14.04 ubuntu-desktop system.

Solution 3

What's changed is they don't want you "bouncing" the network anymore. stopping, and starting, still work. restart no longer works. I just "solved" this "problem", that is to say I got the old behavior back. To revert to prior behavior: Take a 13.10 /etc/init/networking.conf file and replace the 14.04 file with it. (edit: clarified which replaces which)

The process looks like this:

(Just before this, I configured my /etc/network/interfaces for eth1 on a 192.168.117.x address)

"/etc/network/interfaces" 16L, 413C written  
root@1404-Anode:~# service networking restart  
stop: Job failed while stopping  
start: Job is already running: networking  
root@1404-Anode:~# echo "hmm, wth?"
hmm, wth?
root@1404-Anode:~# ifconfig  
eth0      Link encap:Ethernet  HWaddr 00:0c:29:d6:a8:19  
          inet addr:192.168.115.105  Bcast:192.168.115.255  Mask:255.255.255.0  
          inet6 addr: 2002:4077:9050:1234:a08c:29c1:ce9b:a57b/64 Scope:Global  
          inet6 addr: fe80::20c:29ff:fed6:a819/64 Scope:Link  
          inet6 addr: 2002:4077:9050:1234:20c:29ff:fed6:a819/64 Scope:Global  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:646 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:531 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:1000  
          RX bytes:58748 (58.7 KB)  TX bytes:75465 (75.4 KB)  
(lo removed here)  
root@1404-Anode:~# cd /etc/init
root@1404-Anode:/etc/init# diff networking.conf.1310 networking.conf.1404  
13c13  
<       and (stopped udevtrigger or container)) or runlevel [2345]  
---  
>     and (stopped udevtrigger or container)) or runlevel [2345] or stopped  networking   >RESULT=failed PROCESS=post-stop EXIT_STATUS=100  
16a17,20  
>     if [ "$UPSTART_EVENTS" = "stopped" ] && [ "$UPSTART_JOB" = "networking" ] && [ "$EXIT_STATUS" = "100" ]; then  
>         exit 0  
>     fi  
>  
21a26,31  
>     if [ -z "$UPSTART_STOP_EVENTS" ]; then  
>        echo "Stopping or restarting the networking job is not supported."  
>         echo "Use ifdown & ifup to reconfigure desired interface."  
>         exit 100  
>     fi  
root@1404-Anode:/etc/init#  

Do the same for /etc/init.d/networking script, which is what the /etc/init/networking.conf file references/calls.

root@1404-Anode:/etc/init# cp networking.conf.1310 networking.conf  
root@1404-Anode:/etc/init# cd ../init.d  
root@1404-Anode:/etc/init.d# diff networking.1404 networking.1310  
15d14  
< STATEDIR="$RUN_DIR/state"
21a21,27
> # Make sure that it's clear to the user that they shouldn't use this
> # script under upstart
> if init_is_upstart; then
>       echo "ERROR: Calling a sysvinit script on a system using upstart isn't supported. Please use the 'service' command instead."  
>       exit 1
> fi
>
52,54d57
<       if ! chown root:netdev "$RUN_DIR" ; then
<           log_warning_msg "can't chown $RUN_DIR"
<       fi
160,162d162
<       if init_is_upstart; then
<               exit 1
<       fi
166c166
<       state=$(ifquery --state)
---
>       state=$(cat /run/network/ifstate)
root@1404-Anode:/etc/init.d# cp networking.1310 networking
root@1404-Anode:/etc/init.d# service networking restart
networking stop/waiting
networking start/running
root@1404-Anode:/etc/init.d# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:d6:a8:19
          inet addr:192.168.115.105  Bcast:192.168.115.255  Mask:255.255.255.0
          inet6 addr: 2002:4077:9050:1234:a08c:29c1:ce9b:a57b/64 Scope:Global
          inet6 addr: fe80::20c:29ff:fed6:a819/64 Scope:Link
          inet6 addr: 2002:4077:9050:1234:20c:29ff:fed6:a819/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3398 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2545 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:318654 (318.6 KB)  TX bytes:418804 (418.8 KB)

eth1      Link encap:Ethernet  HWaddr 00:0c:29:d6:a8:23
          inet addr:192.168.117.105  Bcast:192.168.117.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fed6:a823/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:98 errors:0 dropped:58 overruns:0 frame:0
          TX packets:15 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:20055 (20.0 KB)  TX bytes:1226 (1.2 KB)
(lo removed)
root@1404-Anode:/etc/init.d# echo "hah, it works! *8^)"
Hah, it works! *8^)
root@1404-Anode:/etc/init.d#

(note: the steps where I scp'd the 1310 versions into the 1404 system are omitted for brevity). 

Obviously there's a reason they put a defensive exit in there, but they don't bother to really output what's going on very well.

An entry does go into /var/log/upstart/networking.log when you try, that looks like:

Stopping or restarting the networking job is not supported.  
Use ifdown & ifup to reconfigure desired interface.  

But they really could/should have output that as the dialog message when you try service networking restart. ah well. figured it out and even a old-way work around.

EDIT: I have found this causes an unintentional triggering of the script controlled by /etc/init/failsafe.conf which is undesirable as it causes a 120 second timeout delay in every boot up... as well as perhaps masking actual misconfigurations/network issues which this delay's appearance would indicate, but it's already showing all the time. (e.g. An unplugged cable, which was allowing access to a network file share mapped in /etc/fstab, for instance)

In any case, I will figure out what is causing this always hitting the timeout, and post a fix when I find it.

Solution 4

In reference to kvm-user420's response, I've gone ahead and setup a script to replace Ubuntu 14.04's networking scripts with that of Ubuntu 13.10

You can find it here: https://github.com/metral/restore_networking

Enjoy!

Share:
488,307

Related videos on Youtube

SimonJGreen
Author by

SimonJGreen

Technical Director @ Wirehive. I spend most of my day sysadmining Linux servers. Twitter: @SimonJGreen

Updated on September 18, 2022

Comments

  • SimonJGreen
    SimonJGreen over 1 year

    In Ubuntu 14.04 neither sudo service networking restart nor sudo /etc/init.d/networking restart do anything any more. They also both exit with code 1. Something has obviously changed (or half changed) but I can't find what. This obviously causes problems with remote network reconfiguration and tools like Ansible.

    $ sudo bash -x /usr/sbin/service networking restart
    [sudo] password for wirehive:
    ++ basename /usr/sbin/service
    + VERSION='service ver. 0.91-ubuntu1'
    ++ basename /usr/sbin/service
    + USAGE='Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]'
    + SERVICE=
    + ACTION=
    + SERVICEDIR=/etc/init.d
    + OPTIONS=
    + '[' 2 -eq 0 ']'
    + cd /
    + '[' 2 -gt 0 ']'
    + case "${1}" in
    + '[' -z '' -a 2 -eq 1 -a networking = --status-all ']'
    + '[' 2 -eq 2 -a restart = --full-restart ']'
    + '[' -z '' ']'
    + SERVICE=networking
    + shift
    + '[' 1 -gt 0 ']'
    + case "${1}" in
    + '[' -z networking -a 1 -eq 1 -a restart = --status-all ']'
    + '[' 1 -eq 2 -a '' = --full-restart ']'
    + '[' -z networking ']'
    + '[' -z '' ']'
    + ACTION=restart
    + shift
    + '[' 0 -gt 0 ']'
    + '[' -r /etc/init/networking.conf ']'
    + which initctl
    + grep -q upstart
    + initctl version
    + case "${ACTION}" in
    + stop networking
    stop: Job failed while stopping
    + :
    + exec start networking
    start: Job is already running: networking
    

    What is the correct way to restart networking in Ubuntu 14.04 Server remotely?

    • chili555
      chili555 about 10 years
      Do you really want to restart networking or just restart an interface such as eth0? In any case, it will probably break the ssh connection.
    • goo
      goo about 10 years
      Since both are shell scripts, gain information with sudo bash -x /usr/sbin/service networking restart and sudo bash -x /etc/init.d/networking restart
    • SimonJGreen
      SimonJGreen about 10 years
      @chili555 what I want to happen is for changes in /etc/network/interfaces to take effect. Prior to these scripts stopping working SSH connections survive this restart.
    • SimonJGreen
      SimonJGreen about 10 years
      @waltinator additional info added to question, thanks for the suggestion
    • Admin
      Admin about 10 years
      @kvm-user420 can you please upload your conf files. It doesn't work for me and my networking script was a bit different (in Ubuntu 14.04).
    • Philippe Paré
      Philippe Paré over 8 years
      FWIW on 15.04 sudo service networking restart seems to work OK for me...
  • Julia Anne Jacobs
    Julia Anne Jacobs about 10 years
    Make sure you run 'sudo ifdown eth0 && sudo ifup eth0' (note the second sudo) if you're not running this command as root
  • meawoppl
    meawoppl almost 10 years
    Is this ok to do if connected via ssh? Ie, will both commands run before the disconnect?
  • u2n
    u2n over 9 years
    Accepted answer didn't work for me; this one did. (Fresh install of 14.04.1)
  • gerlos
    gerlos over 9 years
    Can confirm that this works for me on Trusty (Ubuntu 14.04).
  • dieuwe
    dieuwe over 9 years
    The "ifdown/ifup" answer did not work for me either on Ubuntu 14.04, but this did.
  • Swiss
    Swiss over 9 years
    The accepted answer is applicable for Ubuntu 12.04 and lower. This answer is acceptable for 14.04.
  • 0xF2
    0xF2 over 9 years
    the wisdom of modifying a core OS behavior is debatable.
  • Dig
    Dig over 9 years
    Yes I am using 14.04.1 and the accepted answer did NOT work for me either. The good news is that 'sudo service network-manager restart' works a treat for me. Thank you all involved.
  • Andrew
    Andrew over 9 years
    This worked for me. Hopefully, one day Upstart will just go away ;-)
  • polarise
    polarise about 9 years
    In my case, running sudo service networking restart seemed to have messed things up. So I had to run sudo ifdown eth0 && sudo ifup eth0 twice. Yes, twice.
  • Xdg
    Xdg about 9 years
    @ExceptionSlayer: No, they didn't work for me. I had to power down server physically and turn it on again (Ubuntu 14.04).
  • YahyaE
    YahyaE about 9 years
    It worked on 14.04 LTS.
  • Isaac
    Isaac over 8 years
    Isn't using a ; instead of && a safer option?
  • flickerfly
    flickerfly over 8 years
    Why did they remove the ability to restart? I'd like to know that before reinstating it.
  • flickerfly
    flickerfly over 8 years
    Presence of network-manager depends on your install version. I believe the server version doesn't have it, but the desktop one does. They are very different approaches to network configuration.
  • flickerfly
    flickerfly over 8 years
    That bug report is quite a read! Sounds like there are a notable amount of cases not easily satisfied by this. Thanks
  • Nam G VU
    Nam G VU over 8 years
    Not work for 15.10
  • Dan
    Dan about 8 years
    This unfortunately doesn't work from Ansible, the first ifdown kills the connection and the 2nd command is never run. Borrowing from a stack overflow answer, executing the command in a separate shell will work, e.g. command: bash -c "ifdown eth0 && ifup eth0". Source: stackoverflow.com/questions/28715003/…
  • ihadanny
    ihadanny almost 8 years
    worked for me on 15.10\
  • Dale Anderson
    Dale Anderson over 7 years
    @0xF2 - you mean questionable.
  • 0xF2
    0xF2 over 7 years
    @DaleAnderson that too ;-)