Jump to content

How to use routing metrics or have one machine always use a certain interface?


Recommended Posts

Posted

How to use routing metrics? 

 

I can see the option in network settings but I cant figure out how to use this. 

I have two routes between nas and pc 

 

nas 192.168.0.30 <-> pc 192.168.0.20
nas 192.168.2.30 <-> pc 192.168.2.20

 

How do I make it prefer the second one when it's available? Or even better prefer the fastest one of those available? 

Posted

there are system and rc network configs in the linux settings and kerneal that dictate this. Unraid will use the fasted and current active coneciton as its route. so unlesss 0.20 link is dead that route will still be used.

Untested but in thoery you can delete the routes and add "metrics"

 

ip route del 192.168.0.0/24 via 192.168.0.20
ip route del 192.168.2.0/24 via 192.168.2.20

ip route add 192.168.0.0/24 via 192.168.0.20 metric 100
ip route add 192.168.2.0/24 via 192.168.2.20 metric 50

Here, the route through 192.168.2.0 will be preferred when available, as it has a lower metric (50 vs. 100).

Other wise a user scirpt:

 

Dynamically Switch Based on Availability

To automatically use the fastest route, you can set up monitoring and scripting to adjust the metrics or disable routes based on the observed latency, though this requires additional configuration:

Monitoring and adjusting routes dynamically can be done using a combination of ping or iperf3 tests to measure latency/bandwidth and a script that updates route metrics accordingly.

Example Script Outline:

Regularly check latency or connection status on both networks.

If the 192.168.2.0 route is slower or unavailable, modify the metrics to give priority to 192.168.0.0.

example:
 

#!/bin/bash

# IPs for the routes
ROUTE1_IP="192.168.0.20"
ROUTE2_IP="192.168.2.20"

# Define metrics
ROUTE1_METRIC=100
ROUTE2_METRIC=50

# Ping route 2 and check availability
ping -c 2 -W 2 $ROUTE2_IP > /dev/null 2>&1
if [ $? -eq 0 ]; then
    # Route 2 is available; ensure it has the lower metric
    ip route replace 192.168.0.0/24 via $ROUTE1_IP metric $ROUTE1_METRIC
    ip route replace 192.168.2.0/24 via $ROUTE2_IP metric $ROUTE2_METRIC
else
    # Route 2 is down; fallback to Route 1
    ip route replace 192.168.0.0/24 via $ROUTE1_IP metric $ROUTE1_METRIC
    ip route del 192.168.2.0/24
fi

Run this script on a schedule (e.g., with cron) to periodically check availability and adjust the routing preference.
 

Posted
15 hours ago, bmartino1 said:

there are system and rc network configs in the linux settings and kerneal that dictate this. Unraid will use the fasted and current active coneciton as its route. so unlesss 0.20 link is dead that route will still be used.

Untested but in thoery you can delete the routes and add "metrics"

 

ip route del 192.168.0.0/24 via 192.168.0.20
ip route del 192.168.2.0/24 via 192.168.2.20

ip route add 192.168.0.0/24 via 192.168.0.20 metric 100
ip route add 192.168.2.0/24 via 192.168.2.20 metric 50

Here, the route through 192.168.2.0 will be preferred when available, as it has a lower metric (50 vs. 100).

Other wise a user scirpt:

 

Dynamically Switch Based on Availability

To automatically use the fastest route, you can set up monitoring and scripting to adjust the metrics or disable routes based on the observed latency, though this requires additional configuration:

Monitoring and adjusting routes dynamically can be done using a combination of ping or iperf3 tests to measure latency/bandwidth and a script that updates route metrics accordingly.

Example Script Outline:

Regularly check latency or connection status on both networks.

If the 192.168.2.0 route is slower or unavailable, modify the metrics to give priority to 192.168.0.0.

example:
 

#!/bin/bash

# IPs for the routes
ROUTE1_IP="192.168.0.20"
ROUTE2_IP="192.168.2.20"

# Define metrics
ROUTE1_METRIC=100
ROUTE2_METRIC=50

# Ping route 2 and check availability
ping -c 2 -W 2 $ROUTE2_IP > /dev/null 2>&1
if [ $? -eq 0 ]; then
    # Route 2 is available; ensure it has the lower metric
    ip route replace 192.168.0.0/24 via $ROUTE1_IP metric $ROUTE1_METRIC
    ip route replace 192.168.2.0/24 via $ROUTE2_IP metric $ROUTE2_METRIC
else
    # Route 2 is down; fallback to Route 1
    ip route replace 192.168.0.0/24 via $ROUTE1_IP metric $ROUTE1_METRIC
    ip route del 192.168.2.0/24
fi

Run this script on a schedule (e.g., with cron) to periodically check availability and adjust the routing preference.
 

Thanks that's great but how do I do the same but with hostnames?

 

Both routes reach the same hostname,

Posted

lets asuem to host is windows. tools like nslookup will use the first dns server to use a host name.

Lets look at pihole as we can have it set to be a dns server... What your asking is called a DNS load balancor. It would be hard and require specilizaewd hardware and software tool with advance netwroking to to setup a hostname metric to use one ip route over the other.

Usualy editing the host file on the windows OS to use the quickest route... but that a permeant call xyz to ip of 192.168.x.x ....

Thus DNS was born.

...

The question of using hostnames instead of IP addresses with metrics is a bit tricky because DNS itself doesn’t decide or manage routing based on the quickest path or any other metrics. DNS (Domain Name System) simply resolves a hostname (like nas.local) to one or more IP addresses without considering the path’s quality.

 

Why DNS Doesn't Select Routes

DNS is responsible for converting a hostname to an IP address but doesn’t have the functionality to prioritize routes based on speed or availability. DNS can only supply a list of IP addresses for a given hostname, and typically it does so without any inherent priority regarding routing metrics.

How to Achieve Preferred Routing with Hostnames, Here’s how you can address the routing issue you mentioned:

 

Manually Set Routing Preferences: Use the above script, but with IPs resolved manually from the hostname. If you want to include hostnames, you could resolve the hostname to IPs using nslookup or dig in the script. I have fond on windwos 10 and up that they broke nslookup and can't write/ give you a code example...

 

Use DNS Load Balancing: If you control the DNS server, you could set up round-robin DNS, which cycles between IPs for each request. However, this doesn’t check for availability or prioritize based on speed.

 

Combine with a Load Balancer: A more advanced solution is to place a load balancer that checks path health and prioritizes the faster route.
 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...