常常都被虚拟机的网络折腾得颠三倒四,做点笔记吧。
问题:选择NAT后并配置好IP和NetMask之后,可以ping通主机,但连不通Internet。
解决:手动配置默认网关。
步骤:
- 在虚拟网络编辑器里,找到网关配置,这里是
192.168.171.2
。 - 在虚拟机设置里,确认选择NAT。
-
IP和NetMask已经配置:
[root@localhost ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.171.128 netmask 255.255.255.0 broadcast 192.168.171.255 inet6 fe80::20c:29ff:feba:ba7c prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ba:ba:7c txqueuelen 1000 (Ethernet) RX packets 281023 bytes 361357640 (344.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 68897 bytes 14016800 (13.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-
可以Ping通主机,但无法连接Internet:
[root@localhost ~]# ping 192.168.171.1 PING 192.168.171.1 (192.168.171.1) 56(84) bytes of data. 64 bytes from 192.168.171.1: icmp_seq=1 ttl=128 time=0.416 ms 64 bytes from 192.168.171.1: icmp_seq=2 ttl=128 time=0.318 ms 64 bytes from 192.168.171.1: icmp_seq=3 ttl=128 time=0.348 ms 64 bytes from 192.168.171.1: icmp_seq=4 ttl=128 time=0.262 ms ^C --- 192.168.171.1 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3001ms rtt min/avg/max/mdev = 0.262/0.336/0.416/0.055 ms [root@localhost ~]# ping 8.8.8.8 connect: Network is unreachable
-
检查路由表:
[root@localhost ~]# netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 192.168.171.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
-
手动添加网关:
[root@localhost ~]# route add default gw 192.168.171.2 [root@localhost ~]# netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.171.2 0.0.0.0 UG 0 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 192.168.171.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
-
再次尝试:
[root@localhost ~]# ping 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=23.4 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=26.5 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=22.5 ms 64 bytes from 8.8.8.8: icmp_seq=4 ttl=128 time=29.2 ms ^C --- 8.8.8.8 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3007ms rtt min/avg/max/mdev = 22.524/25.448/29.255/2.666 ms
-
删除刚刚添加的网关,重现之前的错误:
[root@localhost ~]# route del default gw 192.168.171.2 [root@localhost ~]# netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 192.168.171.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 [root@localhost ~]# ping 8.8.8.8 connect: Network is unreachable
参考链接:
https://www.freebsd.org/doc/h...
https://unix.stackexchange.co...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。