Beyond the Basics: Advanced Techniques for Changing Route Metrics Using nmcli
Tahmid Ul Muntakim
Team Manager | Enterprise Solution Architect & DevOps Leader | Certified in Kubernetes (CKA), Red Hat (RHCE), PMP, ITIL | Designing Resilient & Scalable IT Systems
YouTube Tutorial Walkthrough : https://lnkd.in/gdmeKDtk
In today's interconnected world, ensuring your network traffic flows efficiently through the correct gateway is crucial. Recently, I faced an interesting challenge with network routing on one of our Linux servers. The server had two network interfaces, ens160 and ens224, each connected to different gateways. The task was to prioritize ens224 as the default route for internet traffic while maintaining both gateways.
Here's a detailed walkthrough of the problem and how we resolved it using the #nmcli tool.
The Issue
Our server had the following routing configuration:
[root@lvm04 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.50.1 0.0.0.0 UG 100 0 0 ens160
0.0.0.0 192.168.68.1 0.0.0.0 UG 101 0 0 ens224
192.168.50.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
192.168.68.0 0.0.0.0 255.255.255.0 U 101 0 0 ens224
In this setup, the server had two default gateways, but the internet traffic was not reaching the desired gateway (ens224). As a result, pings to external IPs, such as Google's DNS server at 8.8.8.8, were not responding.
The Solution
To resolve this, we needed to adjust the routing metrics so that ens224 would be the preferred gateway for default internet traffic.
Step-by-Step Resolution
Modify the Gateway for Both Interfaces:
nmcli connection modify ens160 ipv4.gateway 192.168.50.1
nmcli connection modify ens224 ipv4.gateway 192.168.68.1
Set Route Metrics:
We set a lower metric for ens224 to prioritize it and a higher metric for ens160:
nmcli connection modify ens224 ipv4.route-metric 100
nmcli connection modify ens160 ipv4.route-metric 200
Restart the Network Connections:
nmcli connection down ens160 && nmcli connection up ens160
nmcli connection down ens224 && nmcli connection up ens224
Verify the Routing Table:
After making these changes, we verified the routing table to ensure the metrics were correctly applied:
领英推荐
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.68.1 0.0.0.0 UG 100 0 0 ens224
0.0.0.0 192.168.50.1 0.0.0.0 UG 200 0 0 ens160
192.168.50.0 0.0.0.0 255.255.255.0 U 200 0 0 ens160
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens224
Testing the Configuration:
Finally, we tested the internet connectivity:
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=114 time=47.0 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=114 time=34.3 ms
By adjusting the routing metrics using nmcli, we successfully prioritized the correct gateway for internet traffic. This solution ensured that our server's network traffic flowed through the desired interface, optimizing connectivity and performance.
The flexibility and power of nmcli make it an invaluable tool for managing network configurations in Linux. Whether you're dealing with multiple gateways or complex network setups, nmcli provides the commands you need to ensure your network operates smoothly.
Feel free to connect with me if you have any questions or need further assistance with network configurations.
Let's keep our networks running efficiently!
#NetworkConfiguration #LinuxNetworking #SysAdmin #ITSolutions #NetworkManagement #NetworkingTips #Linux #SystemAdministration #ITInfrastructure #NetworkOptimization #TechSolutions #ITSupport #NetworkEngineering #TechBlog #ITNetworking #ServerManagement #NMCLI #TechCommunity #LinuxAdmin #NetworkRouting
Some more cheats
# Change the route metric for the ens224 profile
nmcli connection modify ens224 ipv4.route-metric 100
# Add a route with a specific metric
nmcli connection modify ens224 +ipv4.routes "192.168.1.0/24 192.168.1.1 200"
# Remove a specific route
nmcli connection modify ens224 -ipv4.routes "192.168.1.0/24 192.168.1.1"
# Set the metric for an IPv6 route
nmcli connection modify ens224 ipv6.route-metric 100
# Add an IPv6 route with a specific metric
nmcli connection modify ens224 +ipv6.routes "2001:db8::/32 2001:db8::1 200"
# Verify the changes
nmcli connection show ens224
# Bring down and then bring up the connection to apply changes
nmcli connection down ens224
nmcli connection up ens224
Using nmcli in Scripts:
Here is an example script to dynamically change route metrics based on certain conditions for the ens224 profile:
#!/bin/bash
# Script to change route metrics dynamically for ens224
# Example condition: Check if a specific network is reachable
ping -c 1 192.168.1.1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
# Network is reachable, set a lower metric
nmcli connection modify ens224 ipv4.route-metric 50
else
# Network is not reachable, set a higher metric
nmcli connection modify ens224 ipv4.route-metric 200
fi
# Apply the changes
nmcli connection down ens224
nmcli connection up ens224