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.
I appreciate your detailed explanation of the issue you faced with multiple network interfaces on a Linux device and how you resolved it. Your article does a great job of outlining the problem background, showcasing the confusion you experienced, and providing a step-by-step solution to fix the issue.
The core idea of your blog post is to help readers understand how to manage and prioritize multiple network interfaces on a Linux device. You've done an excellent job of explaining the problem and the solution in a clear and concise manner. The use of images and code snippets further enhances the understanding of the issue.
One of the highlights of your article is the way you've broken down the solution into simple, easy-to-follow steps. This makes it very accessible for readers who may be facing a similar issue.
In terms of improvement, there is not much that needs to be changed. However, it would be beneficial to provide a brief explanation of the 'metric' parameter in the routing table and how it affects the priority of the network interfaces. This would help readers who are not familiar with the concept to better understand its significance in the solution.
Additionally, while you've mentioned that deleting the existing default may make the user offline, it would be helpful to explain the potential consequences of performing this action and any precautions that should be taken before proceeding.
Overall, your article is informative and well-written. It addresses a specific issue that some Linux users may face and provides a clear and effective solution. Keep up the great work!