Problem background
I recently built multiple subnets for my data center. And I connected two interfaces of a Linux device to two subnets at the same time.
When I run ifconfig
, below networks are shown:
Only the Green network has Internet access. The blue network is pure internal network.
However, when I try to access the Internet, it may fail with message: destination host unreachable
.
This is so confusing...
Finally when I run ip route list
, I found that Blue network has a higher priority. That is the root cause.
Solution
To view your current routing table:
ip route list
You need to delete the existing default:
# Caution: This may make you offline.
ip route del default
Then add a new default with lower metric:
sudo ip route add default via 172.16.50.1 dev ens160 proto dhcp metric 102
The result may be like (with ip route list
)
default via 172.16.50.1 dev ens160 proto dhcp metric 100
default via 192.168.50.1 dev ens192 proto dhcp metric 101
172.16.50.0/24 dev ens160 proto kernel scope link src 172.16.50.115 metric 100
192.168.50.0/24 dev ens192 proto kernel scope link src 192.168.50.192 metric 101
Finally, you can ask Linux which interface will be used to send a packet to a specific ip address:
ip route get 8.8.8.8
Or try Internet connection:
ping www.baidu.com
If you have multiple 'default' routes, you can prioritize one by removing and re-adding it with a lower metric.