首页 >运维 >CentOS >如何使用CentOS上的防火墙实施高级防火墙规则?

如何使用CentOS上的防火墙实施高级防火墙规则?

Karen Carpenter
Karen Carpenter原创
2025-03-11 16:59:19888浏览

使用CENTOS上的FireWalld实施高级防火墙规则

本节详细介绍了如何在CentOS系统上使用 firewalld 实施高级防火墙规则。 FireWalld 提供了一种强大而灵活的方式来管理防火墙,而不是简单的端口开放。 Its strength lies in its zone-based architecture and the ability to define complex rules using rich syntax.

First, ensure firewalld is installed and running:

<code class="bash">sudo yum install firewalld sudo systemctl start firewalld sudo systemctl enable firewalld</code>

Advanced rules are typically added within a specific区域。 默认区域通常适用于公共接口,而其他类似内部 dmz 的其他则分别是为内部网络或非通用区域创建的。假设我们只想从默认区域上的特定IP地址(192.168.1.100)允许SSH访问。我们可以使用 firewall-cmd 命令行工具:

 <code class="“" bash> sudo firewall-cmd -permanent-permanent -Add-rich-rule ='rule family ='ipv4&quot,源地址='192.168.1.100&quot;接受'sudo firewall-cmd  -  reload </code> 

此命令将永久规则(使用 - 永久)添加到 default Zone。 - 添加富含rule 选项允许在类似XML的语法中指定的复杂规则。该规则专门针对IPv4流量( family =; ipv4; ),源自 192.168.1.100 并接受它( Accept )。请记住,使用重新加载<code> firewalld - 重新加载以进行更改。您可以在 Rich Rule 中添加更复杂的条件,例如端口范围,协议(TCP/UDP)和其他条件。例如,仅允许该IP中的SSH(端口22):

 <code class="“" bash> sudo firewall-cmd -permanent-permanent-add-rich-rule ='rule family ='ipv4&quot;源地址='192.168.1.100&quot;端口协议=“ TCP”端口=“ 22”接受'sudo firewall-cmd  -  reload </code> 

您可以使用:

 <pre class="brush:php;toolbar:false"> <pre class="brush:php;toolbar:false"> <code class="“" bash> sudo firewall-cmd -list-list-allist-all sudo firewall-cmd -list-cmd-list-code-code-code> </code>

使用 firewalld 有效地确保Centos服务器需要分层的方法:

  1. 最少特权的原则:仅允许必要的服务和端口。避免不必要地打开端口。
  2. 基于区域的安全性:利用不同的区域(例如, public internal , dmz dmz )来隔离网络流量并将适当的规则应用于每个区域。这通过限制了违规的影响来提高安全性。
  3. 颗粒控制的丰富规则:使用 acter> rich规则根据源IP地址,端口,协议和其他条件定义高度特定的访问控制。>
  4. -list-all 和 sudo firewall-cmd-富含list-riCh-rules ,以确保它们仍然合适并且不妥协。
  5. 输入过滤:优先级输入过滤。默认情况下阻止所有传入的连接,并明确允许必要的连接。
  6. 禁用不必要的服务:停止并禁用您不需要的任何服务。这减少了攻击表面。
  7. 强密码和身份验证:实现强密码,并使用诸如SSH键之类的强大身份验证机制。 Firewall rules alone are not sufficient for complete security.
  8. Regular Updates: Keep your CentOS system and firewalld up-to-date with the latest security patches.
  9. Log Analysis: Monitor firewall logs for suspicious activity.这可以帮助检测并响应潜在的入侵。
  10. fail2ban:考虑使用 fail2ban firewalld 一起使用 fail2ban fail2ban 自动禁止试图闯入登录的IP地址。

允许通过CENTOS上的FireWalld在CENTOS上进行特定应用程序或服务

的特定端口和协议

允许应用程序和协议的特定端口和协议涉及该端口的特定端口和协议。 For example, to allow HTTP traffic (port 80) and HTTPS traffic (port 443):

<code class="bash">sudo firewall-cmd --permanent --add-port=80/tcp sudo firewall-cmd --permanent --add-port=443/tcp sudo firewall-cmd --reload</code>

For more complex涉及特定IP地址或其他条件的方案,使用 Rich规则

 <pre class="brush:php;toolbar:false"> <code class="“" bash> sudo firewall-cmd-permanent -permanent-add-rich-rich-rule ='rule family ='ipv4&quot;源地址='192.168.1.100&quot;端口协议=“ TCP”端口=“ 8080”接受'sudo firewall-cmd-reload </code> 

这允许从IP地址192.168.1.100上的端口8080上的TCP流量。请记住,将这些值替换为您的特定应用程序的适当端口,协议和IP地址。始终明确指定协议(TCP或UDP)。

常见的故障排除步骤,以解决Centos System上复杂的FireWALLD规则

解决问题的问题

firewalld firewalld

-list-all 和 sudo firewall-cmd - 含量富含rich-rules 确认您的规则正确添加并活动。

  • 检查区域分配:确保规则与正确的区域相关联(EG, public ,,,,, code> cod cod> code> code>)。使用 sudo firewall-cmd- get-active-exones 列出活动区域及其接口。
  • 检查日志:检查 firewalld firewalld logs groun logs in错误或警告。日志文件位置可能会根据系统的配置而有所不同,但通常在/var/log/log/firewalld/
  • 测试连接性:使用 ping telnet code> code> netstat test nc nc nc nc nc rules.
  • Simplify Rules: If you have many complex rules, try temporarily disabling some to isolate the problematic rule.
  • Restart firewalld: After making changes to your rules, always reload firewalld using sudo firewall-cmd --reload. In stubborn cases, a full restart (sudo systemctl restart firewalld) might be necessary.
  • Use iptables (Advanced): For very complex scenarios, you can directly manipulate the underlying iptables rules, though this is generally discouraged unless you are very familiar with iptables. However, remember that changes made directly to iptables will be overwritten when firewalld is reloaded.
  • Consult Documentation: Refer to the official firewalld documentation for detailed information on syntax, options, and troubleshooting tips.
  • By following these steps and best practices, you can effectively使用 FireWalld 在CentOS服务器上管理和故障排除高级防火墙规则,增强其安全性和稳定性。

    以上是如何使用CentOS上的防火墙实施高级防火墙规则?的详细内容。更多信息请关注PHP中文网其他相关文章!

    声明:
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn