Linux Networking: Add Multiple Gateways with netplan
Configuring multiple gateways is an essential networking task when managing multi-homed servers or creating redundant connections in Linux or any other environment. Netplan, a modern network configuration tool, simplifies this process with its YAML-based syntax.
Introduciton
Netplan is a simple and effective utility used to configure networking on modern Ubuntu systems. It helps manage interfaces using YAML configuration files and integrates seamlessly with backends such as systemd-networkd
or NetworkManager
.
Purpose of this Configuration
Add multiple network gateways (redundancy) on Ubuntu with netplan (Backup Gateway in the Same Network) provides essential network redundancy and failover capabilities crucial for maintaining connectivity in diverse networking environments. This guide explores how to set up and manage primary and backup gateways within the same network using Netplan, ensuring reliable network access and seamless failover when encountering connectivity issues.
More about netplan
you can find at the link.
Netplan Configuration to Add Multiple Network Gateways
Netplan configuration file is located under the folder: /etc/netplan
To list the content of the /etc/netplan
folder type the following:
ls /etc/netplan
To edit the file type, the following:
sudo nano 00-installer-config.yaml
Content of the file:
network:
renderer: networkd
version: 2
ethernets:
eth0:
dhcp4: false
addresses: [192.168.19.189/24]
gateway4: 192.168.19.252
optional: true
nameservers:
addresses: [192.168.19.11, 8.8.8.8]
#routes:
- to: 0.0.0.0/0
via: 192.168.19.252 # Primary gateway IP
metric: 100 # Lower metric means higher priority
on-link: yes
- to: 0.0.0.0/0
via: 192.168.19.1 # Backup gateway IP
metric: 1000 # Higher metric means lower priority
on-link: yes
This configuration directs all traffic (0.0.0.0/0)
through the primary gateway 192.168.19.252
with a lower metric (higher priority).
Explanation of the Configuration
Top-Level Keys
network
- This key marks the beginning of the network configuration block.
renderer: networkd
- Specifies the backend responsible for managing the network settings. Here,
systemd-networkd
is used.
- Specifies the backend responsible for managing the network settings. Here,
version: 2
- Defines the
Netplan
schema version.
- Defines the
Configuring Ethernet Interfaces
This section configures the network interface eth0
:
dhcp4: false
- Disables DHCP, indicating that a static IP configuration will be used.
addresses
- Specifies the IP address in CIDR notation.
192.168.19.189/24
corresponds to an IP of192.168.19.189
and a subnet mask of255.255.255.0
.
- Specifies the IP address in CIDR notation.
gateway4
- Defines the primary IPv4 gateway, here set to
192.168.19.252
.
- Defines the primary IPv4 gateway, here set to
optional: true
- Marks this interface as optional. If it does not initialize successfully, the system will still boot without waiting indefinitely.
nameservers
- Defines DNS servers for name resolution. In this example:
192.168.19.11
could be a local DNS server.8.8.8.8
is Google’s public DNS.
- Defines DNS servers for name resolution. In this example:
Adding Static Routes
The routes
section, which is commented out in the provided configuration, allows for more granular control over routing traffic:
- Primary Route
to: 0.0.0.0/0
- The default route, directing traffic destined for any IP through the primary gateway.
via: 192.168.19.252
- Sets the gateway as
192.168.19.252
for this route.
- Sets the gateway as
metric: 100
- Specifies the priority of the route. A lower metric means higher priority.
on-link: yes
- Indicates that the gateway is reachable directly on the link, even without ARP resolution.
- Backup Route
to: 0.0.0.0/0
- Another default route for all traffic.
via: 192.168.19.1
- Sets the backup gateway.
metric: 1000
- Higher metric assigns lower priority. The backup gateway is used only if the primary route becomes unavailable.
on-link: yes
- Similarly ensures that the backup gateway is reachable on the link.
Behavior of the Configuration
- The interface
eth0
is assigned a static IP of192.168.19.189
with a primary gateway (192.168.19.252
) and a DNS resolver setup. - The system will prioritize routing traffic through the primary gateway. If
192.168.19.252
becomes unreachable, the backup gateway192.168.19.1
will take over, thanks to the route metrics. - Uncommenting the
routes
section enables automatic route management for gateway redundancy.
Test and Apply netplan Configuration
Once you have updated your Netplan configuration file, you can test and apply the changes using the following commands:
Test the Configuration
netplan try -timeout 120
This command applies the new configuration temporarily, allowing you to verify that it works as intended. If there are issues, the system will revert to the previous settings automatically after 120 seconds.
Apply the Configuration
# Apply network configuration
netplan apply
Use this command to make the configuration changes permanent. Ensure you have tested the settings before applying them.
Other topics related to the netplan
:
- Linux Networking: Add VLAN on Ubuntu with netplan
- Linux Networking: netplan DHCP Configuration on Ubuntu
- Linux Networking: netplan Network Configuration on Ubuntu