ip rule ,ip tables ,ip route 的过程

 

ip tables 和 ip route 是两个不同的工具,它们在不同的阶段执行不同的功能。ip route 是用来管理和控制路由表的,它决定了数据包应该从哪个网卡或网关发送出去。ip tables 是用来配置、管理和控制网络数据包的过滤、转发和转换的,它根据用户定义的规则对数据包进行检查、修改或丢弃。

一般来说,ip route 先于 ip tables 执行,因为路由表是在内核中最先处理数据包的部分。当内核收到一个数据包时,它会先查看路由表,找到合适的出口网卡或网关,然后将数据包发送出去。在发送之前,内核会根据 ip tables 中的规则对数据包进行过滤、转发或转换。例如,内核可能会根据 NAT 规则更改数据包中的源地址或目标地址,或者根据 filter 规则接受或丢弃数据包。

当然,这只是一个简化的描述,实际上 ip tables 和 ip route 之间还有一些交互和顺序的细节。例如,ip tables 中有一些链可以在路由之前或之后对数据包进行处理,如 prerouting 和 postrouting 链。如果你想了解更多关于 ip tables 和 ip route 的工作原理和执行顺序,你可以参考以下链接:

问:哪ip route 有在ip tables 之后执行的吗? 

一般来说,ip route 是在 ip tables 之前执行的,因为路由表是在内核中最先处理数据包的部分。但是,有一种情况下,ip route 可能会在 ip tables 之后执行,那就是当 ip tables 中有一条规则使用了 MARK 目标时。

MARK 目标是一种特殊的目标,它可以给数据包打上一个标记,这个标记可以被路由策略或其他 iptables 规则所识别。当内核在 iptables 中遇到一个 MARK 目标时,它会将数据包的标记字段设置为指定的值,然后将数据包重新送回路由表进行第二次路由。这样,就可以根据数据包的标记来选择不同的路由策略或出口网卡。

例如,假设我们有两个网卡 eth0 和 eth1,分别连接到两个不同的网络。我们想让所有来自 192.168.1.0/24 网络的数据包都走 eth0 网卡,而其他数据包都走 eth1 网卡。我们可以使用以下命令来实现这个需求:

创建一个名为 mark 的自定义链

iptables -t mangle -N mark

在 PREROUTING 链中跳转到 mark 链

iptables -t mangle -A PREROUTING -j mark

在 mark 链中给来自 192.168.1.0/24 的数据包打上 1 的标记

iptables -t mangle -A mark -s 192.168.1.0/24 -j MARK --set-mark 1

在 mark 链中给其他数据包打上 2 的标记

iptables -t mangle -A mark -j MARK --set-mark 2

创建两个路由表 table1 和 table2

echo “201 table1” >> /etc/iproute2/rt_tables echo “202 table2” >> /etc/iproute2/rt_tables

在 table1 中添加默认路由为 eth0

ip route add default dev eth0 table table1

在 table2 中添加默认路由为 eth1

ip route add default dev eth1 table table2

添加两条路由策略,根据标记选择路由表

ip rule add fwmark 1 table table1 ip rule add fwmark 2 table table2

这样,当内核收到一个数据包时,它会先查看路由表,找到合适的出口网卡或网关,然后将数据包发送到 iptables 的 mangle 表的 PREROUTING 链。在这里,内核会根据数据包的源地址给它打上一个标记,然后将数据包重新送回路由表进行第二次路由。这次,内核会根据数据包的标记选择不同的路由策略和出口网卡,从而实现了按源地址分流的效果。

如果你想了解更多关于 MARK 目标和路由策略的知识,你可以参考以下链接:

那ip rule ,ip tables ,ip route 的顺序是什么呢? 讲讲过程 

 

ip rule,ip tables,ip route 的顺序是这样的:

  • 当内核收到一个数据包时,它会先查看 ip rule 中的路由策略,根据数据包的特征(如源地址、目标地址、标记等)选择一个路由表。
  • 然后,内核会在选定的路由表中查找匹配的路由条目,根据数据包的目标地址选择一个出口网卡或网关。
  • 接着,内核会将数据包发送到 iptables 的 mangle 表和 nat 表的 prerouting 链,根据用户定义的规则对数据包进行修改或转换(如更改 IP 头、执行 DNAT 等)。
  • 然后,内核会根据修改后的数据包重新查看 ip rule 和 ip route,选择一个新的出口网卡或网关。
  • 接着,内核会将数据包发送到 iptables 的 filter 表的 forward 链,根据用户定义的规则对数据包进行过滤或转发(如接受、丢弃、拒绝等)。
  • 然后,内核会将数据包发送到 iptables 的 mangle 表和 nat 表的 postrouting 链,根据用户定义的规则对数据包进行修改或转换(如更改源地址、执行 SNAT 等)。
  • 最后,内核会将数据包发送出去。

这是一个简化的描述,实际上 iptables 和 ip rule 之间还有一些交互和顺序的细节。如果你想了解更多关于 iptables 和 ip rule 的工作原理和执行顺序,你可以参考以下链接:

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#!/bin/sh # Set the name of the primary network interface primary_interface="eth0" # Set the name of the secondary network interface secondary_interface="wlan0" # Set the IP address range of the local network local_network="192.168.1.0/24" primary_interface_table="eth0_table" # Keep running the script indefinitely while true; do # Check if the primary interface is up and connected to the local network if ip addr show $primary_interface up | grep -q $local_network; then # Add a new routing table for the primary interface echo "200 $primary_interface_table" >> /etc/iproute2/rt_tables # Add default route for primary interface to the new routing table primary_gateway=$(ip route show | grep "default" | grep "$primary_interface" | awk '{print $3}') ip route add default via $primary_gateway dev $primary_interface table $primary_interface_table # Add a rule to route all traffic from primary interface through the new routing table primary_ip=$(ip addr show $primary_interface | grep "inet\b" | awk '{print $2}' | cut -d/ -f1) ip rule add from $primary_ip table $primary_interface_table # Remove any existing default route for the secondary interface ip route del default dev $secondary_interface else # Remove any existing routing table for the primary interface existing_table=$(grep -n " $primary_interface_table" /etc/iproute2/rt_tables | cut -f1 -d:) if [ ! -z "$existing_table" ]; then sed -i "${existing_table}d" /etc/iproute2/rt_tables ip route flush table $primary_interface_table ip rule del table $primary_interface_table fi # Add default route for the secondary interface secondary_gateway=$(ip route show | grep "default" | grep "$secondary_interface" | awk '{print $3}') ip route add default via $secondary_gateway dev $secondary_interface fi # Wait for 1 second before checking the network interfaces again sleep 1 done 运行上述脚本,提示ip: RTNETLINK answers: File exists, 请问如何修正
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值