原文链接:https://blog.csdn.net/u012758088/article/details/76255543
Source IP
根据来源端IP来决定数据包参考哪个路由表发送出去。以下两个示例分别指出,如果数据包的来源端IP是192.168.1.10,就参考路由表10;如果来源端IP为192.168.2.0/24网段的IP,就参考路由表20
1 ip rule add from 192.168.1.10 table 10 2 ip rule add from 192.168.2.0/24 table 20
Destination IP
根据目的端IP来决定数据包参考哪个路由表发送出去。以下两个示例分别指出,如果数据包的目的端IP是168.95.1.1,就参考路由表10;如果目的端IP是168.95.0.0/24网段的IP,就参考路由表20
1 ip rule add to 168.95.1.1 table 10 2 ip rule add to 168.96.0.0/24 table 20
ip rule show命令所显示内容的第一个字段就是优先级别,数字越小,代表优先级别越高,也代表这条规则可以排得越靠前
1 [root@localhost ~]# ip rule show 2 0: from all lookup local 3 32766: from all lookup main 4 32767: from all lookup default 5 [root@localhost ~]# ip rule add from 192.168.1.0/24 table 1 prio 10 6 [root@localhost ~]# ip rule add from 192.168.2.0/24 table 2 prio 20 7 [root@localhost ~]# ip rule show 8 0: from all lookup local 9 10: from 192.168.1.0/24 lookup 1 10 20: from 192.168.2.0/24 lookup 2 11 32766: from all lookup main 12 32767: from all lookup default
删除规则
1 ip rule del prio 10 2 ip rule del from 192.168.1.0/24 3 ip rule del table 1 4 ip rule del from 192.168.1.0/24 table 1 prio 10
添加路由
1 [root@localhost ~]# ip route add 192.168.1.0/24 dev eth1 table 10 2 [root@localhost ~]# ip route add default via 192.168.1.254 table 10 3 [root@localhost ~]# 4 [root@localhost ~]# ip route show table 10 5 192.168.1.0/24 dev eth1 scope link 6 default via 192.168.1.254 dev eth1
删除路由表
1 [root@localhost ~]# ip route show table 10 2 192.168.1.0/24 dev virbr0 scope link 3 default via 192.168.1.254 dev eth1 4 [root@localhost ~]# 5 [root@localhost ~]# ip route del default table 10 6 [root@localhost ~]# 7 [root@localhost ~]# ip route show table 10 8 192.168.1.0/24 dev virbr0 scope link 9 [root@localhost ~]# 10 [root@localhost ~]# ip route del 192.168.1.0/24 table 10 11 [root@localhost ~]# 12 [root@localhost ~]# ip route show table 10