nftables 配置ip动态访问
说明
系统是 Debian12,默认的网络工具是 nftables
可以查看是否启用
1 | systemctl status nftables |
没有启用的话设置开机自启,并启用
1 | systemctl enable nftables |
nftables 默认的配置文件在 /etc/nftables.conf
查看当前的表
1 | nft list tables |
查看特定表中的规则
1 | nft list table <table-name> |
查看所有的规则集
1 | nft list ruleset |
配置
可以使用命令执行测试,重启后并不会被保存
这里可以将自己所在的网段替换掉 127.0.0.1
,实现只有自己才能访问指定的端口
可以根据:ASN查询,知道自己的CIDR,ip不怎么变动的话可以通过CIDR实现配置,基本不需要修改
1 | # 创建名为 "filter" 的表格 |
如果需要持久化的保存,可以修改 /etc/nftables.conf
文件,加入以下配置(自行修改放行的端口,下面的示例文件时235端口)
1 | table ip firewall { |
使用以下命令重新加载 nftables 配置
1 | nft -f /etc/nftables.conf |
这会重新加载配置文件并应用新的规则。
动态获取
说明:我的443端口是一个只有自己可以正常访问的网页服务,通过 /var/log/nginx/access.log
获取当前的ip,将它加入到 allowed_ips
集合中
动态获取的目的是精准放行ip
1 |
|
加入定时任务
1 | # 每30分钟执行一次 |
本文采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ShiGuang
评论