Hugh's Blog

虚拟机下无法访问宿主机

 

开发环境一直都是 Hyper-V 下虚拟机,但是最近不知道为什么,虚拟机无法访问宿主机,如虚拟机 IP 段为 237,在虚拟机内无法访问 128 或者 137 等 IP 段。

最后发现是某个网络接口的问题,停用该接口就行了。

一般某台主机无法 ping 的话,有两种情况:Request Timed OutDestination Host Unreachable,这里是第二种情况。

hugh@us1804:~/tmp$ ping 192.168.137.2
PING 192.168.137.2 (192.168.137.2) 56(84) bytes of data.
From 192.168.0.1 icmp_seq=1 Destination Host Unreachable
From 192.168.0.1 icmp_seq=2 Destination Host Unreachable

超时一般是没找到主机无响应,第二种有可能是路由器配置的问题。

查看 arp 表

hugh@us1804:~/tmp$ arp -a
? (172.17.0.3) at 02:42:ac:11:00:03 [ether] on docker0
? (172.17.0.4) at 02:42:ac:11:00:04 [ether] on docker0
? (192.168.137.2) at <incomplete> on br-7632662dcf1b
? (192.168.237.1) at 00:15:5d:80:37:03 [ether] on eth0

可以看到 192.168.137.2 并没有记录到主机的 mac 地址,此时使用的网络接口为 br-7632662dcf1b。

再查看路由表,route -n 或者 netstat -r

hugh@us1804:~/tmp$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.237.1   0.0.0.0         UG    0      0        0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.0.0     0.0.0.0         255.255.0.0     U     0      0        0 br-7632662dcf1b
192.168.237.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0

可以看到,192.168 目标的所有 IP 都需要通过 br-7632662dcf1 这个网络接口,查看该网卡信息:

hugh@us1804:~/tmp$ ifconfig br-7632662dcf1b
br-7632662dcf1b: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.0.1  netmask 255.255.0.0  broadcast 192.168.255.255
        ether 02:42:23:ae:be:38  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

不清楚为什么会出现这样的网卡,关掉就行:

sudo ifconfig br-7632662dcf1b down

停用之后,就能正常访问了。

hugh@us1804:~/tmp$ ping 192.168.137.2
PING 192.168.137.2 (192.168.137.2) 56(84) bytes of data.
64 bytes from 192.168.137.2: icmp_seq=1 ttl=60 time=1.03 ms
64 bytes from 192.168.137.2: icmp_seq=2 ttl=60 time=1.19 ms

查看所有网卡信息(包括停用):

hugh@us1804:~/tmp$ ifconfig -a
br-7632662dcf1b: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.0.1  netmask 255.255.0.0  broadcast 192.168.255.255
        ether 02:42:23:ae:be:38  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:3cff:fe13:30a  prefixlen 64  scopeid 0x20<link>
        ether 02:42:3c:13:03:0a  txqueuelen 0  (Ethernet)
        RX packets 26454  bytes 41621130 (41.6 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27011  bytes 39964067 (39.9 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

参考

ping response Destination Host unreachable