客机

客机

我从 Mac 导出了 VirtualBox VM(Debian 7.11),并将其导入到我的 Linux 机器(Ubuntu 17.04)上。一切似乎都运行正常,只是我导入的机器无法连接到互联网。

(我猜我没有在导入时更改 MAC 地址,因为出于某种原因我没有进一步调查而失败了。)

当前设置如下:

  • VirtualBox 配置为具有“NatNetwork”和“仅主机网络”

  • 对于虚拟机,两个网络适配器均处于活动状态并且电缆已连接。

  • 在主机和虚拟机内有一个 OpenVPN 配置(两台机器上相同)。但目前尚未激活。

客机

# /etc/init.d/openvpn status
[FAIL] VPN 'vpn' is not running ... failed!

# ifconfig 
eth0      Link encap:Ethernet  HWaddr 08:00:27:e7:5d:aa  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fee7:5daa/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:357 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1830 (1.7 KiB)  TX bytes:21368 (20.8 KiB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:cb:4b:40  
          inet addr:192.168.56.120  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fecb:4b40/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3466 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3228 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2247873 (2.1 MiB)  TX bytes:638995 (624.0 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:659 errors:0 dropped:0 overruns:0 frame:0
          TX packets:659 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:49216 (48.0 KiB)  TX bytes:49216 (48.0 KiB)

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.1        0.0.0.0         UG    0      0        0 eth0
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

# iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT

现在进行测试:

# ping -c 1 192.168.56.1
PING 192.168.56.1 (192.168.56.1) 56(84) bytes of data.
64 bytes from 192.168.56.1: icmp_req=1 ttl=64 time=0.831 ms

--- 192.168.56.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.831/0.831/0.831/0.000 ms

# wget 172.217.21.206 # google.com
--2017-05-25 17:31:28--  http://172.217.21.206/
Connecting to 172.217.21.206:80... failed: No route to host.

主机

# /etc/init.d/openvpn status
● openvpn.service - OpenVPN service
   Loaded: loaded (/lib/systemd/system/openvpn.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2017-05-25 17:33:12 CEST; 3s ago
 Main PID: 1280 (code=exited, status=0/SUCCESS)

# ifconfig 
enp0s31f6: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 54:ee:75:c6:13:46  txqueuelen 1000  (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
        device interrupt 16  memory 0xf1200000-f1220000  

enx0050b6dfa166: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.178.26  netmask 255.255.255.0  broadcast 192.168.178.255
        inet6 fe80::1682:e4a2:aca4:9424  prefixlen 64  scopeid 0x20<link>
        ether 00:50:b6:df:a1:66  txqueuelen 1000  (Ethernet)
        RX packets 1310132  bytes 1599207507 (1.5 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 840018  bytes 90022576 (90.0 MB)
        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 1000  (Local Loopback)
        RX packets 7627  bytes 379131 (379.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7627  bytes 379131 (379.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vboxnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.1  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::800:27ff:fe00:0  prefixlen 64  scopeid 0x20<link>
        ether 0a:00:27:00:00:00  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3557  bytes 299336 (299.3 KB)
        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:1d:1b:76  txqueuelen 1000  (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

wlp4s0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 06:eb:85:c3:6b:3a  txqueuelen 1000  (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

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.178.1   0.0.0.0         UG    100    0        0 enx0050b6dfa166
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enx0050b6dfa166
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 vboxnet0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.178.0   0.0.0.0         255.255.255.0   U     100    0        0 enx0050b6dfa166

# iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -i virbr0 -p udp -m udp --dport 53 -j ACCEPT
-A INPUT -i virbr0 -p tcp -m tcp --dport 53 -j ACCEPT
-A INPUT -i virbr0 -p udp -m udp --dport 67 -j ACCEPT
-A INPUT -i virbr0 -p tcp -m tcp --dport 67 -j ACCEPT
-A FORWARD -d 192.168.122.0/24 -o virbr0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -s 192.168.122.0/24 -i virbr0 -j ACCEPT
-A FORWARD -i virbr0 -o virbr0 -j ACCEPT
-A OUTPUT -o virbr0 -p udp -m udp --dport 68 -j ACCEPT

# cat /proc/sys/net/ipv4/ip_forward
1

测试如下:

# ping -c1 192.168.56.120
PING 192.168.56.120 (192.168.56.120) 56(84) bytes of data.
64 bytes from 192.168.56.120: icmp_seq=1 ttl=64 time=1.66 ms

--- 192.168.56.120 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.664/1.664/1.664/0.000 ms

# wget 172.217.21.206 # google.com
--2017-05-25 17:39:15--  http://172.217.21.206/
Connecting to 172.217.21.206:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://www.google.com/ [following]
--2017-05-25 17:39:15--  http://www.google.com/
Resolving www.google.com (www.google.com)... 172.217.22.196
Connecting to www.google.com (www.google.com)|172.217.22.196|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://www.google.de/?gfe_rd=cr&ei=o_omWdH5KefVXp7imGA [following]
--2017-05-25 17:39:15--  http://www.google.de/?gfe_rd=cr&ei=o_omWdH5KefVXp7imGA
Resolving www.google.de (www.google.de)... 216.58.207.131
Connecting to www.google.de (www.google.de)|216.58.207.131|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html’

index.html                        [ <=>                                            ]  11,05K  --.-KB/s    in 0,003s  

2017-05-25 17:39:15 (4,11 MB/s) - ‘index.html’ saved [11316]

因此,主机上可以正常访问互联网,并且可以通过网络访问客户机。(我甚至可以从主机 ssh 进入虚拟机。)

我另外尝试了:

  • iptables -F在主机上

我遗漏了什么?在 Mac 机器上,它可以正常工作(在同一网络中)。

iptables 配置中是否缺少某些内容?或者应该由 VirtualBox 本身处理?

答案1

在发布并重新连接 Linux VDI 以进行备份后,我遇到了同样的问题。可以在 VB 会话中 ping 外部站点,但无法加载页面。尝试了网络适配器的多种配置(NAT/桥接)。

最后,通过禁用适配器 1 并启用适配器 2(作为桥接)使其工作,效果很好。

答案2

TLDR:您需要更改 DNS 设置

如果你有cat /etc/resolv.conf如下输出:

# Generated by NetworkManager
nameserver ::1

然后更改你的 DNS 设置:运行sudo nano /etc/resolv.conf

并在文件中添加文本:

nameserver 1.1.1.1
nameserver 1.0.0.1
nameserver 8.8.8.8
nameserver 8.8.4.4

相关内容