IT袋

当前位置:主页 > 经验教程 > 系统教程 >

Linux防火墙之Firewalld

Linux防火墙之Firewalld(5)

时间:2024-01-19 13:40:39 来源:IT袋 作者:马勇
导读:Linux防火墙之Firewalld,————以上是系统定义的所有的区域(Zone),但是,不是所有的区域(Zone)都在使用,只有活跃的区域(Zone)才有实际操作意义。 注意:因为默认区

Linux防火墙之Firewalld

————以上是系统定义的所有的区域(Zone),但是,不是所有的区域(Zone)都在使用,只有活跃的区域(Zone)才有实际操作意义。

注意:因为默认区域只允许ssh和dhcp,所以在没有任何配置的情况下默认是拒绝ping包的。

常用命令

# 查看所有放行端口
firewall-cmd --zone=public --list-ports
 
# 禁止IP访问机器
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="192.168.0.1" drop'
 
# 禁止一个IP段,比如禁止192.168.*.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="192.168.0.1/16" drop'
 
# 禁止一个IP段,比如禁止192.168.0.*
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="192.168.0.1/24" drop'
 
# 禁止机器IP从防火墙中删除
firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address="192.168.0.1" drop'
 
# 允许http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --add-service=http
 
# 关闭http服务(对应服务策略目录:/usr/lib/firewalld/services/)
firewall-cmd --permanent --remove-service=http
 
# 允许端口:3306
firewall-cmd --permanent --add-port=3306/tcp
 
# 允许端口:1-3306
firewall-cmd --permanent --add-port=1-3306/tcp
 
# 关闭放行中端口:3306
firewall-cmd --permanent --remove-port=3306/tcp
 
# 查看firewall的状态
firewall-cmd --state    
 
# 查看防火墙规则(只显示/etc/firewalld/zones/public.xml中防火墙策略)
firewall-cmd --list-all
 
# 查看所有的防火墙策略(即显示/etc/firewalld/zones/下的所有策略)
firewall-cmd --list-all-zones
 
# 重新加载配置文件
firewall-cmd --reload
 
# 更改配置后一定要重新加载配置文件
firewall-cmd --reload
# Postgresql端口设置。允许192.168.142.166访问5432端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port port="5432" protocol="tcp" accept"
# redis端口设置。允许192.168.142.166访问6379端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port port="6379" protocol="tcp" accept"
# beanstalkd端口设置。允许192.168.142.166访问11300端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port port="11300" protocol="tcp" accept"
# 查看防火墙端口列表
firewall-cmd --list-ports
# 添加指定端口tcp
firewall-cmd --zone=public --add-port=8080/tcp --permanent          # 开放8080/tcp端口 
firewall-cmd --zone=public --add-port=10002-10010/tcp --permanent  # 开放10002-10010/tcp端口范围
# 添加指定端口udp
firewall-cmd --zone=public --add-port=9200/udp --permanent       # 开放9200/udp端口
firewall-cmd --zone=public --add-port=20015-20020/udp --permanent  # 开放20015-20020/udp端口范围
# 删除指定端口
firewall-cmd --zone= public --remove-port=19800/tcp --permanent     # 删除已开放的19880/tcp端口
firewall-cmd --zone= public --remove-port=9200-9300/udp --permanent  # 删除已开放的9200-9300/udp 端口范围
# 热加载防火墙,使之生效
firewall-cmd --reload
# 指定某IP访问某端口
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.0.107" port protocol="tcp" port="3306" accept"
# 删除策略
firewall-cmd --permanent --zone=public --remove-rich-rule="rule family="ipv4" source address="192.168.0.107" port protocol="tcp" port="3306" accept"
# 指定某个网段访问某个端口范围
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="11.76.168.0/24" port protocol="udp" port="1-65535" accept"
# 删除策略
firewall-cmd --permanent --zone=public --remove-rich-rule="rule family="ipv4" source address="11.76.168.0/24" port protocol="tcp" port="1-65535" accept"
# 禁止指定ip 访问某个端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.200" port protocol="tcp" port="80" reject"
 
# 禁止某个段的ip 访问某个端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="80" reject"
 
# 允许指定ip 访问所有端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.100" port protocol="tcp" accept"
# 允许指定ip段 访问所有端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" accept"
# 允许192.168.1.10所有访问所有端口
firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.1.10" accept' --permanent
# 移除192.168.1.10所有访问所有端口
firewall-cmd --zone=public --remove-rich-rule 'rule family="ipv4" source address="192.168.1.10" accept' --permanent
# 允许192.168.2.0/24(0-255)所有访问所有端口
firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.2.0/24" accept' --permanent
# 允许192.168.1.10所有访问TCP协议的22端口
firewall-cmd --zone=public --add-rich-rule 'rule family="ipv4" source address="192.168.1.10" port port=22 protocol=tcp reject' --permanent
# 移除192.168.1.10所有访问TCP协议的22端口
firewall-cmd --zone=public --remove-rich-rule 'rule family="ipv4" source address="192.168.1.10" port port=22 protocol=tcp reject' --permanent
# 防火墙重新载入(必须重新载入后才能生效)
firewall-cmd --reload
# 查看rich-rules(富规则)
firewall-cmd --list-rich-rules
# 查看防火墙服务规则
firewall-cmd --list-services
# 查看防火墙所有规则
firewall-cmd --list-all
# 查看防火墙所有区域的配置规则
firewall-cmd --list-all-zones
# 查看默认区域
firewall-cmd --get-default-zone
# 查看网络接口使用区域
firewall-cmd --get-active-zones
# 查看默认的可用服务
firewall-cmd --get-services
# 要启用或禁用HTTP服务
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --zone=public --remove-service=http --permanent
# 移除现有规则  (此步骤相当重要,很多文章和博客都没提及到)
firewall-cmd --permanent --zone=public --remove-port=80/tcp
firewall-cmd --reload
 
# 在192.168.100.100  102上测试访问
curl  192.168.100.101
发现均无法再访问101的80端口
# 设置规则
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.100.102" port protocol="tcp" port="80" accept"
firewall-cmd --reload
# 测试访问
curl 192.168.100.101  
# 100无法访问  102可以访问
至此实现了通过防火墙firewalld设置规则,指定ip访问指定端口

以上分享的Linux防火墙之Firewalld的具体内容,供大家参考操作。

相关阅读

  • win10如何设置局域网 办公室局域网搭建教程

    win10如何设置局域网 办公室局域网搭建教程

    全面的为您讲解win10如何设置局域网和办公室局域网搭建教程的IT小经验,接下来就是全面介绍。 说到局域网,其实在网络应用中比较常见,而局域网又称作内网,其中的内部电脑可以实现文件

  • win10 kms激活工具怎么用 了解kms激活windows教程

    win10 kms激活工具怎么用 了解kms激活windows教程

    为大家介绍的是win10及kms激活工具怎么用的相关经验,很不错的方法小知识,建议收藏哦! 我们日常使用的电脑大多数安装的都是windows10系统,为了更好地使用我们一般都会选择激活操作系统

  • win10office卸载教程 windows10office卸载了怎么恢复

    win10office卸载教程 windows10office卸载了怎么恢复

    全面为您解析win10office卸载教程的内容,关于win10office卸载教程 windows10office卸载了怎么恢复,接下来IT袋小编为大家介绍。 office在电脑上一般作用于办公的小伙伴,而玩游戏的小伙伴一般不需要

  • iso安装win10使用什么模式 电脑装win10系统教程

    iso安装win10使用什么模式 电脑装win10系统教程

    一篇方法教程,与您分享iso安装win10使用什么模式和电脑装win10系统教程方面的讲解,下面小编为您详细解答 原版Win10系统安装教程复杂吗?说实话并不是很复杂,在PE系统不断跟随Win10脚步的同