一,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地址的集合的更多相关文章

  1. 分享:linux系统如何快速阻止恶意IP地址

    可能你想要在各种情形下阻止有人通过IP地址访问你的Linux系统.比如说,作为最终用户,你可能想要保护自己,避免已知的间谍软件或跟踪者的IP地址.或者如果你在运行P2P软件,可能想要把来自与违反P2P ...

  2. Linux一块网卡添加多个IP地址

    环境: RHEL6.4 需求: Linux一块网卡添加多个IP地址 一.临时生效 1.1 网卡eth0添加一个IP地址 1.2 修改eth0:0的广播地址 二.永久生效 2.1 编辑ifcfg-eth ...

  3. 烂泥:更换ESXI5.0管理网卡及管理IP地址

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 公司的服务器基本上都是在IDC机房里面的,为了更有效的利用服务器性能.所以有几台服务器,安装的是ESXI5.0做成虚拟化. 注意目前这些服务器都是双网卡 ...

  4. Linux查询一台机器的IP地址和其对应的域名

    Linux查询一台机器的IP地址和其对应的域名 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ nslookup > 127.0.0.1 Server: ...

  5. 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:网络接口 ...

  6. Kali Linux常用服务配置教程获取IP地址

    Kali Linux常用服务配置教程获取IP地址 下面以Kali Linux为例,演示获取IP地址的方法 (1)设置网络接口为自动获取IP地址.在Kali Linux的收藏夹中单击图标,将显示所有的程 ...

  7. 编写管理IP地址参数脚本(永久性)

    1.用各种命令取出/etc/passwd文件前5行的最后一个字母.(2种) 2.编写管理IP地址参数脚本(永久性) a.只能用sed命令完成 b.提示用户变量赋值(IP.子网掩码.网关.DNS等) c ...

  8. 在linux(deepin)系统下查看当前ip地址与用户名

    在linux(deepin)系统下查看当前ip地址与用户名 查看当前ip地址 方式一: hostname -I 方式二: ifconfig -a 如下图所示: 其中192.168.11.66即为当前系 ...

  9. 通过DHCP动态管理IP地址

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP ...

随机推荐

  1. Javaweb项目重新编译方式

    project->clean选中需要重新编译的项目确定即可 我不知道重新编译的说法是否准确,但是我喜欢这么叫

  2. Linux下find与exec的联手干大事

    在Linux下工作,find命令绝对是一个非常高频的命令.我们可以用find命令来找到符合某些关键词的文件,找到某些日期的文件,也可以设定一些正则表达式,找到一系列满足该条件的文件. 但是,如果只有一 ...

  3. FastDFS不同步怎么破

    一.背景说明 FastDFS是一款开源的分布式文件系统,具体介绍就不说了,有兴趣的可以自行百度下. 以下是官方的架构图: 一次完整的写交互过程如下: 1.Client向Tracker查询可用的Stor ...

  4. java 泛型学习随笔

    对于java 泛型 编译时处理,运行时擦除的特点理解 对于编译时处理 在使用泛型相关的类或方法时,如果声明时的类型和具体使用时的类型不一致则直接会编译不通过 对于运行时擦除 当在运行时对两个相同类型但 ...

  5. 003.当在windows终端输入ipconfig时,显示不是内部或外部命令,也不是可运行的程序或批处理文件

    当在windows终端输入ipconfig时,显示不是内部或外部命令,也不是可运行的程序或批处理文件,这是环境变量的问题: 右键我的电脑→→→属性→→→高级系统设置→→→(高级)环境变量 在弹出的窗口 ...

  6. nohup命令重定向标准输出和错误输出

    命令:command > /dev/null  2>&1 & 输出到/dev/null表示输出重定向到黑洞,即输出内容不打印到屏幕上,null是/dev下空设备文件. &g ...

  7. ZooKeeper学习(二)ZooKeeper实现分布式锁

    一.简介 在日常开发过程中,大型的项目一般都会采用分布式架构,那么在分布式架构中若需要同时对一个变量进行操作时,可以采用分布式锁来解决变量访问冲突的问题,最典型的案例就是防止库存超卖,当然还有其他很多 ...

  8. 刷题[MRCTF2020]Ezpop

    解题思路 打开一看直接是代码审计的题,就嗯审.最近可能都在搞反序列化,先把反序列化的题刷烂,理解理解 代码审计 Welcome to index.php <?php //flag is in f ...

  9. jQurey轮播插件slides简单使用教程

    动态演示地址: http://www.zqunyan.com/zgproduction/slidesjs/index.html 简介就不多说了,网上有很多,复制粘贴没意义,会想到用这个插件就代表已经了 ...

  10. Restful 风格是什么?

    1.1 什么是RESTful 1. REST与技术无关,代表的是一种软件架构风格(REST是Representational State Transfer的简称,中文翻译为"表征状态转移&q ...