VirtualBox:Ubuntu 上 Debian 来宾中的两个网络接口(NAT 和仅主机接口)

bra*_*zzi 58 networking ubuntu virtual-machine debian virtualbox

我在 VirtualBox 上创建了一个带有两个接口的 Debian VM:一个 NAT(用于访问互联网)和一个仅限主机的接口。但是,我不知道如何让两个界面同时工作。如果我将仅主机定义为适配器 1,则可以从主机访问我的 VM,但不能从 Internet 访问;如果我将 NAT 定义为适配器 1,我可以访问互联网但无法访问我的来宾 Debian。

那么,我怎样才能让两个接口一起工作呢?

注意:我仍在尝试将主机的某个端口映射到来宾 SO 的 SSH 端口,因此无需建议我这样做:)

编辑:这是ifconfig第一个适配器是仅主机适配器时的输出 :

eth0      Link encap:Ethernet  HWaddr 08:00:27:f6:b2:45  
          inet addr:192.168.56.101  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fef6:b245/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:495 errors:0 dropped:0 overruns:0 frame:0
          TX packets:206 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:48187 (47.0 KiB)  TX bytes:38222 (37.3 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:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:560 (560.0 B)  TX bytes:560 (560.0 B)
Run Code Online (Sandbox Code Playgroud)

这是netstat -nr当第一个适配器是仅主机适配器时的输出:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.56.0    0.0.0.0         255.255.255.0   U         0 0          0 eth0
Run Code Online (Sandbox Code Playgroud)

这是ifconfig第一个适配器是NAT时的输出:

eth0      Link encap:Ethernet  HWaddr 08:00:27:f6:b2:45  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fef6:b245/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:53 errors:0 dropped:0 overruns:0 frame:0
          TX packets:59 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:6076 (5.9 KiB)  TX bytes:5526 (5.3 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:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1664 (1.6 KiB)  TX bytes:1664 (1.6 KiB)
Run Code Online (Sandbox Code Playgroud)

这是netstat -nr第一个适配器是NAT时的输出:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
0.0.0.0         10.0.2.2        0.0.0.0         UG        0 0          0 eth0
Run Code Online (Sandbox Code Playgroud)

bra*_*zzi 61

解决方案非常简单:我只需将以下几行添加到Debian 虚拟机/etc/network/interfaces文件中:

allow-hotplug eth1
iface eth1 inet dhcp
Run Code Online (Sandbox Code Playgroud)

第二行指示接口通过 DHCP 获取 IP。第一行在启动时加载接口。

要将更改应用于正在运行的系统,请调用:

ifup eth1
Run Code Online (Sandbox Code Playgroud)

eth1接口的名称可能会有所不同,用于ifconfig -a列出所有可用的接口。

编辑:完整/etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

allow-hotplug eth1
iface eth1 inet dhcp
Run Code Online (Sandbox Code Playgroud)

  • 这很好用,谢谢!然而,Ubuntu 18 现在使用一个名为“netplan”的新界面。知道如何在netplan中复制上述内容吗? (2认同)

Pan*_*til 13

我的 Ubuntu 14.04 VM 遇到了类似的问题,@brandizzi 为 Debian 建议的解决方案几乎没有变化。

EDIT: file /etc/network/interfaces:


# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet dhcp

auto eth1
iface eth1 inet dhcp
Run Code Online (Sandbox Code Playgroud)

对于 UBUNTU 16.04

运行命令

ifconfig -a
Run Code Online (Sandbox Code Playgroud)

寻找新界面,就像在我的情况下是“enp0s8”

EDIT file /etc/network/interfaces:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet dhcp

auto enp0s8
iface enp0s8 inet dhcp  
Run Code Online (Sandbox Code Playgroud)

  • 这现在也适用于 Debian 9+(Ubuntu 16 设置) (5认同)

小智 5

在 Ubuntu 18.04 主机 VirtualBox 6.1 中,使用 Ubuntu 19.04 作为来宾

在来宾编辑/etc/netplan/50-cloud-init.yaml文件中,添加如下所示的两行(在版本行之前)。看起来来宾中的网络配置仅设置为处理一个网络,而必须手动添加第二个网络

network:
    ethernets:
        enp0s3:
            dhcp4: true
        enp0s8:
           dhcp4: true
    version: 2
Run Code Online (Sandbox Code Playgroud)

  • 在 Ubuntu 19.10 中,该文件名为“/etc/netplan/01-netcfg.yaml”。更改后执行 `sudo netplan apply` 使更改生效。 (4认同)