Linux系列—策略路由、ip rule、ip route


原文鏈接: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   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM