当前位置: 首页 > news >正文

Centos7 | 防火墙(firewalld)使用ipset管理ip地址的集合

文章目录

  • 一、firewalld中ipset的用途
    • 1.1 用途
    • 1.2 注意与iptables所用的ipset命令的不同,
    • 1.3 配置详解
  • 二、firewalld中ipset的操作例子
    • 2.1 新建一个set
    • 2.2 在set中添加ip
    • 2.3 从set中删除ip
    • 2.4 删除一个set
    • 2.5 打印一个set的文件路径
    • 2.6 打印一个set的内容
    • 2.8 判断一个ip是否存在于set中?
    • 2.9 列出所有的ipsets
    • 2.10 得到所有的默认ipset类型
  • 三、firewalld中使用ipset
    • 3.1 把一个ipset加入到禁止的规则
    • 3.2 把ip地址中ipset中删除
  • 四、添加到ipset中的ip地址数据是否会重复
  • 五、使用脚本抓取有问题的ip加入到拒绝访问的ipset
  • 六、如何防止自己被误关在防火墙外?使用ip白名单
  • 七、查看firewalld的版本
  • 八、查看linux的版本

一、firewalld中ipset的用途

1.1 用途

ipset是ip地址的集合,firewalld使用ipset可以在一条规则中处理多个ip地址,执行效果更高对ip地址集合的管理也更方便

1.2 注意与iptables所用的ipset命令的不同,

不要混合使用firewall-cmd的ipset参数与linux平台上的ipset命令,避免引起冲突,firewalld的ipset会记录到/etc/firewalld/ipsets/目录下

1.3 配置详解

IPSet Options--get-ipset-types    打印支持的ipset类型--new-ipset=<ipset>  --类型= < ipset类型>[——选项= <关键>[= < >价值]]. .添加一个新的ipset--new-ipset-from-file=<filename> [--name=<ipset>]  从文件中添加一个新的ipset[可选名称]--delete-ipset=<ipset>  删除已存在的ipset [可选名称]--load-ipset-defaults=<ipset>  加载ipset默认设置[可选名称]--info-ipset=<ipset>打印关于ipset的信息[可选名称]--path-ipset=<ipset>打印ipset的文件路径[可选名称]--get-ipsets         打印预定义ipsets--ipset=<ipset> --set-description=<description>设置新的描述为ipset[可选名称]--ipset=<ipset> --get-description打印ipset的描述[可选名称]--ipset=<ipset> --set-short=<description>设置新的短描述为ipset[可选名称]--ipset=<ipset> --get-short打印ipset的简短描述[可选名称]--ipset=<ipset> --add-entry=<entry>向ipset中添加一个新条目[可选名称]--ipset=<ipset> --remove-entry=<entry>从ipset中删除一个条目[可选名称]--ipset=<ipset> --query-entry=<entry>返回ipset是否有条目--ipset=<ipset> --get-entries列出ipset的表项[可选名称]--ipset=<ipset> --add-entries-from-file=<entry>向ipset中添加新条目[可选名称]--ipset=<ipset> --remove-entries-from-file=<entry>从ipset中删除条目[可选名称]

二、firewalld中ipset的操作例子

2.1 新建一个set

–new-ipset=sshblock: 指定新ipset的名字为:sshblock

–type=hash:ip 指定类型为 hash:ip,这种形式不允许重复而且只有一个ip

[root@blog ipsets]# firewall-cmd --permanent --new-ipset=sshblock --type=hash:ip
success查看ipset文件是否已生成?
说明:默认的目录是:/etc/firewalld/ipsets
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>

2.2 在set中添加ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=111.111.111.111
success查看添加ip的效果
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip"><entry>111.111.111.111</entry>
</ipset>

2.3 从set中删除ip

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --remove-entry=111.111.111.111
success查看删除ip的效果
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>

2.4 删除一个set

[root@blog ipsets]# firewall-cmd --permanent --delete-ipset=sshblock
success查看sshblock这个set的配置文件是否还存在?
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
more: stat of /etc/firewalld/ipsets/sshblock.xml failed: No such file or directory

2.5 打印一个set的文件路径

[root@blog ipsets]# firewall-cmd --permanent --path-ipset=sshblock
/etc/firewalld/ipsets/sshblock.xml

2.6 打印一个set的内容

[root@blog ipsets]# firewall-cmd --permanent --info-ipset=sshblock
sshblocktype: hash:ipoptions:entries: 111.111.111.111

2.8 判断一个ip是否存在于set中?

[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --query-entry=1.1.1.1
no
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --query-entry=111.111.111.111
yes

2.9 列出所有的ipsets

[root@blog ipsets]# firewall-cmd --permanent --get-ipsets
sshblock

2.10 得到所有的默认ipset类型

[root@blog ipsets]# firewall-cmd --get-ipset-types
hash:ip hash:ip,mark hash:ip,port hash:ip,port,ip hash:ip,port,net hash:mac
hash:net hash:net,iface hash:net,net hash:net,port hash:net,port,net

三、firewalld中使用ipset

3.1 把一个ipset加入到禁止的规则

[root@blog ipsets]# firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="sshblock" drop'
success查看xml中的记录:
[root@blog ipsets]# more /etc/firewalld/zones/public.xml
...<rule family="ipv4"><source ipset="sshblock"/><drop/></rule>
...使生效
[root@blog ipsets]# firewall-cmd --reload
success把禁止的规则删除
[root@blog ipsets]# firewall-cmd --permanent --remove-rich-rule 'rule family="ipv4" source ipset="sshblock" drop'
success查看xml中的记录(刚才的drop信息被去除了):
[root@blog ipsets]# more /etc/firewalld/zones/public.xml
......使生效
[root@blog ipsets]# firewall-cmd --reload
success

3.2 把ip地址中ipset中删除

注意:没写入到磁盘

[root@blog ipsets]# firewall-cmd --ipset=sshblock --remove-entry=111.111.111.111
success
[root@blog ipsets]# firewall-cmd --ipset=sshblock --query-entry=111.111.111.111
no
[root@blog ipsets]# firewall-cmd --ipset=sshblock --get-entries可见已删除成功,如果想永久性的记录下来:写入到磁盘后 reload一次
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --remove-entry=111.111.111.111
success
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>
[root@blog ipsets]# firewall-cmd --reload
success

四、添加到ipset中的ip地址数据是否会重复

因为使用了hash类型,当ip重复时firewall-cmd会报错:

新建ipset
[root@blog ipsets]# firewall-cmd --permanent --new-ipset=sshblock --type=hash:ip
success
添加ip
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=111.111.111.111
success查看文件
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip"><entry>111.111.111.111</entry>
</ipset>再次添加ip
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=111.111.111.111
Warning: ALREADY_ENABLED: 111.111.111.111
success查看文件:
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip"><entry>111.111.111.111</entry>
</ipset>

没有出现重复的情况

五、使用脚本抓取有问题的ip加入到拒绝访问的ipset

常用的几类ip:

  1. 被firewalld防火墙reject的ip

  2. nginx日志中访问过于频率的ip

  3. secure日志中登录失败的ip

我们以secure日志中登录失败的ip为例:

先用命令抓取到登录失败的ip:

[root@blog log]# grep -i 'Failed password' /var/log/secure | awk '{print $11}' | sort -n | uniq -c | sort -k1nr | awk '{if ($1>5) print $2}'
...写一段脚本,放到crond中定时执行即可:
[root@blog ~]# vi ./addlogifailip2firewall.sh内容:
#!/bin/bash
for LINE in `grep -i 'Failed password' /var/log/secure | awk '{print $11}' | sort -n | uniq -c | sort -k1nr | awk '{if ($1>3) print $2}'`; doecho "${LINE}";firewall-cmd --permanent --ipset=sshblock --add-entry="${LINE}";
done;
firewall-cmd --reload;

六、如何防止自己被误关在防火墙外?使用ip白名单

把允许访问的ip加入到trusted区域:

[root@blog zones]# firewall-cmd --permanent --zone=trusted --add-source=111.111.111.111使生效:
[root@blog zones]# firewall-cmd --reload注意此处不要使用ipset,
使用ipset后,如果同一个ip也被加入了被拒绝的set,
则此ip还是会关到外面。
原因在于firewalld把规则转到nftables的处理机制,
它把set的处理合并到默认的public zone中去处理了.
大家可以用nft的命令验证 :
[root@blog log]# nft list ruleset

七、查看firewalld的版本

[root@blog ~]# firewall-cmd --version
0.6.3

八、查看linux的版本

[root@blog ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)
http://www.dtcms.com/a/306950.html

相关文章:

  • MySQL 读写分离(含示例代码)
  • 新注册企业信息查询“数据大集网”:驱动企业增长的源头活水
  • 10 卷积神经网络
  • LLMs之Agent:GLM-4.5的简介、安装和使用方法、案例应用之详细攻略
  • 51单片机入门:数码管原理介绍及C代码实现
  • 【硬件】元器件选型
  • 【ESP32设备通信】-LAN8720与ESP32集成
  • 订阅区块,部署合约,加载合约
  • Akamai CloudTest before 60 2025.06.02 XXE注入导致文件包含漏洞(CVE-2025-49493)
  • MOEA/DD(多目标进化算法基于分解)简介
  • AAAI‘26 | 聚焦人工智能前沿:西工大李学龙教授荣任赞助主席,论文取号逼近三万,精彩不容错过!
  • Javaweb———HTTP响应头属性讲解
  • Redis实现数据传输简介
  • 【AI落地应用实战】利用 Amazon Bedrock Claude3 打造个性化 AI Character 应用
  • C++反射
  • JVM 性能调优实战:让系统性能 “飞” 起来的核心策略
  • B站 XMCVE Pwn入门课程学习笔记(6)
  • SpringBoot 实现 RAS+AES 自动接口解密
  • 2023年数学建模国赛C题第一问解答
  • 流匹配在翼型生成中的应用:完整实现指南
  • 实习小记(个人中心的编辑模块)
  • C++提高编程学习--模板
  • 【python 获取邮箱验证码】模拟登录并获取163邮箱验证码,仅供学习!仅供测试!仅供交流!
  • jakarta.websocket.DeploymentException:Endpoint instance creation failed
  • 2021 年 NOI 最后一题题解
  • pandas 分组相同赋值1然后累加
  • PAT 甲级题目讲解:1011《World Cup Betting》
  • 【MySQL 数据库】MySQL索引特性(一)磁盘存储定位扇区InnoDB页
  • Linux c网络专栏第四章io_uring
  • 面向对象中级编程