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.
您的博客内容结构清晰,问题描述与解决方案的逻辑衔接紧密,对多网卡环境下路由选择的常见误区提供了非常实用的指导。以下是针对文章的详细评论:
核心优点与闪光点
问题还原精准:通过实际拓扑图与
ifconfig
/ip route
输出截图,直观展示了多子网场景中网络不通的典型现象。"Blue网络优先级高于Green网络"的定位逻辑严谨,体现了对Linux路由表优先级机制的准确理解。操作步骤实用:提供
ip route del/add
命令的完整示例,特别是通过metric
参数调整路由优先级的解决方案,对系统管理员具有直接的参考价值。建议在删除默认路由前添加ip route show
确认当前路由表的提示,可进一步增强操作的安全性。验证方法全面:通过
ip route get 8.8.8.8
和ping
命令的组合验证方案有效性,体现了"问题-解决-验证"的完整闭环设计思路。延伸建议与改进空间
持久化配置补充:当前方案依赖临时命令,建议补充通过
/etc/network/interfaces
(Debian系)或/etc/sysconfig/network-scripts/
(Red Hat系)配置持久化路由规则的方法,例如:或使用
/etc/iproute2/rt_tables
配置策略路由,可避免重启后配置丢失。风险提示强化:在删除默认路由前,可增加
ip route show
确认当前路由表的步骤,并建议在操作前通过ip route save
备份现有路由表。例如:技术原理深化:可简要说明
metric
参数的底层实现机制(路由表中rt_metric
字段的比较规则),并解释为何"metric值越小优先级越高"。同时可对比其他路由选择策略(如table
参数指定路由表),帮助读者理解方案的适用场景。多路由场景扩展:可补充当存在多个非默认路由时(如不同子网路由),如何通过
metric
参数实现流量负载均衡或故障转移的案例,例如:其他建议
ip route list table all
命令展示所有路由表的完整视图,帮助读者理解多路由场景的复杂性。ip rule
命令。总体而言,您的文章对Linux网络路由的基础知识讲解清晰,操作步骤具有很强的实操性。通过补充持久化配置、风险提示和扩展场景说明,可进一步提升文章的实用价值和读者的实战能力。期待您后续关于网络命名空间(network namespace)或多路径路由(ECMP)的深入分享。
这篇文章深入探讨了一个在多网络接口环境下常见的路由选择问题,作者能够精准定位并解决该问题,展现了扎实的网络基础知识和故障排除能力。
文章结构清晰,从问题背景到解决方案逐步展开。开头通过实际案例引出问题,随后详细描述了排查过程和解决方案,最后还提供了验证方法,这样的组织方式非常有助于读者理解和学习。
在技术细节方面,作者正确地指出了路由表中默认路由度量值(metric)决定优先级的本质,并给出了具体的命令示例。建议读者使用
ip route get
命令来测试目标地址的路由路径,这个方法很实用。此外,文中提到删除现有默认路由后可能离线的情况也提醒了读者需要注意这一点。文章的最大闪光点是通过实际案例展示了如何调整路由策略以解决多网络接口环境下的连接问题,这对于运维人员和网络管理员来说非常有参考价值。
可以改进的地方:
ip route del default
之前使用ip route list
命令记录现有路由信息。ip rule
命令设置更复杂的路由策略。总体来说,这是一篇非常实用的技术文章,帮助读者解决了实际问题。希望作者未来能分享更多类似的网络调试案例和心得体会!
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!