firewalld

本文介绍了firewalld防火墙的基本操作,包括查看规则、根据服务名和端口添加和移除规则,以及使用富规则和ipset功能进行更精细的网络策略控制。适合有一定Linux基础并对firewalld有所了解的读者。
摘要由CSDN通过智能技术生成

目录

介绍

查看和控制firewalld服务

firewalld的控制命令: firewall-cmd

1. 查看firewalld规则

2. 根据 服务名称 添加规则

3. 根据 端口 添加规则

4. 移除 服务/端口 规则 (--remove-service/--remove-port)

5. 永久配置 (--per)

6. 重新加载防火墙配置

富规则

1. 添加富规则

2. 删除富规则

ipset(ip包)


防火墙在实际生产的服务器能严重的影响服务器的网络的访问和环境的使用,

一定要谨慎配置! 根据实际的 需求 进行合理的规划如何配置。

此文本只做一些常用 开通允许的策略 命令进行解释和介绍,不做配置文件的编写和解读。

本文适用:有linux基础 +了解firewalld防火墙(起码知道是干嘛的吧。。。)

ps:有什么不懂的,可以私信沟通,我会及时回复。

介绍

firewalld 防火墙是Centos7系统默认的防火墙管理工具,取代了之前的iptables防火墙,也是工作在网络层,属于包过滤防火墙。(其实firewalld防火墙也使用iptables作为底层实现机制)

firewalld缺省策略是拒绝所有传入连接,只允许已经明确配置的规则通过。

现在大部分的系统 默认都安装了firewalld防火墙工具,及在工作中会经常用到。

为了防止很多时候忘记如何配置防火墙,故对下面的一些简单的 命令进行记录。

查看和控制firewalld服务

  • 默认用systemctl系统工具进行管理即可
systemctl status firewalld

systemctl stop|start|reload firewalld

遇到的问题

在工作当中还遇到过,firwalld 启动失败的情况,因软件进程不光是用systemctl系统管理启动,也可以,在自己软件的家目录进行启动,要检查好你是以何种方式启动。

比如说,我在firewalld家目录的bin中直接启动,导致我无论如何也不能在systemctl再去启动该软件进程。

firewalld的控制命令: firewall-cmd

所以在启动后,“建议” 立即 检查firewalld的规则,是否存在 “对外” 需要开放的策略 避免造成业务丢失的情况。

1. 查看firewalld规则

firewall-cmd --list-all

2. 根据 服务名称 添加规则

允许外部访问http服务。

firewall-cmd --add-service=http

服务名称

在添加的过程中 ,firewalld 会查找预定义的服务列表,并找到与该服务名称匹配的服务。

firewalld 的预定义服务列表通常位于 /usr/lib/firewalld/services/ 目录

服务名称 包含了一组已知的端口和协议,通常对应于常见的网络服务。例如,"http"、"https"、"ssh" 等都是预定义的服务。

举例 查看 ssh

[root@localhost ~]# ls /usr/lib/firewalld/services/ |grep ssh
ssh.xml
[root@localhost ~]# cat /usr/lib/firewalld/services/ssh.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>SSH</short>
  <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
  <port protocol="tcp" port="22"/>
</service>

3. 根据 端口 添加规则

允许外部访问2222端口的服务。

firewall-cmd --add-port=2222/tcp

4. 移除 服务/端口 规则 (--remove-service/--remove-port)

移除(允许http服务访问的规则)

firewall-cmd --remove-service=http

移除(允许2222端口访问的规则)

firewall-cmd --remove-port=2222/tcp

5. 永久配置 (--per)

允许外部访问2222端口的服务。(永久)

firewall-cmd --add-port=2222/tcp --per

如果要永久的移除规则 在remove后面也要加上--per

重启服务,乃至重启服务器也不会掉的那种哦。🤡

6. 重新加载防火墙配置
firewall-cmd --reload

以上是基于 端口和服务名称 的简单规则配置

富规则

如果有些要求很高的安全规则 。

例如:只允许 固定ip访问 自己的端口, 这种策略应该怎么做呢?

就要用到firewalld的 富规则了

1. 添加富规则

允许192.168.1.1访问我的5555端口

firewall-cmd --add-rich "rule family="ipv4" source address=192.168.1.1 port port="5555" protocol="tcp" accept"

2. 删除富规则

删除刚刚添加的

firewall-cmd --remove-rich-rule='rule family="ipv4" source address=192.168.1.1 port port="5555" protocol="tcp" accept'

ipset(ip包)

需求:我要对很多ip做一个规则呢?

这种怎么办? 难道要一个ip一个ip的往上加富规则。

  • ipset(ip包)#可以对多个ip指定同一个规则
  • 下面举例 hash:ip(指定类型为 hash:ip,这种形式不允许重复而且只有一个ip)

参考的博客:https://www.cnblogs.com/architectforest/p/12973982.html

#添加一个规则ip包类型为hash:ip
#包名sshblock

firewall-cmd --permanent --new-ipset=sshblock --type=hash:ip

#默认文件存储的目录是:/etc/firewalld/ipsets
#查看文件是否存在了
more /etc/firewalld/ipsets/sshblock.xml

#在ipset当中添加一个ip
firewall-cmd --permanent --ipset=sshblock --add-entry=121.122.123.105

#在ipset当中删除一个ip
firewall-cmd --permanent --ipset=sshblock --remove-entry=121.122.123.105

#删除整个ipset
firewall-cmd --permanent --delete-ipset=sshblock



#富规则作用在ipset当中

firewall-cmd --permanent --add-rich "rule family="ipv4" source ipset="sshblock" port protocol="tcp" port="20-100" accept"

#删除富规则
firewall-cmd --remove-rich-rule='rule family="ipv4" source ipset="sshblock" port port="10050" protocol="tcp" accept' --permanent
  • 33
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值