Blog·Tanky WooABOUTRSS


04 Jun 2014

关于 Ubuntu 12.04 Server 下重启网络:

root@ubuntu:~# sudo /etc/init.d/networking restart
 * Running /etc/init.d/networking restart is deprecated because it may not enable again some interfaces
 * Reconfiguring network interfaces...                                                                                                                                              ssh stop/waiting
ssh start/running, process 1697                        [ OK ]

root@ubuntu:~# service networking restart
stop: Unknown instance:
networking stop/waiting

如果用 /etc/init.d/xxx 重启,都会提示这个方式是过时的,但是使用 service xxx restart 方式又不行。


最简单的方式就是使用 ifupifdown:

ifdown eth0 && ifup eth0


nohup sh -c "ifdown eth0 && ifup eth0 "



service network-manager restart

不确定 network-manager 是不是在desktop版下自带的,至少默认的server版是没有,还需要安装。

Ubuntu Bugs里也有人提到。

另外想到了 /etc/init.d/xxxservice xxx 这两种方式的区别。

man service :

service runs a System V init script or upstart job

/etc/init.d The directory containing System V init scripts.

/etc/init The directory containing upstart jobs.

service方式可以管理老式的System V启动脚本,即/etc/init.d/下的,也可以管理新式的upstart方式,即/etc/init/ 下。



/etc/init.d scripts are the old way of doing things. They come from the System V standard. However, those scripts are fired only in a particular sequence, no real dependencies can be established.

Therefore, upstart has been developed with the intent to substitute all the /etc/init.d scripts with upstart scripts (in /etc/init).

service allows the smooth transition from /etc/init.d scripts to upstart scripts. When in the future more on more scripts are transferred to upstart, service will still work, because it finds both possibilties.


/etc/init.d contains scripts used by the System V init tools (SysVinit). This is the traditional service management package for Linux, containing the init program (the first process that is run when the kernel has finished initializing¹) as well as some infrastructure to start and stop services and configure them. Specifically, files in /etc/init.d are shell scripts that respond to start, stop, restart, and (when supported) reload commands to manage a particular service. These scripts can be invoked directly or (most commonly) via some other trigger (typically the presence of a symbolic link in /etc/rc?.d/).

/etc/init contains configuration files used by Upstart. Upstart is a young service management package championed by Ubuntu. Files in /etc/init are configuration files telling Upstart how and when to start, stop, reload the configuration, or query the status of a service. As of lucid, Ubuntu is transitioning from SysVinit to Upstart, which explains why many services come with SysVinit scripts even though Upstart configuration files are preferred. In fact, the SysVinit scripts are processed by a compatibility layer in Upstart.

.d in directory names typically indicates a directory containing many configuration files or scripts for a particular situation (e.g. /etc/apt/sources.list.d contains files that are concatenated to make a virtual sources.list; /etc/network/if-up.d contains scripts that are executed when a network interface is activated). This structure is usually used when each entry in the directory is provided by a different source, so that each package can deposit its own plug-in without having to parse a single configuration file to reference itself. In this case, it just happens that “init” is a logical name for the directory, SysVinit came first and used init.d, and Upstart used plain init for a directory with a similar purpose (it would have been more “mainstream”, and perhaps less arrogant, if they'd used /etc/upstart.d instead).


已知的服务管理器有 sysvinitsystemdupstart



root@ubuntu:~# status networking
networking stop/waiting
root@cdn-tpl:/etc/init# which status
root@cdn-tpl:/etc/init# ll /sbin/status
lrwxrwxrwx 1 root root 7 Jan 19  2013 /sbin/status -> initctl*

status命令是initctl的别名,相当于调用initctl status xxx


关于 initctlservice 的区别,我暂时觉得可能就是 service 兼容(管理)了sysvinit的脚本。

另外关于 initctl listservice --status-all 也有区别,man service中有解释:

service --status-all runs all init scripts, in alphabetical order, with the status command. This option only calls status for sysvinit jobs, upstart jobs can be queried in a similar manner with initctl list'.


service --status-all输出的一部分:

 [ ? ]  resolvconf
 [ - ]  rsync
 [ ? ]  rsyslog
 [ ? ]  screen-cleanup
 [ ? ]  sendsigs
 [ ? ]  setvtrgb
 [ + ]  ssh
 [ - ]  stop-bootlogd
 [ - ]  stop-bootlogd-single
 [ ? ]  sudo
 [ ? ]  udev

关于前面 +-?的意义,man/info里都没找到,不过在这个帖子里有人解释了:

"+" started

"-" stopped

"?" unknown


root@ubuntu:~# /etc/init.d/rsync status
 * rsync is not running

root@ubuntu:~# /etc/init.d/ssh status
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service ssh status

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the status(8) utility, e.g. status ssh
ssh start/running, process 1697


lrwxrwxrwx  1 root root   21 Nov 27  2013 rsyslog -> /lib/init/upstart-job*
lrwxrwxrwx  1 root root   21 Jun  7  2011 screen-cleanup -> /lib/init/upstart-job*
-rwxr-xr-x  1 root root 4321 Jul 27  2012 sendsigs*
lrwxrwxrwx  1 root root   21 Apr 20  2012 setvtrgb -> /lib/init/upstart-job*
-rwxr-xr-x  1 root root  590 Jul 27  2012 single*
-rw-r--r--  1 root root 4304 Jul 27  2012 skeleton
-rwxr-xr-x  1 root root 4371 Apr 11  2013 ssh*



root@ubuntu:/etc/init.d# initctl list | grep network
network-interface (lo) start/running
network-interface (eth0) start/running
network-interface-security (network-interface/eth0) start/running
network-interface-security (network-interface/lo) start/running
network-interface-security (networking) start/running
networking stop/waiting
network-interface-container stop/waiting


root@ubuntu:~# status network-interface INTERFACE=eth0
network-interface (eth0) start/running

root@ubuntu:/etc/init.d# service network-interface status INTERFACE=eth0
network-interface (eth0) start/running

所以重启网络用network-interface 也可以:

root@ubuntu:/etc/init.d# service network-interface restart INTERFACE=eth0
network-interface stop/waiting
network-interface (eth0) start/running


接下来要了解upstat具体的管理方式和配置,进一步看看为何service networking restart不行。


刚在微博把这篇文章贴出来,基友 @枯木 就找上我了,他前阵子在14.04下也遇到相关的问题,详见Ubuntu14.04重启网卡不生效