在虚拟机上运行的 Centos 7 如何设置默认网关 - ping google.com 失败

在虚拟机上运行的 Centos 7 如何设置默认网关 - ping google.com 失败

我正在使用在 Oracle VM VirtualBox Manager 中运行的 Centos 7。我希望我的 Windows(主机)能够 ping Centos 7(来宾),因此我更改了虚拟机中的网络设置以使用仅主机网络适配器,该适配器运行良好,因为我的 Windows 可以 ping 虚拟机 IP,但虚拟机没有网络连接。好像是默认网关的问题,请问如何设置默认网关?默认网关的正确值是多少?这样虚拟机就有可用的网络连接。

没有默认路由和DNS的网络截图

[root@hostname sysconfig]# ifconfig -a
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:48ff:fe73:32ae  prefixlen 64  scopeid 0x20<link>
        ether 02:42:48:73:32:ae  txqueuelen 0  (Ethernet)
        RX packets 1086312  bytes 190220822 (181.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2244812  bytes 1153561488 (1.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.101  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fe57:e658  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:57:e6:58  txqueuelen 1000  (Ethernet)
        RX packets 1083  bytes 112900 (110.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 166  bytes 24915 (24.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 70229  bytes 8733878 (8.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 70229  bytes 8733878 (8.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth0022793: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::541a:3bff:fefe:40fc  prefixlen 64  scopeid 0x20<link>
        ether 56:1a:3b:fe:40:fc  txqueuelen 0  (Ethernet)
        RX packets 206  bytes 15300 (14.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5850  bytes 272760 (266.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth0333914: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 16:78:ed:04:52:5e  txqueuelen 0  (Ethernet)
        RX packets 68480  bytes 84562622 (80.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 90298  bytes 14502209 (13.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth06f16da: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::4aa:47ff:fe36:c394  prefixlen 64  scopeid 0x20<link>
        ether 06:aa:47:36:c3:94  txqueuelen 0  (Ethernet)
        RX packets 51  bytes 4678 (4.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5823  bytes 267358 (261.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth16e581d: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::2cb7:66ff:fe1a:a627  prefixlen 64  scopeid 0x20<link>
        ether 2e:b7:66:1a:a6:27  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5769  bytes 262582 (256.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth1ecf198: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::2022:c4ff:fe1c:9ab5  prefixlen 64  scopeid 0x20<link>
        ether 22:22:c4:1c:9a:b5  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5773  bytes 262882 (256.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth8536f91: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::ac21:2fff:fe0e:87e6  prefixlen 64  scopeid 0x20<link>
        ether ae:21:2f:0e:87:e6  txqueuelen 0  (Ethernet)
        RX packets 841994  bytes 71927871 (68.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1841996  bytes 992110236 (946.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethda88d72: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::f44c:52ff:fe6c:b856  prefixlen 64  scopeid 0x20<link>
        ether f6:4c:52:6c:b8:56  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5759  bytes 261746 (255.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:ce:19:b8  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

virbr0-nic: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:ce:19:b8  txqueuelen 500  (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


[root@hostname network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3 
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s3
UUID=dc80dadc-3072-428f-888f-25dc8dcd8ca9
DEVICE=enp0s3
ONBOOT=no
[root@hostname network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-lo
DEVICE=lo
IPADDR=127.0.0.1
NETMASK=255.0.0.0
NETWORK=127.0.0.0

[root@hostname network-scripts]# cat /etc/resolv.conf 
# Generated by NetworkManager


# No nameservers found; try putting DNS servers into your
# ifcfg files in /etc/sysconfig/network-scripts like so:
#
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com


[root@hostname sysconfig]# ip route show
172.17.0.0/16 dev docker0  proto kernel  scope link  src 172.17.0.1 
192.168.56.0/24 dev enp0s3  proto kernel  scope link  src 192.168.56.101  metric 100 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
[root@hostname sysconfig]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@hostname sysconfig]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.56.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@hostname sysconfig]# ifconfig enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.101  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fe57:e658  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:57:e6:58  txqueuelen 1000  (Ethernet)
        RX packets 1070  bytes 110928 (108.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 164  bytes 24513 (23.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@hostname sysconfig]# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0
192.168.56.0    0.0.0.0         255.255.255.0   U         0 0          0 enp0s3
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0

[root@hostname sysconfig]# ip route list
172.17.0.0/16 dev docker0  proto kernel  scope link  src 172.17.0.1 
192.168.56.0/24 dev enp0s3  proto kernel  scope link  src 192.168.56.101  metric 100 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 

[root@hostname sysconfig]# ping google.com
ping: unknown host google.com
[root@hostname sysconfig]# nslookup google.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached

答案1

这是预期的行为。使用仅主机顾名思义,网络仅允许主机和虚拟机之间或同一主机上的虚拟机之间的连接。手册中写道:“……虚拟机无法与主机外部的世界通信,因为它们没有连接到物理网络接口。”

如果您希望能够建立从主机到访客的网络连接从访客到外部世界,您可能需要一个桥接网络适配器。

相关内容