ip rule

Ip rule:

进行路由时,根据路由规则来进行匹配,按优先级(pref)从低到高匹配,直到找到合适的规则.所以在应用中配置默认路由是必要的

 

路由规则的添加
ip rule add from 192.168.1.10/32 table 1 pref 100
如果pref值不指定,则将在已有规则最小序号前插入

 

PS: 创建完路由规则若需立即生效须执行#ip route flush cache

        

From -- 源地址
        To -- 目的地址(这里是选择规则时使用,查找路由表时也使用)
   Tos -- IP包头的TOS(type of sevice)域Linux高级路由-
   Dev -- 物理接口
     Fwmark -- iptables标签

 

采取的动作除了指定路由表外,还可以指定下面的动作:
         Table 指明所使用的表
       Nat 透明网关

      Prohibit 丢弃该包,并发送 COMM.ADM.PROHIITED的ICMP信息 
      Reject 单纯丢弃该包
      Unreachable丢弃该包, 并发送 NET UNREACHABLE的ICMP信息

 

Usage: ip rule [ list | add | del ]SELECTOR ACTION
        SELECTOR := [ from PREFIX ] [ toPREFIX ] [ tos TOS ][ dev STRING ] [ pref NUMBER ]
        ACTION := [ table TABLE_ID ] [ natADDRESS ][ prohibit | reject | unreachable ]
                 [ flowid CLASSID ]
        TABLE_ID := [ local | main | default| new | NUMBER ]

srvcFpt.c:447:24: error: assignment to expression with array type handleCfg->rule.srcIp = ipRule->ipRule.sourceAddress; ^ srvcFpt.c:448:24: error: assignment to expression with array type handleCfg->rule.dstIp = ipRule->ipRule.destAddress; ^ srvcFpt.c:454:50: error: subscripted value is neither array nor pointer nor vector UINT8 srcIp[] = {ipRule->ipRule.sourceAddress[0], ipRule->ipRule.sourceAddress[1], ipRule->ipRule.sourceAddress[2], ipRule->ipRule.sourceAddress[3]}; ^ srvcFpt.c:454:83: error: subscripted value is neither array nor pointer nor vector UINT8 srcIp[] = {ipRule->ipRule.sourceAddress[0], ipRule->ipRule.sourceAddress[1], ipRule->ipRule.sourceAddress[2], ipRule->ipRule.sourceAddress[3]}; ^ srvcFpt.c:454:116: error: subscripted value is neither array nor pointer nor vector UINT8 srcIp[] = {ipRule->ipRule.sourceAddress[0], ipRule->ipRule.sourceAddress[1], ipRule->ipRule.sourceAddress[2], ipRule->ipRule.sourceAddress[3]}; ^ srvcFpt.c:454:149: error: subscripted value is neither array nor pointer nor vector UINT8 srcIp[] = {ipRule->ipRule.sourceAddress[0], ipRule->ipRule.sourceAddress[1], ipRule->ipRule.sourceAddress[2], ipRule->ipRule.sourceAddress[3]}; ^ srvcFpt.c:455:48: error: subscripted value is neither array nor pointer nor vector UINT8 dstIp[] = {ipRule->ipRule.destAddress[0], ipRule->ipRule.destAddress[1], ipRule->ipRule.destAddress[2], ipRule->ipRule.destAddress[3]}; ^ srvcFpt.c:455:22: error: initialization makes integer from pointer without a cast [-Werror=int-conversion] UINT8 dstIp[] = {ipRule->ipRule.destAddress[0], ipRule->ipRule.destAddress[1], ipRule->ipRule.destAddress[2], ipRule->ipRule.destAddress[3]}; ^ srvcFpt.c:455:22: note: (near initialization for 'dstIp[0]') srvcFpt.c:455:81: error: subscripted value is neither array nor pointer nor vector UINT8 dstIp[] = {ipRule->ipRule.destAddress[0], ipRule->ipRule.destAddress[1], ipRule->ipRule.destAddress[2], ipRule->ipRule.destAddress[3]}; ^ srvcFpt.c:455:55: error: initialization makes integer from pointer without a cast [-Werror=int-conversion] UINT8 dstIp[] = {ipRule->ipRule.destAddress[0], ipRule->ipRule.destAddress[1], ipRule->ipRule.destAddress[2], ipRule->ipRule.destAddress[3]}; ^ srvcFpt.c:455:55: note: (near initialization for 'dstIp[1]') srvcFpt.c:455:114: error: subscripted value is neither array nor pointer nor vector UINT8 dstIp[] = {ipRule->ipRule.destAddress[0], ipRule->ipRule.destAddress[1], ipRule->ipRule.destAddress[2], ipRule->ipRule.destAddress[3]}; ^ srvcFpt.c:455:88: error: initialization makes integer from pointer without a cast [-Werror=int-conversion] UINT8 dstIp[] = {ipRule->ipRule.destAddress[0], ipRule->ipRule.destAddress[1], ipRule->ipRule.destAddress[2], ipRule->ipRule.destAddress[3]}; ^ srvcFpt.c:455:88: note: (near initialization for 'dstIp[2]') srvcFpt.c:455:147: error: subscripted value is neither array nor pointer nor vector UINT8 dstIp[] = {ipRule->ipRule.destAddress[0], ipRule->ipRule.destAddress[1], ipRule->ipRule.destAddress[2], ipRule->ipRule.destAddress[3]}; ^ srvcFpt.c:455:121: error: initialization makes integer from pointer without a cast [-Werror=int-conversion] UINT8 dstIp[] = {ipRule->ipRule.destAddress[0], ipRule->ipRule.destAddress[1], ipRule->ipRule.destAddress[2], ipRule->ipRule.destAddress[3]}; ^
最新发布
11-07
### ip route 命令使用指南 `ip route` 命令用于配置和管理 Linux 系统中的路由表。它可以添加、删除或修改路由表中的条目,以确保数据包能够正确地从一个网络传输到另一个网络。 #### 添加静态路由 要添加一条静态路由,可以使用 `ip route add` 命令。例如,如果希望将所有发往 `192.168.1.0/24` 网络的数据包通过网关 `192.168.0.1` 发送出去,可以执行以下命令: ```bash ip route add 192.168.1.0/24 via 192.168.0.1 ``` 这条命令会在主路由表(main table)中添加一个新的路由条目。如果需要指定不同的路由表,可以通过 `table` 参数来指定,例如: ```bash ip route add 192.168.1.0/24 via 192.168.0.1 table 10 ``` 这里的 `table 10` 表示这条路由规则将被添加到编号为 10 的路由表中。 #### 删除静态路由 如果需要删除一条已经存在的路由条目,可以使用 `ip route del` 命令。例如,要删除上面添加的那条路由规则,可以执行: ```bash ip route del 192.168.1.0/24 via 192.168.0.1 ``` 同样地,如果路由条目是添加到特定路由表中的,也需要指定相同的路由表编号来删除它: ```bash ip route del 192.168.1.0/24 via 192.168.0.1 table 10 ``` #### 查看路由表 要查看当前系统的路由表信息,可以使用 `ip route show` 命令。默认情况下,它会显示主路由表的内容: ```bash ip route show ``` 如果想要查看特定路由表的信息,同样需要使用 `table` 参数: ```bash ip route show table 10 ``` ### ip rule 命令使用指南 `ip rule` 命令用于配置和管理 Linux 系统中的路由策略规则。这些规则决定了数据包应该根据哪些条件被分配到哪个路由表中进行处理。 #### 添加路由策略规则 要添加一条新的路由策略规则,可以使用 `ip rule add` 命令。例如,如果希望所有来自 `192.168.0.0/24` 网络的数据包都使用路由表 `10` 进行处理,可以执行以下命令: ```bash ip rule add from 192.168.0.0/24 table 10 ``` 这条规则意味着,所有源地址属于 `192.168.0.0/24` 网络的数据包都将被路由到编号为 `10` 的路由表中进行处理。 #### 删除路由策略规则 如果需要删除一条已经存在的路由策略规则,可以使用 `ip rule del` 命令。例如,要删除上面添加的那条规则,可以执行: ```bash ip rule del from 192.168.0.0/24 table 10 ``` #### 查看路由策略规则 要查看当前系统的路由策略规则,可以使用 `ip rule show` 命令: ```bash ip rule show ``` 这将列出所有已配置的路由策略规则,并显示它们的优先级顺序。 ### 高级路由配置示例 假设有一个场景,其中系统有两个网络接口:`eth0` 连接到互联网,而 `eth1` 连接到内部网络。我们希望所有来自 `eth1` 接口的数据包都通过特定的路由表 `10` 进行处理,以实现更精细的流量控制。 首先,我们需要创建一个新的路由表 `10`,并在其中添加相应的路由规则: ```bash ip route add 192.168.1.0/24 dev eth1 table 10 ip route add default via 192.168.1.1 dev eth1 table 10 ``` 接下来,我们需要添加一条路由策略规则,使得所有来自 `eth1` 接口的数据包都使用路由表 `10`: ```bash ip rule add dev eth1 table 10 ``` 这样,所有通过 `eth1` 接口进入系统的数据包都将按照路由表 `10` 中的规则进行处理。 ### 注意事项 - `src` 参数只会影响该主机上产生的网络包。对于被路由的外来包,它们已经带有了一个源 IP 地址,因此 `src` 参数对其没有任何影响,除非使用 NAT 来改变它。 - 在某些复杂的网络环境中,如 OpenStack Neutron,`qrouter` 和 `qif` namespace 中的路由表中的 `src` 都没有实际意义,因为它们只会处理外来的网络包。 通过合理使用 `ip route` 和 `ip rule` 命令,可以灵活地配置和管理 Linux 系统中的网络路由规则,从而满足各种复杂的网络需求 [^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值