In complex enterprise networks, a single default gateway is often not enough. If your Ubuntu server needs to talk to the internet through one router, but needs to access a private database subnet through a completely different internal router (often connected via a VPN tunnel or a secondary network interface), you must configure Static Routes. On modern Ubuntu systems, network configuration is handled entirely by Netplan, a YAML-based configuration utility.
Step 1: Understand the Routing Goal
Before modifying configuration files, you need to know three pieces of information:
- Destination Subnet: The remote network you are trying to reach (e.g.,
10.50.0.0/24). - Next Hop (Gateway): The IP address of the internal router on your local network that knows how to reach the destination (e.g.,
192.168.1.254). - Interface: The network card on your server connected to that local router (e.g.,
eth0orens18).
Step 2: Locate the Netplan Configuration File
Netplan configuration files are stored in /etc/netplan/. The exact filename varies depending on whether your server was provisioned by cloud-init or installed manually from an ISO.
List the files in the directory:
ls /etc/netplan/
You will typically see a file named 00-installer-config.yaml or 50-cloud-init.yaml.
Step 3: Edit the YAML File
Open the file with your preferred text editor (like nano or vim):
sudo nano /etc/netplan/00-installer-config.yaml
Crucial Warning: YAML relies entirely on strict indentation (spaces, not tabs). Ensure your indentation is perfectly aligned, or Netplan will throw a syntax error.
Locate the specific network interface (e.g., ens18) under the ethernets: block. You will add a routes: block directly underneath it.
network:
version: 2
ethernets:
ens18:
dhcp4: false
addresses:
- 192.168.1.10/24
routes:
- to: default
via: 192.168.1.1
- to: 10.50.0.0/24
via: 192.168.1.254
nameservers:
addresses:
- 8.8.8.8
- 1.1.1.1
Explanation of the changes:
- to: defaultreplaces the oldergateway4:directive (which is now deprecated in Netplan). This sets192.168.1.1as the standard internet gateway.- to: 10.50.0.0/24is your custom static route. It tells the server, “If traffic is destined for the 10.50.x.x network, don’t send it to the internet; send it to192.168.1.254instead.”
Step 4: Test the Configuration
Because a typo in network configurations can instantly disconnect you from the server, Netplan includes a fantastic “try” feature. It applies the configuration, but if you don’t confirm it within 120 seconds, it automatically reverts to the previous working state.
sudo netplan try
If you lose your SSH connection, simply wait two minutes for it to revert. If the connection stays active and the tool prompts you, press Enter to accept the new configuration.
Step 5: Verify the Routing Table
Once applied, you should verify that the Linux kernel has accepted the route.
ip route
You should see an output similar to this:
default via 192.168.1.1 dev ens18 proto static
10.50.0.0/24 via 192.168.1.254 dev ens18 proto static
192.168.1.0/24 dev ens18 proto kernel scope link src 192.168.1.10
The kernel is now correctly routing traffic for the 10.50.x.x subnet through your designated internal router.