常常都被虚拟机的网络折腾得颠三倒四,做点笔记吧。问题:选择NAT后并配置好IP和NetMask之后,可以ping通主机,但连不通Internet。解决:手动配置默认网关。步骤:在虚拟网络编辑器里,找到网关配置,这里是192.168.171.2 。在虚拟机设置里,确认选择NAT。IP和NetMask已经配置:[root@localhost ~]# ifconfig ens33ens33: 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.1PING 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 ms64 bytes from 192.168.171.1: icmp_seq=2 ttl=128 time=0.318 ms64 bytes from 192.168.171.1: icmp_seq=3 ttl=128 time=0.348 ms64 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 3001msrtt min/avg/max/mdev = 0.262/0.336/0.416/0.055 ms[root@localhost ~]# ping 8.8.8.8connect: Network is unreachable检查路由表:[root@localhost ~]# netstat -nrKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.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 -nrKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface0.0.0.0 192.168.171.2 0.0.0.0 UG 0 0 0 ens33192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.168.171.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33再次尝试:[root@localhost ~]# ping 8.8.8.8PING 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 ms64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=26.5 ms64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=22.5 ms64 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 3007msrtt 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 -nrKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.168.171.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33[root@localhost ~]# ping 8.8.8.8connect: Network is unreachable参考链接: https://www.freebsd.org/doc/hhttps://unix.stackexchange.co