ubuntu 网络设置

原文 http://www.felix021.com/blog/read.php?2140
我大约从2010年起,就一直在自己的机器上使用 Windows宿主机+Ubuntu Server@VBox虚拟机 这种组合 ,一方面不用抛弃windows上早已熟悉的众多GUI软件,另一方面又可以享受到Linux带来的便利,在上面做开发等等。虽然仅仅用一个NAT就可以解决网络的问题,但是如果每次新增一个服务就要添加一个端口映射也很麻烦,所以我开了双网卡,另一个使用Host-Only,这样主机和宿主机之间可以直接互访。
虽然有人说用Bridged Network也能解决这个问题,但是前述方法却有更多好处:首先因为是外网无法直接访问这台机器,所以可以使用弱密码;其次虚拟机里使用NAT通过宿主机访问外部网络,因此像我现在主机上的双网卡的目标网络也可以免配置直接访问。
不过昨天遇到了个问题(貌似以前也曾经遇到过),就是突然不能访问外网了(但是仍然能ping通192.168.56.1,即宿主机的Host-Only IP),经过测试发现如果把Host-Only的网卡去掉就没问题,所以看起来像是这两个网络冲突了。
经过放狗搜索, SuperUser上的一个问题 提醒我,这个其实是路由表的问题,由于没有指定默认网关,因此不知道为什么Ubuntu(WinXP也会)就把Host-Only的gateway当成默认网关了
解决问题很简单,先删掉错误的默认网关,再添加新的默认网关:
$ sudo route del default
$ sudo route add default gw 10.0.2.2
不过这个重启以后就会消失,需要保持的话,就在 /etc/network/interfaces 添加一行
up sudo route add default gw 10.0.2.2

可以在/etc/network/interfaces文件中添加静态路由

经测试,本人使用如下方法1:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet dhcp
up route add default gw 10.0.2.2 dev eth0
#The primary network interface
auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0
#把eth1的gateway注销掉,防止影响 eth0的默认路由
#gateway 192.168.56.1 #up route add -net 192.168.56.0/24 gw 192.168.56.254 dev eth1 #The primary network interface

 

 


example:
#static route
up route add default gw 192.168.1.1 dev eth0
up route add -net 192.168.1.0/24 gw 192.168.1.1 dev eth1

 

如果虚拟机网络失效,则把网络的默认路由都删除,添加一条默认路由,可以实现上网。

 

方法2:

第一块网卡为nat

第二块网卡为host-only,注意第一和第二块网卡的位置不能换

# The loopback network interface
auto lo
iface lo inet loopback
#eth0
auto eht0
iface eth0 inet dhcp
up route add default gw 10.0.2.2 dev eth0
#eth1
auto eth1
iface eth1 inet static
address 10.20.0.200
netmask 255.255.255.0
gateway 10.20.0.2

 VMware网卡设置

posted @ 2015-11-18 10:31  剑风云  阅读(236)  评论(0编辑  收藏  举报