VirtualBox: two network interfaces (NAT and host-only ones) in a Debian guest on Ubuntu

I created a Debian VM on VirtualBox with two interfaces: a NAT one (for accessing internet) and a host-only one. However, I do not know how to make both interfaces work at the same time. If I the define the host-only as the adapter 1, I can access my VM from the host but not the internet; if I define the NAT one as adapter 1, I can access the internet but cannot reach my guest Debian.

So, how could I make both interfaces work together?

Note: I am still trying to map some port from my host to the SSH port from my guest SO, so there is no need to suggest me to do it 🙂

EDIT: This is the output of ifconfig when the first adapter is the host-only one:

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)

This is the output of netstat -nr when the first adapter is the host-only one:

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

This is the output of ifconfig when the first adapter is the NAT one:

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)

This is the output of netstat -nr when the first adapter is the NAT one:

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The solution was pretty simple: I just had to add the following lines into the Debian virtual machine‘s /etc/network/interfaces file:

allow-hotplug eth1
iface eth1 inet dhcp

The second line instructs the interface to obtain an IP via DHCP. The first line loads the interface at boot time.

To apply the changes to a running system, invoke:

ifup eth1

The name for the eth1 interface may vary, use ifconfig -a to list all available interfaces.

EDIT: full /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

Method 2

I was facing similar problem with my Ubuntu 14.04 VM, and Solution suggested by @brandizzi for Debian worked with little change.

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

For UBUNTU 16.04

Run command

ifconfig -a

Look for new interface like in my case it is ‘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

Method 3

In Ubuntu 18.04 host, VirtualBox 6.1, using Ubuntu 19.04 as guest

In the guest edit /etc/netplan/50-cloud-init.yaml file, add two lines as shown below (before the version line). Looks like the network configuration in the guest is set up only to handle one network and the second one has to be added manually

network:
    ethernets:
        enp0s3:
            dhcp4: true
        enp0s8:
           dhcp4: true
    version: 2

Method 4

Both adapter should be configured in Debian

It seems that in both case you only have one adapter configured, hence your problem.

Create 2 adapters in the VirtualBox configuration of your VM and then start it. If you see only one configured adapter when using ifconfig (a single eth0, no eth1 too), then you should use the network configuration tool for Debian (Network Manager, ifupdown, etc.) to configure both interface in DHCP. So you should have eth0 and eth1 in DHCP.

Method 5

Ubuntu Server 20.04 LTS uses Netplan for network configuration by default. The default configuration file in that case is /etc/netplan/00-installer-config.yaml.

  • Get the device name with ip link
  • Insert the device in the file /etc/netplan/00-installer-config.yaml (for example, enp0s8):
enp0s8:
  addresses: [192.168.2.89/24] # example
  gateway4: 192.168.2.1
  dhcp4: true
  • Run sudo netplan apply to apply changes.

Check this link for more information.

Method 6

Ensure you are using separate Private Address space subnets for each interface.

So for example, for the interface that is NAT, you could use an address in 192.168.0.0/16 space.

The interface that is ‘host only’ could be in the 10.0.0.0/24 space.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x