Linux系统
Linux物理层
LSI Raid 阵列日常操作
MegaCLI基本使用指南
重要参数含义说明
Raid的增删改
Linux系统层
timedatectl命令时间时区操作
sar命令用法
Linux 性能调优工具9张图
Linux 特殊权限说明
Linux系统三级等保整改脚本
CentOS 7 停止维护(EOL)后的仓库变动
Linux查看主板内存槽与内存信息
安装麒麟Kylin-v10 Arm64版本到阿里云
CentOS7 多网卡单网关利用策略路由实现源进源出
初始化Linux数据盘(parted)
解决CentOS7下yum命令的异常
EXSI虚机mount出现‘unknown filesystem type 'LVM2_member'’
Linux虚机网卡单队列导致压测CPU无法满载的问题
Linux网络性能优化建议
Linux 修改系统语言环境
LInux文件系统中的默认保留空间 Ext4 vs. XFS
Linux CPU占用率原理与精确度分析
中标麒麟安装Nvidia显卡驱动
Linux主机双网卡同网段同网关配置
Linux 服务层
编译Expat 2.6.2的rpm包并升级
Linux主机挂载共享samba出现普通用户没有写权限的问题
编译OpenSSH 9.3p1的rpm包并升级
CentOS 7.x通过rpm升级OpenSSH到 8.5p1版本
Linux日志切割Logrotate原理和配置详解
systemd下配置sshd监听端口
编译NTP 4.2.8p17的rpm包并升级
编译OpenSSL 1.1.1w的rpm包并升级
linux命令集
磁盘工具集
Linux du 命令
fpsync数据迁移工具
字符处理集
Linux sed 命令
Linux命令输出重定向到变量
使用 paste 合并文件内容
常用调试指令集
编译cmake 3.5.2版本
网络工具集
MTR探测主机间丢包
Linux性能测试
甲骨文主机测试
本文档使用 MrDoc 发布
-
+
home page
Linux主机双网卡同网段同网关配置
# 1. 网络架构 在同一台机配置双网卡,并且都使用同一子网网段的内网地址的网络架构图 ``` +---------------------+ | Linux | | .168 .169 | +-----+--------+------+ │ │ +-----+--------+------+ | Switch | +---------+-----------+ │ +---------+-----------+ | .1 | | Gateway | +---------------------+ ``` # 2. 常见问题 在同一台机配置双网卡,并且都使用同一子网网段的内网地址,则通常会出现**仅有一个网卡可实现正常访问**的现象 抓包发现应该从网卡1传送过来的数据包,实际上是由网卡2传送过来的数据包 推荐的解决方案是使用单个接口或单个绑定/组接口,然后以别名 IP 地址的形式将其他 IP 地址添加到该接口。如果在网络接口级别需要容错和/或负载平衡,则应使用绑定或团队的别名。请注意,Red Hat Enterprise Linux 6 或更早版本不支持分组。如果接口级别不需要容错和/或负载平衡,则可以将别名 IP 地址添加到单个接口。 # 3. 根本原因 当同一子网上有 2 个接口时,无法保证哪个接口将用于传输流量,机器将接受任一接口上任一IP的流量。 这是因为在 Linux 中,IP 地址属于主机,与接口无关。 如果您尝试使用`ping`命令并使用参数`-I DEV`指定接口,则无法保证回复数据包(如果有的话)会返回同一接口,因此`ping -I DEV`操作可能无法正常工作。 # 4. 解决办法 该脚本自动实现静态路由的更改和创建,您只需要提供您想要修改的子网。 ```bash #!/bin/sh # fixFlux.sh - Assigns fixed routes for each NIC # residing on the same subnet to resolve # the ARP FlUX problem # See https://access.redhat.com/kb/docs/DOC-59096 # # Date: 7/21/2011 # Update Date: 12/09/2022 SCRIPT_REVISION=1.1.0 SCRIPT_LOG=/dev/null # Author: Ken Banks # Company: Kontron America. # # CHANGE LOG: # 7/21/2011 1.0.1 Original code. # 8/23/2011 1.0.2 Fix a bug with 'undo' to re-enble NetworkManager # and another bug that allowed multiple 'no's to be # added to NM_CONTROLLED= if the script was run more than once. # 12/09/2022 1.1.0 Fix network card acquisition and ip acquisition error problems. SYSCTL=/etc/sysctl.conf RT_TABLES=/etc/iproute2/rt_tables function usage() { echo "usage: $0 subnet <undo>" echo " version $SCRIPT_REVISION" echo " : This script will solve the Arp Flux for multiple NICs" echo " : residing on the same subnet, as described in this document:" echo " : https://access.redhat.com/kb/docs/DOC-59096" echo echo " e.g. $0 192.168.1 - Configures a static route for ever IC on 192.168.1.0" echo " $0 192.168.1 undo - Reverts the changes caused above and re-instates NetworkManager" } function log_message() { echo -e "$*" echo -e "$*" >> $SCRIPT_LOG } function compareValues() { awk "BEGIN { if ( $1 $2 $3 ) print \"TRUE\" }" } function addRoute() { NICNAME=$1 TABLENUM=$2 TABLENAME=$3 SUBNET=$4 DEFAULTGW=$5 NICIP=$6 FirstOctect=`echo $SUBNET | awk -F. '{ print $1 }'` ip route show | grep $6 > /dev/null if [ "$?" != "0" ] ; then echo ip route add $SUBNET dev $NICNAME src $NICIP table $TABLENAME ip route add $SUBNET dev $NICNAME src $NICIP table $TABLENAME echo ip route add table $TABLENAME default via $DEFAULTGW dev $NICNAME ip route add table $TABLENAME default via $DEFAULTGW dev $NICNAME #echo ip route show table $TABLENAME #ip route show table $TABLENAME fi ETHROUTE=/etc/sysconfig/network-scripts/route-$NICNAME echo $FirstOctect.0.0.0/8 dev $NICNAME src $NICIP table $TABLENAME > $ETHROUTE echo default via $DEFAULTGW dev $NICNAME table $TABLENAME >> $ETHROUTE echo "Modified: $ETHROUTE" ETHRULE=/etc/sysconfig/network-scripts/rule-$NICNAME echo table $TABLENAME from $NICIP > $ETHRULE echo "Modified: $ETHRULE" } function disableNetworkManager() { service NetworkManager status | grep stopped >/dev/null if [ "$?" != "0" ] ; then service NetworkManager stop > /dev/null fi echo "NetworkManager service is stopped." chkconfig NetworkManager --list | grep :on >/dev/null if [ "$?" == "0" ] ; then chkconfig NetworkManager off > /dev/null fi echo "NetworkManager service is disabled." ETHSCRIPT=/etc/sysconfig/network-scripts/ifcfg-$1 TMPSCRIPT=/tmp/tmpcfg-$1 grep -i NM_CONTROLLED=[\"yes\"] $ETHSCRIPT | awk -F. '{ print $1 }' > /dev/null if [ "$?" == "0" ] ; then cp $ETHSCRIPT $TMPSCRIPT cat $TMPSCRIPT | sed 's/NM_CONTROLLED=["yes"].*/NM_CONTROLLED=no/' > $ETHSCRIPT echo "Modified: $ETHSCRIPT" fi } function processNIC() { # inet 192.168.6.99/24 brd 192.168.6.255 scope global eth0show table # inet 192.168.6.60/24 brd 192.168.6.255 scope global eth1 export NICNAME=$1 export TABLENUM=$2 export TABLENAME=$3 export SUBNET=$4 export DEFAULTGW=`ip route | grep default | awk '{ print $3 }'` export NICDATA=`ifconfig -a $NICNAME | grep broadcast` NICIP=`echo $NICDATA | awk '{ print $2 }'` if [ "$NICIP" == "" ] ; then echo "ERROR: No IP for this NIC: $NICNAME" exit fi echo "====PROCESS NIC==== $NICDATA" echo "==== $NICNAME ====" grep $TABLENAME $RT_TABLES > /dev/null if [ "$?" != "0" ] ; then echo "Creating route table: $TABLENUM $TABLENAME" echo "$TABLENUM $TABLENAME" >> $RT_TABLES else echo "Route table already exists: $TABLENUM $TABLENAME" fi addRoute $NICNAME $TABLENUM $TABLENAME $SUBNET $DEFAULTGW $NICIP disableNetworkManager $NICNAME echo } function delete_route() { ip route delete $1 dev $2 } function undo_routes() { SUBNET=$1 echo 0 > /proc/sys/net/ipv4/conf/default/arp_filter echo 0 > /proc/sys/net/ipv4/conf/all/arp_filter cp $RT_TABLES /tmp/rt_tables grep -v "^.00 t[0-9]*" /tmp/rt_tables > $RT_TABLES rm -f /etc/sysconfig/network-scripts/r[ou][ul][te]* for i in `ip route show | grep src | awk '{ print $1"-"$3 }'` ; do delete_route `echo $i | sed 's/-/ /'` done grep net.ipv4.conf.all.arp_filter $SYSCTL > /dev/null if [ "$?" == "0" ] ; then grep "arp_filter[ =]*0" $SYSCTL > /dev/null if [ "$?" != "0" ] ; then cp $SYSCTL /tmp/tmpctl.conf cat /tmp/tmpctl.conf | sed 's/arp_filter[ =]*./arp_filter = 0/' >> $SYSCTL echo "Modified: $SYSCTL" fi fi for device in `ip addr | grep $SUBNET | awk '{ print $8 }'` ; do ETHSCRIPT=/etc/sysconfig/network-scripts/ifcfg-$devic TMPSCRIPT=/tmp/tmpcfg-$device grep -i NM_CONTROLLED=[\"no\"] $ETHSCRIPT | awk -F. '{ print $1 }' > /dev/null if [ "$?" == "0" ] ; then cp $ETHSCRIPT $TMPSCRIPT cat $TMPSCRIPT | sed 's/NM_CONTROLLED=[\"no\"].*/NM_CONTROLLED=yes/' > $ETHSCRIPT echo "Modified: $ETHSCRIPT" fi done chkconfig NetworkManager on service NetworkManager start } function main() { [ "$1" == "" ] && usage && exit SUBNET=$1 [ "$2" == "undo" ] && undo_routes $SUBNET && exit echo "# $0 $*" log_message "==========================================================================" log_message "=============================BEGIN ROUTE CREATION=========================" log_message "==========================================================================" table=1 for device in `ip addr | grep $SUBNET | awk '{ print $7 }'` ; do processNIC $device $((100 * $table)) t$table $SUBNET table=$((1 + $table)) done echo 1 > /proc/sys/net/ipv4/conf/default/arp_filter echo "set /proc/sys/net/ipv4/conf/default/arp_filter = 1" echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter echo "set /proc/sys/net/ipv4/conf/all/arp_filter = 1" grep net.ipv4.conf.all.arp_filter $SYSCTL > /dev/null if [ "$?" != "0" ] ; then echo "net.ipv4.conf.all.arp_filter = 1" >> $SYSCTL echo "Modified: $SYSCTL" else grep "arp_filter[ =]*1" $SYSCTL > /dev/null if [ "$?" != "0" ] ; then cp $SYSCTL /tmp/tmpctl.conf cat /tmp/tmpctl.conf | sed 's/arp_filter[ =]*./arp_filter = 1/' >> $SYSCTL echo "Modified: $SYSCTL" fi fi service network restart log_message "==========================================================================" log_message "==========================ROUTES HAVE BEEN CREATED========================" log_message "==========================================================================" echo "Contents of file: $RT_TABLES" cat $RT_TABLES echo "Command output from: ip route show" ip route show } main $* ``` >原文地址: [*如何连接同一子网上的两个网络接口* ](https://access.redhat.com/solutions/30564 "如何连接同一子网上的两个网络接口") >(因源文中的脚本在使用中存在问题,已在上述脚本中修正)
Nathan
Dec. 9, 2022, 2:05 p.m.
转发文档
Collection documents
Last
Next
手机扫码
Copy link
手机扫一扫转发分享
Copy link
Markdown文件
PDF文件
Docx文件
share
link
type
password
Update password