---
title: "Iptables 根据IP、端口、MAC做策略路由"
id: "385"
type: "post"
slug: "iptables-%e6%a0%b9%e6%8d%aeip%e3%80%81%e7%ab%af%e5%8f%a3%e3%80%81mac%e5%81%9a%e7%ad%96%e7%95%a5%e8%b7%af%e7%94%b1"
published_at: "2021-12-02T16:05:31+00:00"
modified_at: "2021-12-02T16:09:45+00:00"
url: "https://www.hitsword.org/2021/385.html"
markdown_url: "https://www.hitsword.org/2021/385.md"
excerpt: "添加一条默认路由到188路由表： 给数据包mark上标签 添加rule路由规则,把mark为188的数据包走1 […]"
taxonomy_category:
  - "网关"
taxonomy_post_tag:
  - "iptables"
---

# Iptables 根据IP、端口、MAC做策略路由

添加一条默认路由到188路由表：

```
ip route add default via 127.0.0.1 table 188
```

  
 给数据包mark上标签  
```
##根据IP
iptables -t mangle -A PREROUTING -s 192.168.1.100 -j MARK --set-mark 188
iptables -t mangle -A PREROUTING -d 192.168.1.100 -j MARK --set-mark 188
##根据IP段和端口
iptables -t mangle -A PREROUTING -s 192.168.1.0/24 -p tcp --dport 18888 -j MARK --set-mark 188
iptables -t mangle -A PREROUTING -s 192.168.1.0/24 -p udp --dport 53 -j MARK --set-mark 188
##根据MAC地址
iptables -t mangle -A PREROUTING -m mac --mac-source XX:XX:XX:XX:XX:XX -j MARK --set-mark 188
```

  
 添加rule路由规则,把mark为188的数据包走188路由表  
```
ip rule add fwmark 188 lookup 188
```

  
 其他：刷新路由表缓存、查看路由表、查看路由规则、修改188路由表的默认路由  
```
ip route flush cache
ip route show table 188
ip rule show
ip route change default via 192.168.0.1 table 188
```
