Score:1

Ubuntu Server 22.10 on Virtualbox - host-only network and NAT can't coexist together?

be flag

I'm trying to create a Virtualbox VM (running Ubuntu Server 22.10) which has NAT (for internet access) and host-only adapter with static IP. It seems that I can get only one of both working. Optimally I'd configure the whole setup with automation tools like vagrant and ansible, but for now I am using netplan to debug directly in the VM.

(I'm new to networking, so any additional explanations are welcome.)

If I netplan apply only the NAT configuration:

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s3:
      dhcp4: true
  version: 2

I get normal internet access. Results of route -n are:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
10.0.2.2        0.0.0.0         255.255.255.255 UH    100    0        0 enp0s3
10.0.2.3        0.0.0.0         255.255.255.255 UH    100    0        0 enp0s3

If I also apply the host-only configuration:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s8:
      addresses:
      - 192.168.56.11/24

I lose internet access (ping -c 1 google.com results in Network is unreachable, as well as other commands like curl something or sudo apt update). Output from route -n:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.2.0        0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s8

I notice the difference, but I'm not able to interpret it in a useful way.

in flag
Experience has shown that bridged and host-only are better when working together on the same VM.
Score:2
at flag

They absolutely can! You are going to have to configure the two interfaces to make them work. I can help you. First, turn off your VM. Then, in the setting section of VirtualBox for your VM, make sure Adapter 1 is attached to NAT. Next, click the Apadpter 2 tab and make it a "Host-only Adapter". You probably have already gotten to this point. Now the fun beings!

Boot up your VM and log into it. To make things easier and not have to type sudo EVERY time, execute sudo su and then cd /etc/netplan. WARNING: DOING ANYTHING AS ROOT IS POTENTIALLY DANGEROUS. FOLLOW MY INSTRUCTIONS AND ONCE COMPLETE EXIT OUT OF ROOT.

This is the directory where all the magic happens. You are going to need to edit the YAML file. In my case, I had to edit the 00-installer-config.yaml file. Below is how I configured my VM to have both NAT and Host-only running together:

# This is the network config written by 'subiquity'
network:
  version: 2
  ethernets:
    enp0s3: #This is your network adapter attached to NAT 
      dhcp4: true
      routes:
        - to: default
          via: 10.0.2.2
      nameservers:
        addresses: #Below I chose to use google as the DNS servers. You can choose other DNS servers if you'd like.
          - 8.8.8.8 
          - 8.8.4.4
    enp0s8: #This is your network adapter attached to Host-only
      dhcp4: no
      addresses:
        - 192.168.56.101/24 #This is the static IP.
      routes:
        - to: default
          via: 192.168.56.1 
      nameservers:
        addresses:
          - 192.168.1.200

After modifying the file, you have to make sure the OS knows to use the new configuration by executing netplan apply

Now exit root: root@machine-name:/etc/netplan# exit

Now you can ping google.com and have both internet access to your VM and a static IP address!

Boom!

be flag
Thanks @edward-kennedy, this did work. I needed to add `metric: "50"` under `enp0s8` to avoid some conflict in the two route definitions. I'm not sure whether I need `nameservers` under `enp0s8`? Where did `192.168.1.200` come from?
Edward Kennedy avatar
at flag
Those IPs for the nameservers are arbitrary (example). You likely do not need to define the nameservers on the host-only network adapter.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.