linux

Linux系统中如何配置防火墙

小樊
38
2025-04-16 11:09:31
栏目: 智能运维

在Linux系统中,配置防火墙通常涉及使用iptables或firewalld这样的工具。以下是使用这两种工具配置防火墙的基本步骤:

使用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. 允许特定端口

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

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

    • 对于Debian/Ubuntu系统:
      sudo iptables-save > /etc/iptables/rules.v4
      
    • 对于Red Hat/CentOS系统:
      sudo service iptables save
      
  7. 重启防火墙服务

    • 对于Debian/Ubuntu系统:
      sudo systemctl restart iptables
      
    • 对于Red Hat/CentOS系统:
      sudo systemctl restart iptables.service
      

使用firewalld配置防火墙

  1. 查看当前区域和规则

    sudo firewall-cmd --list-all
    
  2. 允许特定IP访问

    sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept'
    
  3. 拒绝特定IP访问

    sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.101" reject'
    
  4. 允许特定端口

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

    sudo firewall-cmd --reload
    
  6. 查看防火墙状态

    sudo firewall-cmd --state
    

注意事项

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

0
看了该问题的人还看了