Linux双网卡配置问题:ETH1接口访问超时的排查与解决方案


阅读 5 次

问题现象描述

在CentOS服务器上配置了双网卡环境,通过eth0(192.168.10.237)可以正常访问Web服务,但通过eth1(192.168.18.14)访问时出现连接超时。使用tcpdump抓包确认请求确实到达了eth1接口。

路由表分析

当前路由配置存在两个默认网关,这可能导致路由选择异常:

default via 192.168.10.1 dev eth0 proto dhcp metric 101
default via 192.168.18.1 dev eth1 proto dhcp metric 102
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.237 metric 101
192.168.18.0/24 dev eth1 proto kernel scope link src 192.168.18.14 metric 102

关键排查步骤

首先检查防火墙规则是否阻止了eth1的访问:

iptables -L -n -v | grep 192.168.18.14
firewall-cmd --list-all --zone=public

然后验证反向路径过滤(rp_filter)设置:

sysctl -a | grep .rp_filter
cat /proc/sys/net/ipv4/conf/eth1/rp_filter

解决方案实现

创建自定义路由表实现策略路由:

# 创建新的路由表
echo "200 eth1_routing" >> /etc/iproute2/rt_tables

# 添加路由规则
ip route add 192.168.18.0/24 dev eth1 src 192.168.18.14 table eth1_routing
ip route add default via 192.168.18.1 table eth1_routing

# 添加策略路由
ip rule add from 192.168.18.14/32 table eth1_routing
ip rule add to 192.168.18.14/32 table eth1_routing

永久化配置

/etc/sysconfig/network-scripts/目录下创建rule-eth1文件:

from 192.168.18.14/32 table eth1_routing
to 192.168.18.14/32 table eth1_routing

创建route-eth1文件:

192.168.18.0/24 dev eth1 src 192.168.18.14 table eth1_routing
default via 192.168.18.1 table eth1_routing

验证测试

使用curl命令指定出口网卡测试:

curl --interface eth1 http://192.168.18.14
curl --interface eth0 http://192.168.10.237

检查路由缓存信息:

ip route get 192.168.18.14
ip route get 192.168.10.237