To configure two network interfaces in Ubuntu 22.04 on Hyper-V, you must edit the Netplan configuration files located in
/etc/netplan/. This setup allows one interface to handle external internet traffic while the second manages internal or isolated network communication, ensuring stable connectivity for complex virtual machine environments.
Identifying Your Network Interfaces
Before modifying any configuration, you need to know the exact names of your network adapters. Modern Linux distributions use predictable network interface naming schemes, which means the names will not be simple
eth0 or
eth1. Instead, they often follow patterns like
enp3s0,
ens33, or similar variations depending on the bus and slot.
Open a terminal in your Ubuntu VM and run the following command:
ip link show
This output lists all network interfaces along with their MAC addresses and state. You will see at least two active interfaces if you have attached two virtual switches in Hyper-V. Note down the names of both interfaces. For example, you might identify
enp0s3 as your primary internet-facing adapter and
enp0s8 as your internal-only adapter. It is crucial to use these exact names in your configuration files to avoid errors. If you are unsure which interface corresponds to which physical switch in Hyper-V, you can temporarily disconnect one virtual switch and observe which interface goes down using
ip link show.
Configuring Netplan YAML Files
Ubuntu 22.04 uses Netplan for network configuration. The configuration files are stored in
/etc/netplan/ and typically end with a
.yaml extension. There is usually only one file in this directory, such as
00-installer-config.yaml or
50-cloud-init.yaml. Always back up the original file before making changes:
sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak
Edit the YAML file using a text editor like
nano or
vim:
sudo nano /etc/netplan/00-installer-config.yaml
The structure of the Netplan file defines how each interface behaves. You need to specify whether an interface gets its IP address via DHCP or if it should use a static IP address. Below is a generic template that demonstrates how to define two interfaces. Replace the placeholder interface names and IP details with your actual values.
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: networkd
ethernets:
enp0s3: # Primary Interface (Internet)
dhcp4: true
dhcp6: false
enp0s8: # Secondary Interface (Internal)
dhcp4: no
dhcp6: no
addresses:
- 192.168.100.10/24
routes:
- to: 192.168.100.0/24
via: 192.168.100.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
In this example,
enp0s3 is configured to obtain an IP automatically from your router via DHCP. This is the standard approach for the interface connected to your main network. The second interface,
enp0s8, is set to static addressing. This is common for internal networks where you want consistent IP addresses for services like DNS, web servers, or database connections that other machines need to reach reliably.
Note that the deprecated
gateway4 directive has been removed in recent Netplan versions. Instead, you must explicitly define routes using the
routes block. In the example above, the route directs traffic destined for the
192.168.100.0/24 subnet through the gateway
192.168.100.1. If your secondary interface does not require a default gateway (because it is isolated), you can omit the
routes section entirely for that interface. However, ensure that the primary interface handles the default route to the internet.
Applying and Verifying Changes
Once you have saved the YAML file, apply the configuration by running:
sudo netplan apply
If there are syntax errors in your YAML file, this command will return an error message indicating the line number and nature of the problem. YAML is sensitive to indentation, so ensure that spaces are used consistently (typically two spaces per level) and that tabs are not used.
After applying the configuration, verify that both interfaces are working correctly. Use the
ip addr show command to check if the IP addresses have been assigned as expected. You can also test connectivity by pinging an external host from the primary interface and pinging a host on the internal network from the secondary interface.
ping -c 4 8.8.8.8
ping -c 4 192.168.100.1
If you experience issues with name resolution after configuring multiple interfaces, it is often due to how the resolver handles multiple DNS sources. You may need to adjust your
/etc/resolv.conf settings or ensure that your DHCP server provides the correct DNS servers. For more details on resolving name resolution issues in multi-interface setups, refer to our guide on
Temporary Failure in Name Resolution with pfSense on Ubuntu Server 22.04.
Additionally, if you find that internet access is lost after adding a second interface, it might be because the routing table is prioritizing the wrong interface for default traffic. Ensure that only one interface defines a default route (
to: default). If you accidentally add a default route to the secondary interface, it can cause asymmetric routing issues. See
Lost Internet Connection After Upgrading Ubuntu 20.04.6: Two Different Interfaces named en01 and eth1 for troubleshooting steps related to interface conflicts.
Troubleshooting Common Issues
Even with correct YAML syntax, you might encounter connectivity problems. One common issue is that the Hyper-V virtual switch is not properly bridged to the physical NIC on the host machine. Check the Hyper-V Manager on your Windows host to ensure that the virtual switch is connected to an external network adapter.
Another potential pitfall is firewall rules blocking traffic on the secondary interface. Ubuntu uses
ufw (Uncomplicated Firewall) by default. If you enable
ufw, you must explicitly allow traffic on the specific interface or port ranges you intend to use. You can check the status of your firewall with:
sudo ufw status verbose
If you are running services on the secondary interface, ensure that they are bound to the correct IP address. For example, if you are setting up a web server, you might want it to listen only on the internal IP to prevent external access. You can configure this in your application's configuration file by specifying the listen address as the static IP of
enp0s8.
Finally, always keep your system updated. Kernel updates can sometimes change driver behaviors or network stack optimizations. Regularly run
sudo apt update && sudo apt upgrade to ensure you have the latest stability fixes. For broader troubleshooting of misconfigured files that affect SSH and web labs, consult
Misconfigured Files: Web Labs, SSH Server, ping, traceroute, and Web Apps Not Working on Main VM (SSH via Browser Works).
Frequently Asked Questions
Can I use DHCP for both interfaces?
Yes, you can configure both interfaces to use DHCP by setting
dhcp4: true for each. However, this is generally discouraged for internal interfaces because dynamic IP assignments can break connections to services that rely on fixed addresses. Static IPs provide better predictability for internal networking.
What happens if I make a typo in the Netplan YAML file?
If you introduce a syntax error,
netplan apply will fail and display an error message. Your existing network configuration remains unchanged until you successfully apply a new valid configuration. To recover, you can revert to the backup file you created earlier using
sudo cp /etc/netplan/00-installer-config.yaml.bak /etc/netplan/00-installer-config.yaml and then reapply.
How do I remove a default gateway from a specific interface?
To remove a default gateway, simply delete the line containing
gateway4 (if present in older configs) or the route entry with
to: default under the specific interface block in the YAML file. Only one interface should have a default route to ensure proper internet routing.
Why is my secondary interface not visible in ip addr show?
If the interface does not appear, it may be disabled in Hyper-V or disconnected from the virtual switch. Verify the connection in Hyper-V Manager. Additionally, check if the interface name has changed due to kernel updates or hardware changes. Using
ip link show helps identify the current names.
Can I assign multiple IP addresses to a single interface?
While Netplan primarily supports one primary IP per interface, you can add additional IPs using the
addresses list within the same interface block. However, for distinct network segments, it is cleaner and more manageable to use separate physical or virtual interfaces rather than aliasing multiple IPs on one NIC.