其实网上关于这个的教程已经大把了,顺手备份几个:
虚拟网络 桥接、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 做的修改如下:
#The loopback network interface
auto lo
iface lo inet loopback
#The primary network interface
auto eth0
iface eth0 inet static #dhcp
address 172.21.5.41
netmask 255.255.255.0
gateway 172.21.5.254
恩,看起来好像没什么问题,和网上教程中描述的配置是一样的。
结果通过 /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行的内容:
iface eth0 inet static #dhcp
真相大白。应该是行尾末端的#dhcp注释前的空格被iface读入,导致iface以为static后面还要有参数输入(猜测,未证实)。
将#dhcp移至11行上方,interfaces文件改为:
#The loopback network interface
auto lo
iface lo inet loopback
#The primary network interface
auto eth0
#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, the echo command.) The interface name is followed by the name of the address family 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 method 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