Linux 策略路由

一、多网卡,多公网IP,策略路由配置

1、配置路由表

1、安装
# apt install iproute2

2、添加路由表
# cat /etc/iproute2/rt_tables
   10 10 #ens224
   11 11 #ens192

2、配置网卡

1、网卡的配置
# cat /etc/netplan/00-installer-config.yaml network:
renderer: networkd ethernets: ens192: addresses:
- 10.2.11.152/24 gateway4: 10.2.11.116 routes: - to: 0.0.0.0/0 via: 10.2.11.116 table: 11 nameservers: addresses: [8.8.8.8] search: [] routing-policy: - from: 10.2.11.152 table: 11 priority: 300 ens224: addresses: - 10.2.10.152/24 routes: - to: 0.0.0.0/0 via: 10.2.10.254 table: 10 routing-policy: - from: 10.2.10.152 table: 10 priority: 300 version: 2

2、配置生效

# netplan apply

3、查看路由

# route -n

3、测试效果

# curl myip.ipip.net --interface 10.2.10.152
# curl myip.ipip.net --interface 10.2.11.152

 4、命令模式

1、主网卡
清空路由表 ip route flush table
10
添加默认路由
ip route add default via 10.2.11.116 dev ens192 table 11
添加网段
ip route add 10.2.11.0/24 dev ens192 table 11
添加网卡IP ip rule add from 10.2.11.152 table 11 扩展网卡 ip route flush table 10 ip route add default via 10.2.10.254 dev ens224 table 10 ip route add 10.2.10.0/24 dev ens224 table 10 ip rule add from 10.2.10.152 table 10 虚拟网卡IP ip rule add to 169.254.169.254 table main 查看策略路由 ip rule 查看路由表 ip route show table 10 ip route show table 20
测试 ping
-I 10.2.10.152 8.8.8.8
ping -I 10.2.11.152 8.8.8.8
ip route flush table 10
ip route add default via 10.7.10.1 dev eth0 table 10
ip route add 10.7.10.0/24 dev eth0 table 10
ip rule add from 10.7.10.201 table 10

ip route flush table 11
ip route add default via 10.7.11.11 dev eth1 table 11
ip route add 10.7.11.0/24 dev eth1 table 11
ip rule add from 10.7.11.201 table 11

ip route add default via 10.7.11.11 dev eth1 
ip route del default via 10.7.10.1 dev eth0

vim /etc/NetworkManager/NetworkManager.conf
#dns=dnsmasq
dns=8.8.8.8
rm /etc/resolv.conf
vim /etc/resolv.conf
nameserver 8.8.8.8

5、Windows 配置策略路由

1、添加策略路由
route add -p 0.0.0.0 mask 0.0.0.0 10.2.11.116 metric 555
metric :扩展网卡的优先级要小于主网卡,数字越大,优先级越低

2、测试
ping -S 10.2.10.152 8.8.8.8
ping -S 10.2.11.152 8.8.8.8

6、其他方法

1、配置优先级
ip route add default via 192.168.11.254 dev eth1 metric 1001 

2、配置路由表
ip route add default via 192.168.11.254 dev eth1 table 1001 

3、配置网卡IP
ip rule add from 192.168.11.11 lookup 1001 

 7、启用和禁用

1、禁用
# systemctl stop NetworkManager
# systemctl disable NetworkManager
# systemctl mask NetworkManager

2、启用systemd-networkd
# systemctl unmask systemd-networkd.service
# systemctl enable systemd-networkd.service
# systemctl start systemd-networkd.service

 

posted on 2024-05-17 10:20  五光十色  阅读(606)  评论(0编辑  收藏  举报

导航