Software & AppsOperating SystemLinux

How To Set Default Route with Netplan on Ubuntu 18.04 Server with 2 NICs

Ubuntu 13

In this tutorial, we’ll guide you through the process of setting up a default route with Netplan on an Ubuntu 18.04 server with two network interface cards (NICs).

Quick Answer

To set the default route with Netplan on Ubuntu 18.04 Server with 2 NICs, you need to modify the Netplan configuration file located in /etc/netplan/. Add a routes section to the configuration file, specifying the default gateway for each NIC using the to and via parameters. After making the changes, apply them by running sudo netplan apply.

Introduction

Netplan is a utility for easily configuring networking on a Linux system. You can use YAML descriptions to configure network interfaces and, from these descriptions, Netplan will generate the necessary configuration for your chosen renderer tool.

Prerequisites

Before we begin, ensure that you have:

  • An Ubuntu 18.04 server with at least two NICs installed and configured.
  • Root or sudo access to your server.

Understanding the Netplan Configuration File

Netplan configuration files are stored in the /etc/netplan/ directory and are written in YAML format. The configuration file defines the network interfaces and sets the default routes for your server.

Here’s an example of a Netplan configuration file:

network:
 version: 2
 renderer: networkd
 ethernets:
 ens18:
 dhcp4: yes
 dhcp6: no
 nameservers:
 addresses: [8.8.8.8, 8.8.4.4]
 ens19:
 dhcp4: yes
 dhcp6: no

In this example, ens18 and ens19 are the names of the two network interfaces. dhcp4 and dhcp6 are set to yes or no to enable or disable DHCP for IPv4 and IPv6 respectively. The nameservers section is used to specify the DNS servers.

Setting the Default Route

To set the default route for each network interface, we’ll add a routes section to the configuration file. Here’s an example configuration:

network:
 version: 2
 renderer: networkd
 ethernets:
 ens18:
 dhcp4: yes
 dhcp6: no
 nameservers:
 addresses: [8.8.8.8, 8.8.4.4]
 routes:
 - to: 0.0.0.0/0
 via: <public_gateway_ip>
 ens19:
 dhcp4: yes
 dhcp6: no
 routes:
 - to: 0.0.0.0/0
 via: <private_gateway_ip>

In this example, replace <public_gateway_ip> with the IP address of the default gateway for the public NIC (ens18), and <private_gateway_ip> with the IP address of the default gateway for the private NIC (ens19).

The routes section specifies the default gateway for each NIC. The to parameter is set to 0.0.0.0/0, which represents the default route. The via parameter is used to specify the gateway IP address.

Applying the Configuration

After modifying the configuration file, you can apply the changes by running the following command:

sudo netplan apply

This command will generate the necessary configuration for the networkd renderer and apply the changes to the system.

Conclusion

In this tutorial, we’ve shown you how to set the default route for two NICs on an Ubuntu 18.04 server using Netplan. By specifying the default gateway for each NIC, you can ensure that the correct default route is set even after a reboot.

Remember to replace the placeholders in the example configuration with your actual gateway IP addresses. And always make sure to run sudo netplan apply to apply your changes.

For more information about Netplan, you can refer to the official Netplan documentation.

What is Netplan?

Netplan is a utility for easily configuring networking on a Linux system. It uses YAML descriptions to configure network interfaces and generates the necessary configuration for your chosen renderer tool.

Where are Netplan configuration files stored?

Netplan configuration files are stored in the /etc/netplan/ directory.

How can I set the default route for multiple network interfaces with Netplan?

To set the default route for each network interface, you can add a routes section to the Netplan configuration file and specify the default gateway IP address for each NIC.

How do I apply the Netplan configuration changes?

After modifying the Netplan configuration file, you can apply the changes by running the command sudo netplan apply.

Can I use DHCP for configuring the network interfaces?

Yes, you can enable DHCP for IPv4 or IPv6 by setting the dhcp4 or dhcp6 option to yes in the Netplan configuration file.

How can I specify DNS servers for my network interfaces?

You can specify DNS servers by adding a nameservers section to the Netplan configuration file and providing the IP addresses of the DNS servers.

Where can I find more information about Netplan?

You can refer to the official Netplan documentation for more detailed information about Netplan and its configuration options.

Leave a Comment

Your email address will not be published. Required fields are marked *