linux

Linux防火墙怎样配置IP黑名单

小樊
34
2025-11-30 16:25:45
栏目: 智能运维

在Linux系统中,可以使用iptables或firewalld来配置IP黑名单。以下是两种方法的详细步骤:

使用iptables配置IP黑名单

  1. 查看当前iptables规则

    sudo iptables -L -n -v
    
  2. 添加黑名单规则 假设要将IP地址192.168.1.100加入黑名单,阻止其所有流量:

    sudo iptables -A INPUT -s 192.168.1.100 -j DROP
    sudo iptables -A OUTPUT -d 192.168.1.100 -j DROP
    
  3. 保存iptables规则 不同的Linux发行版保存iptables规则的方法不同:

    • Debian/Ubuntu:
      sudo iptables-save > /etc/iptables/rules.v4
      
    • CentOS/RHEL:
      sudo service iptables save
      
      或者
      sudo systemctl enable iptables
      sudo systemctl start iptables
      
  4. 查看黑名单规则

    sudo iptables -L -n -v | grep DROP
    

使用firewalld配置IP黑名单

  1. 查看当前firewalld状态

    sudo firewall-cmd --state
    
  2. 添加黑名单规则 假设要将IP地址192.168.1.100加入黑名单,阻止其所有流量:

    sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" reject'
    sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" destination address="192.168.1.100" reject'
    
  3. 重新加载firewalld配置

    sudo firewall-cmd --reload
    
  4. 查看黑名单规则

    sudo firewall-cmd --list-all
    

注意事项

通过以上步骤,你可以成功地在Linux系统中配置IP黑名单,从而阻止特定IP地址的访问。

0
看了该问题的人还看了