在他人博客的帮助下。
目前情况为:
我不知道哪个是默认路由。应该是Destination为0.0.0.0的为默认路由吧
虚拟机:virtualbox
系统:centos 6.5
问题:虚拟机有2块网卡,NAT+ host-only, 可以ping通 192.168.56.1网关,ping不通宿主机,宿主机可以ping通虚拟机
原因:在路由表中,默认网关被设置成了192.168.56.1(而非10.0.2.2)
解决:删除网卡配置文件中的“GATEWAY=x.x.x.x”即可
具体:1. 确认问题,输入命令 route,结果中 Destination 为 default 项的 Gateway的值为"10.0.2.2"以外的值,且Flags为“UG”2. 删除2块网卡配置文件/etc/sysconfig/network-scripts/ifcfg-ethX 中的“GATEWAY=x.x.x.x”3. 在/etc/sysconfig/network文件中加入“GATEWAY=10.0.2.2”(可省略,如果第二步的配置文件中不存GATEWAY=x.x.x.x)
如果在第二步中不存在GATEWAY=x.x.x.x,也可以做第三步
重启虚拟机
可以ping通外网。
以上,解决方法也有可能是只是重启虚拟机就可以了,不需要多余操作
经过实验,好像确实只重启虚拟机就可以了。
知识补充:
连接外网
如何获取这个外网网卡的网关地址呢?virtualbox如下规定:
In NAT mode, the guest network interface is assigned to the IPv4 range 10.0.x.0/24 by default where x corresponds to the instance of the NAT interface +2. So x is 2 when there is only one NAT instance active. In that case the guest is assigned to the address 10.0.2.15, the gateway is set to 10.0.2.2 and the name server can be found at 10.0.2.3.
(https://www.virtualbox.org/manual/ch09.html#changenat)
使用nat网卡ping外网,hostonly网卡ping物理机
虚拟机ping自己要ping hostonly网卡的ip
要ping通的话要注意虚拟机以及物理机设置的映射。(hosts)