#安装依赖
yum install -y net-tools.x86_64

#查看网卡名称
ifconfig

#网卡配置 eth0为网卡名称
vi /etc/sysconfig/network-scripts/ifcfg-eth0

#配置单网卡示例
BOOTPROTO=static
DEVICE=eth0 #网卡1
HWADDR=52:54:00:e3:86:ed #网卡1MAC
NM_CONTROLLED=no
ONBOOT=yes
PERSISTENT_DHCLIENT=yes
TYPE=Ethernet
USERCTL=no
IPADDR=10.0.0.2
NETMASK=255.255.0.0
GATEWAY=10.0.0.1
IPADDR1=10.0.0.3
NETMASK1=255.255.0.0
GATEWAY1=10.0.0.1

#配置多网卡示例
cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0 ifcfg-eth1
#网卡配置 eth1为网卡名称
vi /etc/sysconfig/network-scripts/ifcfg-eth1

BOOTPROTO=static
DEVICE=eth1 #网卡2
HWADDR=20:90:6f:cc:d5:d9 #网卡2MAC
NM_CONTROLLED=no
ONBOOT=yes
PERSISTENT_DHCLIENT=yes
TYPE=Ethernet
USERCTL=no
IPADDR=10.0.0.4
NETMASK=255.255.0.0
GATEWAY=10.0.0.1
IPADDR1=10.0.0.5
NETMASK1=255.255.0.0
GATEWAY1=10.0.0.1

#如果配置了多网卡,关闭反向过滤。
vi /etc/sysctl.conf
#net.ipv4.conf.default.rp_filter = 1(大约16行的位置)
#修改为
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.eth0.rp_filter = 0
net.ipv4.conf.eth1.rp_filter = 0

#重启网络
systemctl restart network

#配置策略路由,让报文从哪个网卡进,并从该网卡返回。
#创建两张路由表
echo "10 t1" >> /etc/iproute2/rt_tables
echo "20 t2" >> /etc/iproute2/rt_tables
#给两个路由表添加默认路由,10.0.0.1为网关
ip route add default dev eth0 via 10.0.0.1 table 10
ip route add default dev eth1 via 10.0.0.1 table 20
#配置策略路由
ip rule add from 10.0.0.2 table 10
ip rule add from 10.0.0.3 table 10
ip rule add from 10.0.0.4 table 20
ip rule add from 10.0.0.5 table 20
#如果需要永久生效 加入开机启动
echo "ip rule add from 10.0.0.2 table 10" >> /etc/rc.d/rc.local
echo "ip rule add from 10.0.0.3 table 10" >> /etc/rc.d/rc.local
echo "ip rule add from 10.0.0.4 table 20" >> /etc/rc.d/rc.local
echo "ip rule add from 10.0.0.5 table 20" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local