温馨提示×

centos防火墙如何配置IP黑名单

小樊
49
2025-06-28 11:15:33
栏目: 智能运维

在CentOS系统中,你可以使用firewalld来配置IP黑名单。以下是具体步骤:

方法一:使用firewalld的富规则(rich rules)

  1. 安装firewalld(如果尚未安装):

    sudo yum install firewalld 
  2. 启动并启用firewalld服务

    sudo systemctl start firewalld sudo systemctl enable firewalld 
  3. 添加IP黑名单: 使用firewall-cmd命令添加富规则来阻止特定IP地址。

    sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" reject' 

    这里的192.168.1.100是你想要阻止的IP地址。你可以根据需要替换为你想要阻止的IP地址。

  4. 重新加载firewalld配置

    sudo firewall-cmd --reload 
  5. 验证规则是否添加成功

    sudo firewall-cmd --list-all 

方法二:使用iptables

如果你更喜欢使用iptables,可以按照以下步骤操作:

  1. 安装iptables(如果尚未安装):

    sudo yum install iptables 
  2. 启动并启用iptables服务

    sudo systemctl start iptables sudo systemctl enable iptables 
  3. 添加IP黑名单: 使用iptables命令添加规则来阻止特定IP地址。

    sudo iptables -A INPUT -s 192.168.1.100 -j DROP 

    这里的192.168.1.100是你想要阻止的IP地址。你可以根据需要替换为你想要阻止的IP地址。

  4. 保存iptables规则: CentOS 7及以上版本使用firewalld,但你可以手动保存iptables规则。

    sudo service iptables save 
  5. 验证规则是否添加成功

    sudo iptables -L -n 

注意事项

  • 永久生效:使用--permanent选项可以使规则在系统重启后仍然有效。
  • 安全性:确保你阻止的IP地址是正确的,以免误封合法用户。
  • 备份:在进行任何更改之前,建议备份当前的防火墙配置。

通过以上步骤,你可以在CentOS系统中成功配置IP黑名单。

0