首页 > 文章列表 > 深入了解 Linux 防火墙配置(iptables和firewalld)的指南

深入了解 Linux 防火墙配置(iptables和firewalld)的指南

linux 防火墙 iptables
243 2024-02-20

Linux 防火墙配置(iptables和firewalld)详细教程。

下面是一个简要的 Linux 防火墙配置教程,涵盖了iptables和firewalld两种常用的防火墙工具。

iptables是Linux上最常用的防火墙工具之一,而firewalld是CentOS 7及其衍生版本中默认使用的防火墙管理工具。

iptables 防火墙配置:

  1. 查看当前防火墙规则:

    iptables -L -n
  2. 清空当前的防火墙规则:

    iptables -F
  3. 允许特定端口的入站连接:

    iptables -A INPUT -p <协议> --dport <端口号> -j ACCEPT

    例如,允许TCP协议的80端口:

    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
  4. 允许特定IP地址范围的入站连接:

    iptables -A INPUT -s <IP地址/子网掩码> -j ACCEPT

    例如,允许来自192.168.0.0/24子网的连接:

    iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT
  5. 阻止所有入站连接:

    iptables -P INPUT DROP
  6. 保存配置:

    service iptables save

firewalld 防火墙配置:

  1. 查看当前防火墙规则:

    firewall-cmd --list-all
  2. 允许特定端口的入站连接:

    firewall-cmd --zone=public --add-port=<端口号>/tcp --permanent

    例如,允许TCP协议的80端口:

    firewall-cmd --zone=public --add-port=80/tcp --permanent
  3. 允许特定IP地址范围的入站连接:

    firewall-cmd --zone=public --add-source=<IP地址/子网掩码> --permanent

    例如,允许来自192.168.0.0/24子网的连接:

    firewall-cmd --zone=public --add-source=192.168.0.0/24 --permanent
  4. 阻止所有入站连接:

    firewall-cmd --zone=public --set-default=drop
  5. 重载防火墙配置:

    firewall-cmd --reload

以上只是一些常见的iptables和firewalld命令示例,你可以根据自己的需求进行修改和扩展。请注意,在配置防火墙时务必小心,确保不会阻塞你所需要的合法流量,并确保保存和加载配置以使其生效。另外,建议在配置防火墙之前备份现有的防火墙规则以防止意外情况发生。