Linux下多网卡不同IP在同一网段的情况

本文介绍了解决多网卡在同一网段内负载均衡时出现的ARP冲突问题的方法。通过调整内核参数如 arp_announce 和 arp_ignore,可以有效避免IP地址冲突,并确保正确的网络通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在看Intel 1000 PT网卡的驱动时,有如下信息:

Multiple Interfaces on Same Ethernet Broadcast Network
echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter

google了一下,有人已经试验了这个,摘录如下:

多网卡在同一网段内的问题(2005-11-23 23:38:42)
  分类:网络安全
关键字:Strict Interface ARP, Multi-link, Multipath, Multiple network cards on same subnet problem
 
公司那个提供音乐下载的域名流量直逼1Gbps,但是系统的连接却不高,服务器用的是DELL2850的,板载两个Gbe的网口,还插了两块Intel的Gbe网卡,因此打算通过多网卡的负载均衡扩充一下系统的网络带宽。但是不想用多网卡的邦定,感觉那样不太灵活,因此决定采用Advanced Routing来解决这个问题。
 
查了一下 man ip,然后开始做了:
1) 给四个网口配置了四个地址
2) 在BIGIP上面把四个地址都加入到负载均衡
3) 用ip命令实现多网卡负载均衡
ip route replace default equalize scope global nexthop via 172.24.x.11 dev eth0 weight 1 nexthop via 172.24.x.11 dev eth1 weight 1 nexthop via 172.24.x.1 dev eth2 weight 1 nexthop via 172.24.x.11 dev eth3 weight 1
 
立即看到所有的流量被均衡到4个网口
 
但是出现了新的问题,大量的IP地址冲突信息出现在BIGIP的日志:
Nov 23 19:18:56 tc04 kernel: arp info overwritten for 172.24.x.30 by 00:14:22:1b:94:dc
Nov 23 20:30:24 tc04 kernel: arp info overwritten for 172.24.x.30 by 00:04:23:c0:5c:88
Nov 23 20:30:28 tc04 kernel: arp info overwritten for 172.24.x.30 by 00:14:22:1b:94:dc
Nov 23 21:03:14 tc04 kernel: arp info overwritten for 172.24.x.30 by 00:14:22:1b:94:dd
 
发现出现的几个产生IP冲突的MAC地址都是这台机器上面其他的网口的MAC,这就奇怪了。
通过tcpdump抓包,分析ARP信息发现是因为Linux响应ARP请求的时候,使用的IP地址并未被严格的限定,因此导致了这个问题。
 
很快 Google 到这篇文章: [PATCH] strict interface arp patch for Linux 2.4.2
 
 
发现没有什么可用的信息,然后就硬着头皮进入到 /proc/sys/net/ipv4/conf/all 目录看看有啥线索是解决2.6的
 
看到三个文件 arp_announce, arp_ignore, arp_filter
立即去 /usr/src/linux/Documentation/networking/ip-sysctl.txt 找到相关的说明:
arp_filter - BOOLEAN
        1 - Allows you to have multiple network interfaces on the same
        subnet, and have the ARPs for each interface be answered
        based on whether or not the kernel would route a packet from
        the ARP'd IP out that interface (therefore you must use source
        based routing for this to work). In other words it allows control
        of which cards (usually 1) will respond to an arp request.
        0 - (default) The kernel can respond to arp requests with addresses
        from other interfaces. This may seem wrong but it usually makes
        sense, because it increases the chance of successful communication.
        IP addresses are owned by the complete host on Linux, not by
        particular interfaces. Only for more complex setups like load-
        balancing, does this behaviour cause problems.
        arp_filter for the interface will be enabled if at least one of
        conf/{all,interface}/arp_filter is set to TRUE,
        it will be disabled otherwise
arp_announce - INTEGER
        Define different restriction levels for announcing the local
        source IP address from IP packets in ARP requests sent on
        interface:
        0 - (default) Use any local address, configured on any interface
        1 - Try to avoid local addresses that are not in the target's
        subnet for this interface. This mode is useful when target
        hosts reachable via this interface require the source IP
        address in ARP requests to be part of their logical network
        configured on the receiving interface. When we generate the
        request we will check all our subnets that include the
        target IP and will preserve the source address if it is from
        such subnet. If there is no such subnet we select source
        address according to the rules for level 2.
        2 - Always use the best local address for this target.
        In this mode we ignore the source address in the IP packet
        and try to select local address that we prefer for talks with
        the target host. Such local address is selected by looking
        for primary IP addresses on all our subnets on the outgoing
        interface that include the target IP address. If no suitable
        local address is found we select the first local address
        we have on the outgoing interface or on all other interfaces,
        with the hope we will receive reply for our request and
        even sometimes no matter the source IP address we announce.
        The max value from conf/{all,interface}/arp_announce is used.
        Increasing the restriction level gives more chance for
        receiving answer from the resolved target while decreasing
        the level announces more valid sender's information.
arp_ignore - INTEGER
        Define different modes for sending replies in response to
        received ARP requests that resolve local target IP addresses:
        0 - (default): reply for any local target IP address, configured
        on any interface
        1 - reply only if the target IP address is local address
        configured on the incoming interface
        2 - reply only if the target IP address is local address
        configured on the incoming interface and both with the
        sender's IP address are part from same subnet on this interface
        3 - do not reply for local addresses configured with scope host,
        only resolutions for global and link addresses are replied
        4-7 - reserved
        8 - do not reply for all local addresses
        The max value from conf/{all,interface}/arp_ignore is used
        when ARP request is received on the {interface}
 
嘿嘿,通过:
 
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
 
or
 
echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
 
立即搞定!
 
继续 Google 了一些信息,发现前面两个内核控制好像是在 2.6.5 实现的 :-)
推荐用前面的两个,arp_filter应该也能解决,感觉前面的两个是对arp_filter的更细节控制的实现。
 
然后还发现在inter pro/1000的网卡驱动的安装指南里面也有一节:
Multiple Interfaces on Same Ethernet Broadcast Network
------------------------------------------------------
Due to the default ARP behavior on Linux, it is not possible to have 
one system on two IP networks in the same Ethernet broadcast domain 
(non-partitioned switch) behave as expected. All Ethernet interfaces 
will respond to IP traffic for any IP address assigned to the system.
This results in unbalanced receive traffic.
If you have multiple interfaces in a server, either turn on ARP 
filtering by entering:
echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
(this only works if your kernel's version is higher than 2.4.5),
or,
install the interfaces in separate broadcast domains (either
in different switches or in a switch partitioned to VLANs).
 
参考信息:
<think>好的,用户现在问的是“双网卡同一网段IP怎么走一个网卡”。首先,我需要理解用户的需求。用户可能有两块网卡在一个网段,并且希望所有的流量都通过其中一块网卡出去,而不是系统自动选择另一块。这可能是因为用户希望控制流量路径,或者另一块网卡作为备用,或者有其他特定的网络配置需求。 接下来,我需要回忆之前的对话历史。之前用户询问过如何将两个网卡绑定到一个网关,我给出了三种方案:主备模式、策略路由和ECMP。现在的问题有所不同,用户不是要绑定到一网关,而是同一网段下让IP一个网卡。这可能涉及到路由策略的调整,避免系统默认的路由选择导致流量分散。 用户可能遇到的问题场景是,他们有两块网卡(比如eth0和eth1),都配置了同一网段IP地址,比如192.168.1.100/24和192.168.1.101/24,网关都是192.168.1.1。这时候系统可能会有两个默认路由,导致路由表混乱,流量可能随机选择其中一个网卡,或者根据路由优先级选择其中一个。用户希望所有流量都通过其中指定的一个网卡,比如eth0,而另一个网卡eth1可能作为备用或者其他用途。 接下来,我需要考虑如何解决这个问题。首先,系统默认情况下,如果有多个网卡在一子网,可能会有多个默认路由,导致路由冲突。解决方法是调整路由表的优先级,确保只有一个默认路由生效,或者使用策略路由来指定流量走特定的网卡。 可能的解决方案包括: 1. 调整路由指标(metric),让主网卡的路由metric更低,从而优先使用。 2. 删除或禁用副网卡的默认路由,只保留主网卡的路由。 3. 使用策略路由(ip rule)来指定特定流量走主网卡。 4. 如果用户需要备用功能,可以考虑主备模式的bonding。 但用户的问题明确指出要“走一个网卡”,可能不需要bonding,而是希望强制所有流量走其中一块,无论另一块的状态如何。因此,调整路由metric或删除副网卡的默认路由可能是更直接的方法。 然后,我需要验证这些方法的正确性。例如,在Linux系统中,路由表中的metric值决定了优先级,metric越低优先级越高。因此,将主网卡的metric设置得更低,系统会优先选择它作为默认路由。另外,如果副网卡没有默认路由,那么流量自然会走主网卡。 还需要考虑持久化配置的问题,因为通过命令行修改可能只是临时生效,重启后会失效。需要指导用户如何修改网络配置文件,比如在Netplan(Ubuntu)或NetworkManager中设置。 可能的步骤包括: 1. 查看当前路由表,确认默认路由的情况。 2. 调整主网卡的路由metric,使其优先级更高。 3. 删除副网卡的默认路由。 4. 配置持久化,确保重启后依然有效。 此外,需要提醒用户注意的问题,比如如果副网卡没有默认路由,当主网卡故障时,系统可能无法自动切换到副网卡,除非有其他机制(如keepalived或监控脚本)。因此,是否需要高可用性也是需要考虑的因素。 最后,确保回答清晰,步骤详细,并提供验证方法和注意事项,帮助用户正确实施配置,避免网络中断。</think>--- ### **核心问题解析** 当双网卡配置同一网段IP时,系统默认会基于**路由metric值**选择出口网卡。要实现强制流量走指定网卡,需通过路由优先级控制或策略路由实现。以下是详细解决方案: --- ### **解决方案一:调整路由Metric值** #### **原理说明** 系统优先选择**metric值更低**的默认路由。通过降低主网卡路由metric,确保流量始终优先通过主网卡。 #### **配置步骤(临时生效)** 1. **查看当前路由表** ```bash ip route show ``` 输出示例: ``` default via 192.168.1.1 dev eth0 metric 100 default via 192.168.1.1 dev eth1 metric 200 ``` 2. **删除副网卡默认路由** ```bash sudo ip route del default via 192.168.1.1 dev eth1 ``` 3. **修改主网卡metric(更低值)** ```bash sudo ip route replace default via 192.168.1.1 dev eth0 metric 50 ``` --- ### **解决方案二:持久化配置(Ubuntu netplan)** #### **配置文件修改** ```yaml # /etc/netplan/01-netcfg.yaml network: version: 2 renderer: networkd ethernets: eth0: addresses: [192.168.1.100/24] routes: - to: 0.0.0.0/0 via: 192.168.1.1 metric: 50 # 主网卡更低metric eth1: addresses: [192.168.1.101/24] # 不配置默认路由 ``` #### **应用配置** ```bash
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值