Globe

Kratos
专注于用户阅读体验的响应式博客主题
  1. 首页
  2. 随手记录
  3. 正文

openwrt的防火墙规则记录

2024年3月5日 120点热度 0人点赞 0条评论

1
2
3
4
5
iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53
iptables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53
[ -n "$(command -v ip6tables)" ] && ip6tables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53
iptables -t nat -I POSTROUTING -o eth0 -j MASQUERADE

openwrt 从22.03后,防火墙更新到FW4,luci界面没有了自定义规则的地方,经过参考官方文档,找到了自定义规则的方法,具体如下:(高手可以直接路过了)

1.用工具远程登录路由,修改防火墙配置文件:/etc/config/firewall
在最后一行加上:

1
2
3
4
5
config include
         option        enabled                1
         option        type                'script'
         option        path                '/etc/firewall.user'
         option        fw4_compatible        1

2.在etc目录下新建文件:firewall.user,这个文件里就是保存自定义规则的,但是需要注意的是,fw4使用的是nfttables命令,比如下面的命令是强制劫持局域网内客户机的dns到路由:

1
2
3
4
5
6
# IPv4 和 IPv6 DNS 端口 53 TCP/UDP 重定向(PREROUTING)
nft add rule inet fw4 dstnat udp dport 53 counter redirect to :53
nft add rule inet fw4 dstnat tcp dport 53 counter redirect to :53
 
# 出口接口 eth0 的 POSTROUTING MASQUERADE
nft add rule inet fw4 postrouting oifname "eth0" counter masquerade

执行:

1
/etc/init.d/firewall restart

标签: 暂无
最后更新:2025年6月4日

free

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2024 EverythingIThink. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang