随笔-网络-Linux多网卡同网段解决方法(配置IP路由)

系统配置:

多网口情况下,假设各个网口ip在同一网段;此时,由于默认路由规则,从不同网口的ip访问,服务器依旧会返回默认网口的mac地址,配置方式如下:

sysctl -w net.ipv4.ip_forward=1 # 开启ip转发规则
net.ipv4.conf.X.rp_filter = 0 # X 填 all default 各个网卡

路由设置:

  • 假设默认网卡eth0,现在想利用eth1,ip为192.168.xx.xx
  • 添加策略路由规则,将源ip为192.168.xx.xx的数据包使用table 1
ip rule add from 192.168.xx.xx table 1
  • 在table 1中添加默认路由,将数据包发送到eth1口:
  ip route add default dev eth1 table 1

示例:

假设两张网卡eth0 eth1, ip 分别配置为192.168.78.191 192.168.78.120

  1. 编辑网卡配置文件,注释掉GATEWAY 配置

  2. 编辑/etc/iproute2/rt_tables 添加路由表

echo "191 net_191" >> /etc/iproute2/rt_tables
  1. 重启服务器

  2. 添加路由规则

# echo "191 net_191" >> /etc/iproute2/rt_tables
ip route add 192.168.78.0/24 dev eth0 src 192.168.78.191 table net_191
ip route add default dev eth0 table net_191
ip rule add from 192.168.78.191 table net_191

# echo "120 net_120" >> /etc/iproute2/rt_tables
ip route add 192.168.78.0/24 dev eth1 src 192.168.78.120 table net_120
ip route add default dev eth1 table net_120
ip rule add from 192.168.78.120 table net_120

测试:找另外一台服务器,使用arping,如果返回对应网卡的mac地址正确即OK

参考:

posted @ 2024-05-13 21:32  LiYanbin  阅读(703)  评论(0编辑  收藏  举报