温馨提示×

Linux Trigger:防火墙怎么配置

小樊
46
2025-07-12 11:29:42
栏目: 智能运维

在Linux系统中,配置防火墙通常使用iptablesfirewalld这两个工具。以下是关于如何使用这两个工具进行防火墙配置的基本指南。

使用iptables配置防火墙

  1. 查看现有规则

    sudo iptables -L -n -v
    
  2. 允许特定IP访问

    sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT
    
  3. 拒绝特定IP访问

    sudo iptables -A INPUT -s 192.168.1.101 -j DROP
    
  4. 允许特定端口访问(例如,允许TCP端口80):

    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    
  5. 拒绝所有入站连接

    sudo iptables -P INPUT DROP
    
  6. 保存规则

    sudo iptables-save > /etc/iptables/rules.v4
    
  7. 恢复规则

    sudo iptables-restore < /etc/iptables/rules.v4
    

使用firewalld配置防火墙

  1. 查看现有区域和规则

    sudo firewall-cmd --list-all
    
  2. 添加一个新区域(例如,trusted):

    sudo firewall-cmd --permanent --new-zone=trusted
    
  3. 将接口添加到区域(例如,eth0):

    sudo firewall-cmd --permanent --zone=trusted --add-interface=eth0
    
  4. 允许特定IP访问

    sudo firewall-cmd --permanent --zone=trusted --add-source=192.168.1.100
    
  5. 允许特定端口访问(例如,允许TCP端口80):

    sudo firewall-cmd --permanent --zone=trusted --add-port=80/tcp
    
  6. 重新加载防火墙配置

    sudo firewall-cmd --reload
    
  7. 查看所有区域

    sudo firewall-cmd --get-zones
    

注意事项

  • 备份配置:在进行任何更改之前,建议备份现有的防火墙配置。
  • 测试更改:在生产环境中应用更改之前,先在测试环境中进行测试。
  • 权限:大多数防火墙命令需要root权限,因此请确保使用sudo

通过以上步骤,您可以在Linux系统上配置基本的防火墙规则。根据您的具体需求,可能需要进一步调整和优化这些规则。

0