Skip to content

通过知识获得解放,通过技术获得自由

Menu
  • 专题目录
  • 液压相关
    • 液压技术
    • 液压相邻技术
    • 液压应用
  • 计算机相关
    • 计算机和软件
    • 网络和网站技术
  • 哲学
  • 关于本站
Menu

linux的防火墙firewalld[0]

Posted on 2023年11月14日 by

linux防火墙和centos7的防火墙设置问题。

firewalld是centos管理iptables的前端,用于简化iptables的命令。

在系统安装程序时,一般推荐暂时性关闭防火墙,待安装完成后,再开放相应的端口

systemctl stop firewalld
systemctl disable firewalld #禁止开机启动
systemctl start firewalld
systemctl restart firewalld #再次开启
systemctl status firewalld  #防火墙状态

对于后期需要开放的端口的常用设置参照如下设置:

一般开放的服务时http和https,端口是80和443,其他常见端口见网络常用端口号

iptables -I INPUT -p tcp --dport 12345 -j REJECT 
firewall-cmd --zone=public --add-service=https --permanent #对外开放的服务
firewall-cmd --zone=public --add-port=8080-8081/tcp --permanent #对外开放的端口

几种不同的初始化区域:FirewallD的默认区域是public

配置文件位置:/usr/lib /firewalld/zones/

阻塞区域(block):任何传入的网络数据包都将被阻止。
工作区域(work):相信网络上的其他计算机,不会损害你的计算机。
家庭区域(home):相信网络上的其他计算机,不会损害你的计算机。
公共区域(public):不相信网络上的任何计算机,只有选择接受传入的网络连接。
隔离区域(DMZ):隔离区域也称为非军事区域,内外网络之间增加的一层网络,起到缓冲作用。对于隔离区域,只有选择接受传入的网络连接。
信任区域(trusted):所有的网络连接都可以接受。
丢弃区域(drop):任何传入的网络连接都被拒绝。
内部区域(internal):信任网络上的其他计算机,不会损害你的计算机。只有选择接受传入的网络连接。
外部区域(external):不相信网络上的其他计算机,不会损害你的计算机。只有选择接受传入的网络连接。

firewall使用参考

查看版本: 
firewall-cmd --version
查看帮助: 
firewall-cmd --help
查看设置:
显示状态:
firewall-cmd --state
查看区域信息: 
firewall-cmd --get-active-zones
查看指定接口所属区域:
firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:
firewall-cmd --panic-on
取消拒绝状态:
firewall-cmd --panic-off
查看是否拒绝:
firewall-cmd --query-panic 
更新防火墙规则:
firewall-cmd --reload
firewall-cmd --complete-reload
两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务
 
将接口添加到区域,默认接口都在public
firewall-cmd --zone=public --add-interface=eth0
永久生效再加上 --permanent 然后reload防火墙
 
设置默认接口区域
firewall-cmd --set-default-zone=public
立即生效无需重启
 
打开端口(貌似这个才最常用)
查看所有打开的端口:
firewall-cmd --zone=dmz --list-ports
加入一个端口到区域:
firewall-cmd --zone=dmz --add-port=8080/tcp
若要永久生效方法同上
 
打开一个服务,类似于将端口可视化,服务需要在配置文件中添加,/etc/firewalld 目录下有services文件夹,这个不详细说了,详情参考文档
firewall-cmd --zone=work --add-service=smtp
 
移除服务
firewall-cmd --zone=work --remove-service=smtp

以下是centos7 的设置参考。

  • 防火墙的命令
systemctl stop firewalld
systemctl start firewalld
systemctl status firewalld

在firewalld运行状态下,查看防火墙的默认、活跃区域

firewall-cmd --get-default-zone
firewall-cmd --get-active-zones

  • 服务设置
systemctl stop firewalld
#临时放行端口,可用于firewalld未运行状态
iptables -A INPUT -p tcp --dport 7000 -j ACCEPT  #用于frp
iptables -A INPUT -p tcp --dport 29698 -j ACCEPT #用于ssh
systemctl restart firewalld
#永久放行
firewall-cmd --permanent --zone=public --add-port=29698/tcp #用于ssh
firewall-cmd --permanent --zone=public --add-port=7000/tcp #用于frp
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp

查看已经暴露的端口

firewall-cmd --permanent --list-port

查看已经暴露的服务

firewall-cmd --zone=public --list-all

firewall-cmd --zone=public --add-service=ssh
firewall-cmd --zone=public --remove-service=dhcpv6-client
firewall-cmd --zone=public --remove-service=cockpit
firewall-cmd --zone=public --list-all #再次检查

  • vps端口检查
netstat -lntp

  • 防火墙的AllowZoneDrifting警告
nano /etc/firewalld/firewalld.conf

将最后的设置为:

AllowZoneDrifting=no

重启防火墙

systemctl restart firewalld
  • 补充阅读:

iptables介绍和常用方式

 

参考资料:

  1. https://www.jianshu.com/p/5825b18c80a1
  2. https://zhuanlan.zhihu.com/p/35335800

欢迎回来

希望本站对你有所帮助!

如有疑问请联系info@fanlog.org
2023 年 11 月
一二三四五六日
 12345
6789101112
13141516171819
20212223242526
27282930 
« 6 月    

AI辅助 (17)

© 2025 | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT