2022-10-18
发表者 Venus
通过单个配置文件实现腾讯云双网卡配置多ip已关闭评论
之前都是用子配置文件方式,比如 ifcfg-eth0:0 ifcfg-eth0:1这种,需要创建多个配置文件。
现在通过单个配置文件实现多ip配置。
# 第一块网卡
[root@VM-0-3-centos network-scripts]# cat ifcfg-eth0
BOOTPROTO=static
DEVICE=eth0
HWADDR=52:54:00:4f:c4:e4
ONBOOT=yes
PERSISTENT_DHCLIENT=yes
TYPE=Ethernet
USERCTL=no
IPADDR=172.17.0.3
NETMASK=255.255.240.0
GATEWAY=172.17.0.1
IPADDR1=172.17.0.13
NETMASK1=255.255.240.0
IPADDR2=172.17.0.14
NETMASK2=255.255.240.0
IPADDR3=172.17.0.15
NETMASK3=255.255.240.0
IPADDR4=172.17.0.2
NETMASK4=255.255.240.0
IPADDR5=172.17.0.8
NETMASK5=255.255.240.0
# 第二块网卡配置
[root@VM-0-3-centos network-scripts]# cat ifcfg-eth1
BOOTPROTO=static
DEVICE=eth1
ONBOOT=yes
PERSISTENT_DHCLIENT=yes
TYPE=Ethernet
USERCTL=no
IPADDR=172.17.0.6
NETMASK=255.255.240.0
IPADDR1=172.17.0.11
NETMASK1=255.255.240.0
IPADDR2=172.17.0.12
NETMASK2=255.255.240.0
IPADDR3=172.17.0.16
NETMASK3=255.255.240.0
IPADDR4=172.17.0.4
NETMASK4=255.255.240.0
IPADDR5=172.17.0.5
NETMASK5=255.255.240.0
# 配置策略路由
echo "10 t1" >> /etc/iproute2/rt_tables
echo "20 t2" >> /etc/iproute2/rt_tables
ip route add default dev eth0 via 172.17.0.1 table 10
ip route add default dev eth1 via 172.17.0.1 table 20
ip rule add from 172.17.0.13 table 10
ip rule add from 172.17.0.14 table 10
ip rule add from 172.17.0.15 table 10
ip rule add from 172.17.0.2 table 10
ip rule add from 172.17.0.3 table 10
ip rule add from 172.17.0.8 table 10
ip rule add from 172.17.0.11 table 20
ip rule add from 172.17.0.12 table 20
ip rule add from 172.17.0.16 table 20
ip rule add from 172.17.0.4 table 20
ip rule add from 172.17.0.5 table 20
ip rule add from 172.17.0.6 table 20
# 测试ip连通性
#!/bin/bash
ip a | grep -o -e 'inet [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}'|grep -v "127.0.0"|awk '{print $2}' > ip
while read line || [[ -n ${line} ]]
do
line=`curl -s --interface $line -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.62" https://ipv4.icanhazip.com/`
echo "ip地址 " $line
done < ip