手動配置網關,解決VMWare虛擬機內能夠訪問主機但沒法訪問互聯網的問題

經常都被虛擬機的網絡折騰得顛三倒四,作點筆記吧。html

問題:選擇NAT後並配置好IP和NetMask以後,能夠ping通主機,但連不通Internet。網絡

解決:手動配置默認網關。編輯器

步驟spa

  1. 在虛擬網絡編輯器裏,找到網關配置,這裏是192.168.171.2

    虛擬網絡編輯器

  2. 在虛擬機設置裏,確認選擇NAT。

    虛擬機設置

  3. IP和NetMask已經配置:unix

    [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
  4. 能夠Ping通主機,但沒法鏈接Internet:code

    [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
  5. 檢查路由表:htm

    [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
  6. 手動添加網關:路由

    [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
  7. 再次嘗試:get

    [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
  8. 刪除剛剛添加的網關,重現以前的錯誤:虛擬機

    [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...

相關文章
相關標籤/搜索