Ubuntu VM虚拟机 Bridge方式 配置静态IP

其实网上关于这个的教程已经大把了,顺手备份几个:

虚拟网络 桥接、NAT、host-only 介绍,以及桥接方式中主机的bridge protocol设置:http://blog.csdn.net/youxin2012/article/details/17231149

桥接方式:http://blog.csdn.net/yuhaibin168/article/details/7525413

NAT方式(里面顺便介绍了一下VM的虚拟网络管理工具:Virtual Network Manager) : http://www.cnblogs.com/lanxuezaipiao/p/3613497.html


然后再说说我自己看完上述教程后,遇到的仍然没有解决的问题:

本人一开始对 /etc/network/interfaces 做的修改如下:

  1. #The loopback network interface  
  2. auto lo  
  3. iface lo inet loopback  
  4.   
  5. #The primary network interface  
  6. auto eth0  
  7. iface eth0 inet static #dhcp  
  8. address 172.21.5.41  
  9. netmask 255.255.255.0  
  10. gateway 172.21.5.254  

其中在由dhcp模式改为静态ip模式时,为了备份自己做的修改,将系统默认的dhcp直接在代码行末尾注释掉了。

恩,看起来好像没什么问题,和网上教程中描述的配置是一样的。

结果通过 /etc/init.d/networking restart 重启网络服务时,ip还是无法分配到指定的静态IP。

尝试重启Ubuntu系统,结果在系统启动时报错:Starting configure network device [failed]

百思不得其解。


最后尝试用 ifdown ifup命令手动关闭重启 eth0 网络设备。

结果报错:

/etc/network/interfaces:11: too many parameters for iface line
ifdown: couldn't read interfaces file "/etc/network/interfaces"


查看一下/etc/network/interfaces 11行的内容:

  1. iface eth0 inet static #dhcp  

真相大白。应该是行尾末端的#dhcp注释前的空格被iface读入,导致iface以为static后面还要有参数输入(猜测,未证实)。

将#dhcp移至11行上方,interfaces文件改为:

  1. #The loopback network interface  
  2. auto lo  
  3. iface lo inet loopback  
  4.   
  5. #The primary network interface  
  6. auto eth0  
  1. <pre name="code" class="plain">#iface eth0 inet dhcp  
iface eth0 inet staticaddress 172.21.5.41netmask 255.255.255.0gateway 172.21.5.254

重启网络服务,妥妥的~

后记:这次bug也提醒了我,在脚本中,好的注释习惯是在目标代码行的上方添加一行注释行,而非直接在目标代码行的末尾添加注释。

===150414更新===

今天为实验室机房另一台ubuntu服务器配置静态IP。想起之前接触的一位工程师说网络服务配置后不需要重启机器,深以为然,决定试试。

但是,问题来了,在对/etc/network/interfaces 修改后,sudo /etc/init.d/networking restart 报错:

 stop: Job failed while stopping

网上查了一下,原来Ubuntu14.04坑爹的停止了 对 /etc/init.d/networking restart 的支持:https://bugs.launchpad.net/ubuntu/+source/ifupdown/+bug/1301015

尽管好多人在这个bug下面吐槽,要求Ubuntu提供一个更好的解决办法,但是,现在的情况是只能通过 sudo ifdown --- && sudo ifup --- 进行相应网络设备的重启。(叹气)

==== 附录更新 ====

其他觉得很有帮助的文章:

【0】http://www.linuxquestions.org/questions/linux-newbie-8/etc-network-interfaces-too-many-parameters-for-iface-line-4175491064/

个人是通过下面这段话理解了eth0的含义,以及iface命令中相关参数的意义:

Stanzas defining logical interfaces start with a line consisting of the word "iface" followed by the name of the logical interface. In simple configurations without mapping stanzas this name should simply be the name of the physical interface to which it is to be applied. (The default mapping script is, in effect, <span class="IL_AD" id="IL_AD5">the echo</span> command.) The interface name is followed by the name of the <span class="IL_AD" id="IL_AD10">address family</span> that the interface uses. This will be "inet" for TCP/IP networking, but there is also some support for IPX networking ("ipx"), and IPv6 networking ("inet6"). Following that is the name of the <span class="IL_AD" id="IL_AD3">method</span> used to configure the interface.

Additional options can be given on subsequent lines in the stanza. Which options are available depends on the family and method, as described below. Additional options can be made available by other Debian packages.

iface 网络设备ID 协议族 其他选项


【1】理解Linux系统/etc/init.d目录和/etc/rc.local脚本 http://blog.csdn.net/acs713/article/details/7322082

【2】ubuntu静态DNS配置,重启继续生效 http://blog.sina.com.cn/s/blog_67be3b450101de45.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dxmcu

谢谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值