如何通过 SSH 成功重启网络而无需重启?

如何通过 SSH 成功重启网络而无需重启?

在 Ubuntu 14.04 中,它们都不再sudo service networking restart执行sudo /etc/init.d/networking restart任何操作。它们也都以代码 1 退出。显然有些东西已经改变(或改变了一半),但我找不到是什么。这显然会导致远程网络重新配置和 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

远程重启 Ubuntu 14.04 Server 中的网络的正确方法是什么?

答案1

事实证明,这是一个“功能”。仅有的在 Ubuntu Server 中支持重新启动接口的方式是sudo ifdown eth0 && sudo ifup eth0

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

答案2

ifdown、ifup 对我来说不起作用(可能是第二条命令之前 SSH 连接超时)。起作用的是:

sudo service network-manager restart

这是在 14.04 ubuntu-desktop 系统上。

答案3

变化的是他们不再希望你“反弹”网络。停止和启动仍然有效。重新启动不再有效。我刚刚“解决”了这个“问题”,也就是说我恢复了旧的行为。要恢复到以前的行为:获取 13.10 /etc/init/networking.conf 文件并用它替换 14.04 文件。(编辑:澄清哪个替换哪个)

该过程如下:

(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#  

对 /etc/init.d/networking 脚本执行相同操作,这是 /etc/init/networking.conf 文件引用/调用的脚本。

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). 

显然,他们在那里设置防守出口是有原因的,但他们并没有费心去真正地输出正在发生的事情。

当您尝试时,/var/log/upstart/networking.log 中会进入一个条目,如下所示:

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

但是当您尝试重新启动服务网络时,他们确实可以/应该将其作为对话框消息输出。啊,好吧。弄清楚了,甚至用老方法解决了问题。

编辑:我发现这会导致无意中触发由 /etc/init/failsafe.conf 控制的脚本,这是不可取的,因为它会导致每次启动时出现 120 秒的超时延迟……以及可能掩盖实际的错误配置/网络问题,这种延迟的出现会表明,但它已经一直显示。(例如,拔掉一根电缆,允许访问 /etc/fstab 中映射的网络文件共享)

无论如何,我会找出导致这个问题总是超时的原因,并在找到原因后发布修复程序。

答案4

我修复了此脚本的问题:请在“/etc/network/if-down.d”上添加此脚本

cd /etc/network/if-down.d
vim ifdown

内容 :

#!/bin/bash 

for I in /sys/class/net/* 
do
        ifname=$(basename $I) 
        if [ $ifname != "lo" ] ; then 
                ip addr flush $ifname
        fi
done

最后:

chmod +x ifdown

现在您可以使用 systemctl 或 service 命令更改 ip 地址并重新启动网络服务。
笔记:这个脚本太简单了,请勿触碰vm、tap、bridge、... 接口。实际上刷新所有接口(不包括 lo(环回)。

相关内容