linux(centos8):firewalld使用ipset管理ip地址的集合
一,firewalld中ipset的用途:
1,用途
ipset是ip地址的集合,
firewalld使用ipset可以在一条规则中处理多个ip地址,
执行效果更高
对ip地址集合的管理也更方便
2,注意与iptables所用的ipset命令的不同,
不要混合使用firewall-cmd的ipset参数与linux平台上的ipset命令,
避免引起冲突,
firewalld的ipset会记录到/etc/firewalld/ipsets/目录下
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,firewalld中ipset的操作例子:
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,在set中添加ip
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=121.122.123.105
success
查看添加ip的效果
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
<entry>121.122.123.105</entry>
</ipset>
3,从set中删除ip
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --remove-entry=121.122.123.105
success
查看删除ip的效果
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
</ipset>
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
5,打印一个set的文件路径:
[root@blog ipsets]# firewall-cmd --permanent --path-ipset=sshblock
/etc/firewalld/ipsets/sshblock.xml
6,打印一个set的内容:
[root@blog ipsets]# firewall-cmd --permanent --info-ipset=sshblock
sshblock
type: hash:ip
options:
entries: 121.122.123.105
7,列出一个set下的所有entry
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --get-entries
121.122.123.105
...
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=121.122.123.105
yes
9,列出所有的ipsets
[root@blog ipsets]# firewall-cmd --permanent --get-ipsets
sshblock
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
1,把一个ipset加入到禁止的规则
[root@blog ipsets]# firewall-cmd --permanent --add-rich-rule 'rule family="ipv4" source ipset="sshblock" drop'
success
使生效
[root@blog ipsets]# firewall-cmd --reload
success
查看xml中的记录:
[root@blog ipsets]# more /etc/firewalld/zones/public.xml
...
<rule family="ipv4">
<source ipset="sshblock"/>
<drop/>
</rule>
...
2,把ip地址中ipset中删除
注意:没写入到磁盘
[root@blog ipsets]# firewall-cmd --ipset=sshblock --remove-entry=121.122.123.105
success
[root@blog ipsets]# firewall-cmd --ipset=sshblock --query-entry=121.122.123.105
no
[root@blog ipsets]# firewall-cmd --ipset=sshblock --get-entries
可见已删除成功,
如果想永久性的记录下来:写入到磁盘后 reload一次
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --remove-entry=121.122.123.105
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=121.122.123.105
success
查看文件
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
<entry>121.122.123.105</entry>
</ipset>
再次添加ip
[root@blog ipsets]# firewall-cmd --permanent --ipset=sshblock --add-entry=121.122.123.105
Warning: ALREADY_ENABLED: 121.122.123.105
success
查看文件:
[root@blog ipsets]# more /etc/firewalld/ipsets/sshblock.xml
<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:ip">
<entry>121.122.123.105</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}'`; do
echo "${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=121.122.123.105
使生效:
[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)
linux(centos8):firewalld使用ipset管理ip地址的集合的更多相关文章
- 分享:linux系统如何快速阻止恶意IP地址
可能你想要在各种情形下阻止有人通过IP地址访问你的Linux系统.比如说,作为最终用户,你可能想要保护自己,避免已知的间谍软件或跟踪者的IP地址.或者如果你在运行P2P软件,可能想要把来自与违反P2P ...
- Linux一块网卡添加多个IP地址
环境: RHEL6.4 需求: Linux一块网卡添加多个IP地址 一.临时生效 1.1 网卡eth0添加一个IP地址 1.2 修改eth0:0的广播地址 二.永久生效 2.1 编辑ifcfg-eth ...
- 烂泥:更换ESXI5.0管理网卡及管理IP地址
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 公司的服务器基本上都是在IDC机房里面的,为了更有效的利用服务器性能.所以有几台服务器,安装的是ESXI5.0做成虚拟化. 注意目前这些服务器都是双网卡 ...
- Linux查询一台机器的IP地址和其对应的域名
Linux查询一台机器的IP地址和其对应的域名 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ nslookup > 127.0.0.1 Server: ...
- VMware 虚拟机 linux执行 ifconfig 命令 eth0没有IP地址(intet addr、Bcast、Mask) UP BROADCAST MULTICAST 问题
VMware 虚拟机 linux执行 ifconfig 命令 eth0没有IP地址(intet addr.Bcast.Mask) UP BROADCAST MULTICAST 问题 eth0:网络接口 ...
- Kali Linux常用服务配置教程获取IP地址
Kali Linux常用服务配置教程获取IP地址 下面以Kali Linux为例,演示获取IP地址的方法 (1)设置网络接口为自动获取IP地址.在Kali Linux的收藏夹中单击图标,将显示所有的程 ...
- 编写管理IP地址参数脚本(永久性)
1.用各种命令取出/etc/passwd文件前5行的最后一个字母.(2种) 2.编写管理IP地址参数脚本(永久性) a.只能用sed命令完成 b.提示用户变量赋值(IP.子网掩码.网关.DNS等) c ...
- 在linux(deepin)系统下查看当前ip地址与用户名
在linux(deepin)系统下查看当前ip地址与用户名 查看当前ip地址 方式一: hostname -I 方式二: ifconfig -a 如下图所示: 其中192.168.11.66即为当前系 ...
- 通过DHCP动态管理IP地址
DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP ...
随机推荐
- Spring Boot 启动第一个页面(Spring二)
在前面的文章里我配置好了spring boot的环境,并选择STS开发工具 具体文章见:https://blog.csdn.net/qq_38175040/article/details/105481 ...
- wsl 修改默认安装路径
如果已经装了,先删除 mklink /j C:\Users\XXXX\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rh ...
- JVM初认识
运行时数据区域 程序计数器:程序计数器是一块较小的内存空间,它可以看作是当前线程所执行的字节码的行号指示器.在虚拟机的概念模型里,字节码解释器工作时就是通过改变这个计数器的值来选取下一条需要执行的字节 ...
- 使用Spring Boot+MyBatis框架做查询操作
一.在你建立的工程下创建 Module 选择Spring initializr创建. 二.在Type处选择: Maven Project(项目的构建工具) 三.创建依赖时勾上web,mybatis,m ...
- 用华为HMS ML kit人体骨骼识别技术,Android快速实现人体姿势动作抓拍
你有没有过这种体验,拍照时对着镜头,脑子一片空白.表情僵硬.手和脚无处安放,最后拍出来的照片很是奇怪.拍照软件中的固定姿势抓拍功能可以帮助你:选择一个你想要的姿势模板,当你摆出同款姿势时,软件会进 ...
- nioServerChannel的的状态
转载自https://blog.csdn.net/zxhoo/article/details/17964353 Channel继承层次图分析上面提到的三个状态的时候,会去看Channel继承层次里某些 ...
- pytest封神之路第五步 参数化进阶
用过unittest的朋友,肯定知道可以借助DDT实现参数化.用过JMeter的朋友,肯定知道JMeter自带了4种参数化方式(见参考资料).pytest同样支持参数化,而且很简单很实用. 语法 在& ...
- Redis散列(Hash)的相关命令
散列 就像一个减配的Redis 内部及其类似Java的Map 内容就是key:value结构 hash类型在面向对象编程的运用中及其适合,因为它可以直接保存编程语言中的实体类关系 增 hset hse ...
- Java高级开发必会的50个性能优化的细节(珍藏版)
在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. ● 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短 ...
- Java源码赏析(一)Object 类
写这个系列的原因,其实网上已经有无数源码分析的文章了,多一篇不多,少一篇不少,但为什么还要写这部分文章呢?于私,其一,上班族已经很久没有打过完整的一整段有意义的话,算是锻炼个人的书写.总结能力,其二, ...