如何在Linux系统中配置和管理防火墙规则?

如何在Linux系统中配置和管理防火墙规则?

💡 原文中文,约1100字,阅读约需3分钟。
📝

内容提要

本文介绍了Linux系统中的两个常用防火墙工具:iptables和firewalld,包括配置和管理规则的命令,以及注意事项。

🎯

关键要点

  • 本文介绍了Linux系统中的两个常用防火墙工具:iptables和firewalld。
  • iptables用于传统的iptables防火墙,firewalld用于较新的firewalld防火墙。
  • 使用iptables查看当前规则的命令是:sudo iptables -L。
  • 允许SSH连接的命令是:sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT。
  • 拒绝HTTP连接的命令是:sudo iptables -A INPUT -p tcp --dport 80 -j DROP。
  • 保存iptables规则的命令是:sudo service iptables save。
  • 重启iptables防火墙的命令是:sudo service iptables restart。
  • 使用firewalld查看当前规则的命令是:sudo firewall-cmd --list-all。
  • 允许SSH服务的命令是:sudo firewall-cmd --add-service=ssh --permanent。
  • 拒绝HTTP服务的命令是:sudo firewall-cmd --remove-service=http --permanent。
  • 保存firewalld规则的命令是:sudo firewall-cmd --runtime-to-permanent。
  • 重启firewalld防火墙的命令是:sudo systemctl restart firewalld。
  • 配置规则的顺序很重要,规则按照添加的顺序进行匹配。
  • 在iptables中,规则需要手动保存,而在firewalld中使用--permanent标志将规则永久保存。
  • 可以根据服务名或端口号配置规则,firewalld使用服务名作为高级抽象。
  • 建议在进行大规模更改之前备份防火墙规则,以防止配置错误。
  • 具体命令可能因Linux发行版和版本而有所不同,使用时应参考相应文档。
➡️

继续阅读