admin管理员组文章数量:1642335
工作区Zone说明
zone概念:
硬件防火墙默认一般有三个区,firewalld引入这一概念系统默认存在以下区域:
drop:默认丢弃所有包
block:拒绝所有外部连接,允许内部发起的连接
public:指定外部连接可以进入
external:这个不太明白,功能上和上面相同,允许指定的外部连接
dmz:和硬件防火墙一样,受限制的公共连接可以进入
work:工作区,概念和workgoup一样,也是指定的外部连接允许
home:类似家庭组
internal:信任所有连接
开启服务器的端口
firewall-cmd --add-port 8080/tcp
开启防火墙
systemctl start firewalld.service
重启防火墙
systemctl restart firewalld
防火墙开机启动
systemctl enable firewalld.service
关闭防火墙
systemctl stop firewalld.service
查看防火墙状态
firewall-cmd --state
查看防火墙开机时是否启动
systemctl list-unit-files | grep firewalld
查看区域信息
firewall-cmd--get-active-zones
查看指定接口所属区域
firewall-cmd--get-zone-of-interface=eth0
查看现有的规则
firewall-cmd --list-all
或
iptables -nL
重载防火墙配置
firewall-cmd --reload
添加单个单端口
firewall-cmd --permanent --zone=public --add-port=8080/tcp
参数说明:
--permanent 永久生效
--zone作用域
--add-port 添加一个端口
查看现有端口
firewall-cmd --zone=public --query-port=80/tcp
或
firewall-cmd --zone=public --list-ports
添加多个端口
firewall-cmd --permanent --zone=public --add-port=8080-8083/tcp
删除某个端口
firewall-cmd --permanent --zone=public --remove-port=81/tcp
针对某个 IP开放端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="6379" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.233" accept"
删除某个IP
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.1.51" accept"
针对一个ip段访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.0.0/16" accept"
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="9200" accept"
富规则
firewall-cmd --list-rich-rules #列出富规则
firewall-cmd --add-rich-rule=<rule> #使用富规则语言添加富规则
firewall-cmd --remove-rich-rule=<rule> #移除富规则
firewall-cmd --query-rich-rule=<rule> #查询某条富规则是否存在
firewall-cmd --list-rich-rules #列出指定区域中的所有富规则
add-rich-rule 语法:
rule [source] [destination] service|port|protocol|icmp-block|masquerade|forward-port [log] [audit] [accept|reject|drop]
rule [family="ipv4|ipv6"]
source address="address[/mask]" [invert="True"]
destination address="address[/mask]" invert="True"
service name="service name"
port port="port value" protocol="tcp|udp"
protocol value="protocol value"
forward-port port="port value" protocol="tcp|udp" to-port="port value
" to-addr="address"
log [prefix="prefix text"] [level="log level"] [limit value="rate/duration"]
accept | reject [type="reject type"] | drop
后悔药 调试使用
firewall-cmd ... --timeout=30 # 30s后删除这条规则,防止把自己关在门外,可选单位5m 分钟,1h 小时
添加操作后别忘了执行重载
firewall-cmd --reload #重新载入规则,已建立的连接不会中断
firewall-cmd --complete-reload #重新载入规则,断开所有已建立的连接
其他设定
firewall-cmd --set-default-zone=work #设定默认区为work
firewall-cmd --get-default-zone #查询默认区域
firewall-cmd --set-default-zone=trusted #设置默认区域
firewall-cmd --get-active-zones #查看活动区域信息
firewall-cmd --list-all-zones #显示所有区域
firewall-cmd --zone=public --add-interface=eth0 #将接口加入public区域
firewall-cmd --zone=public --change-interface=eth0 #将接口移动到public区域
firewall-cmd --zone=public --remove-interface=eth0 #将接口移出public区域
firewall-cmd --get-zone-of-interface=eth0 #查询接口所属区域
firewall-cmd --panic-on #紧急情况拒绝所有的包
firewall-cmd --panic-off #恢复正常模式
firewall-cmd –query-panic #查询紧急状态
firewall-cmd --permanent --query-port=8080/tcp #查询端口是否开放
firewall-cmd --add-service=mysql # 开放mysql端口
firewall-cmd --remove-service=http # 阻止http端口
firewall-cmd --list-services # 查看开放的服务
firewall-cmd --add-port=3306/tcp # 开放通过tcp访问3306
firewall-cmd --remove-port=80tcp # 阻止通过tcp访问3306
firewall-cmd --add-port=233/udp # 开放通过udp访问233
firewall-cmd --list-ports # 查看开放的端口
firewall-cmd --query-masquerade # 检查是否允许伪装IP
firewall-cmd --add-masquerade # 允许防火墙伪装IP
firewall-cmd --remove-masquerade# 禁止防火墙伪装IP
# 将80端口的流量转发至8080
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
# 将80端口的流量转发至
firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.0.1192.168.0.1
# 将80端口的流量转发至192.168.0.1的8080端口
firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1:toport=8080
#查看所有已注册的服务名称
firewall-cmd --get-service
# 查询服务的启用状态
firewall-cmd --query-service ftp
PS1:我觉得firewall并没有iptables的四表五链简单明了
参考文献
- 官方网站 firewalld
- firewalld详解 - 钟桂耀 - 博客园
- Firewalld - Document - Zone - Predefined Zones
- Documentation - Manual Pages - firewalld.richlanguage | firewalld
版权声明:本文标题:firewall-cmd 常用命令 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1729335092a1196875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论