VMware:虚拟机无法上网的问题
大概的问题如题所示,即:通过VMware建立的一个虚拟机无法访问外部网络。
而造成这个问题的原因,其实是有很多的。
在这里,主要描述下我当下环境里遇到的问题,及其解决方法。以便于为日后再次遇到类似问题的时候提供一个可行的思考方向与解决方案。
环境说明:
VMware版本:VMware workstation Pro 12
操作系统发行版及其版本:RHEL5 64Bit
虚拟机 – 虚拟网卡类型:NAT
虚拟机的网络IP:192.168.184.130
物理机的网络IP:192.168.184.1
物理机防火墙状态:关闭
问题状态:
相关命令:
1. (Linux)ping -c
2. (MS Windows)ping -n
就像截图中看到的那样:
1. 物理机是可以访问外部网站的
2. 物理机是可以访问虚拟机的
3. 虚拟机可以访问物理机
但是:
1. 虚拟机无法访问外部网站
这里,造成该问题的原因是因为虚拟机的网卡配置中没有添加“Gateway”:
1 2 3 4 5 6 7 8 9 10 11 |
[root@oracle-db ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 # Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) DEVICE=eth0 BOOTPROTO=static DHCPCLASS= HWADDR=00:0C:29:32:F8:92 ONBOOT=yes DHCP_HOSTNAME=oracle-db IPADDR=192.168.184.130 NETMASK=255.255.255.0 [root@oracle-db ~]# |
当前的虚拟机的网络环境也没有网关的说明:
1 2 3 4 5 6 |
[root@oracle-db ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.184.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 [root@oracle-db ~]# |
解决办法:
1.确定虚拟机的网关
因为,当前虚拟机的网卡配置是“NAT”,所以,对应的真实的虚拟网卡是“VMnet8”。
通过“VMnet8”的“NAT设置”,可以看到当前的VMware虚拟环境中的网关是:192.168.184.2。
2.为虚拟机设置网关:
修改网卡的配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[root@oracle-db ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 # Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) DEVICE=eth0 BOOTPROTO=static DHCPCLASS= HWADDR=00:0C:29:32:F8:92 ONBOOT=yes DHCP_HOSTNAME=oracle-db IPADDR=192.168.184.130 NETMASK=255.255.255.0 # Add Gateway setting GATEWAY=192.168.184.2 [root@oracle-db ~]# |
如上,增加了“GATEWAY”的说明。
重启网络服务:
1 2 3 4 5 6 |
[root@oracle-db ~]# service network restart Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: [ OK ] [root@oracle-db ~]# |
然后,再次查看网关的信息:
1 2 3 4 5 6 7 |
[root@oracle-db ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.184.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 192.168.184.2 0.0.0.0 UG 0 0 0 eth0 [root@oracle-db ~]# |
你可以看到,网关“192.168.184.2”添加成功。
3.再次尝试访问外网:
1 2 3 4 5 6 7 8 9 |
[root@oracle-db ~]# ping -c 2 baidu.com PING baidu.com (123.125.114.144) 56(84) bytes of data. 64 bytes from 123.125.114.144: icmp_seq=1 ttl=128 time=32.0 ms 64 bytes from 123.125.114.144: icmp_seq=2 ttl=128 time=31.9 ms --- baidu.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 31.997/32.004/32.011/0.007 ms [root@oracle-db ~]# |
不出意外,就可以正常的访问外网了。
————————
Done。