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 ...
随机推荐
- shell字体颜色应用
输出特效格式控制: \033[0m 关闭所有属性 \033[1m 设置高亮度 \03[4m 下划线 \033[5m 闪烁 \033[7m 反显 \033[8m ...
- linux(centos)下密码有效期和密码复杂度设置
1.密码有效期 方法一: chage -l 用户名 查看用户的过期时间 chage -M 99999 用户名 用命令修改过期时间为永久 chage -M 90 用户名 设置密码有效期为90天 chag ...
- JS数据类型及常用操作
1.字符串 2.数字类型 3.布尔类型 4.数组类型 5.字典
- PHP学习路线图(转)
PHP学习路线图 在网上很多人公布了太多的PHP学习路线图,本人在互联网公司工作十余年,也带了很多PHP入门的新手,将他们的一些问题和学习路线图为大家整理出来,希望很多小白少走弯路. 一. 网上某些错 ...
- Oracle闪回flashback
参考资料:Using Oracle Flashback Technology Oracle 11g的新特性闪回操作 闪回查询 闪回查询 闪回版本查询 闪回事务查询 闪回数据 闪回表 闪回删除 闪回数据 ...
- 初等函数——指数函数(Exponential Function)
一般地,函数叫做指数函数,其中x是自变量,函数的定义域是R.
- C#开发PACS医学影像处理系统(十七):2D处理之影像旋转和翻转
1.任意角度旋转 在XAML设计器中,设置RotateTransform属性 <InkCanvas x:Name="ToolInkCanvas" UseCustomCurso ...
- 格式化时间戳的n种方法
1 var time = new Date(ele.time).toLocaleString().split(' ')[0] 2 formatDate: function (value) { let ...
- CSAPP 第一章 计算机系统漫游
第一章 计算机系统漫游 C语言的起源:(系统级编程的首选) C语言与Unix操作系统关系密切 C语言小而简单:其设计由一个人掌控 C语言是为实践目的设计的:其设计用来实现Unix操作系统 C语言程序编 ...
- Spring Boot实战之Redis缓存登录验证码
1.工具类 1 import lombok.experimental.UtilityClass; 2 3 import java.awt.*; 4 import java.awt.image.Buff ...