温馨提示×

CentOS防火墙如何阻止恶意IP访问

小樊
45
2025-09-05 17:34:50
栏目: 智能运维

在CentOS系统中,你可以使用firewalld或iptables来阻止恶意IP访问。以下是两种方法的详细步骤:

使用firewalld

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

    sudo yum install firewalld
    
  2. 启动firewalld服务

    sudo systemctl start firewalld
    
  3. 启用firewalld服务(使其在系统启动时自动运行):

    sudo systemctl enable firewalld
    
  4. 添加规则阻止恶意IP

    sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="恶意IP地址" reject'
    

    例如,要阻止IP地址为192.168.1.100的访问:

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

    sudo firewall-cmd --reload
    

使用iptables

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

    sudo yum install iptables-services
    
  2. 启动iptables服务

    sudo systemctl start iptables
    
  3. 启用iptables服务(使其在系统启动时自动运行):

    sudo systemctl enable iptables
    
  4. 添加规则阻止恶意IP

    sudo iptables -A INPUT -s 恶意IP地址 -j DROP
    

    例如,要阻止IP地址为192.168.1.100的访问:

    sudo iptables -A INPUT -s 192.168.1.100 -j DROP
    
  5. 保存iptables规则

    sudo service iptables save
    

注意事项

  • 备份现有规则:在修改防火墙规则之前,建议备份现有的规则,以便在需要时可以恢复。
  • 测试规则:在生产环境中应用新规则之前,先在测试环境中验证规则的有效性。
  • 定期更新:定期检查和更新防火墙规则,以确保它们仍然有效并适应新的安全威胁。

通过以上步骤,你可以有效地阻止恶意IP访问你的CentOS系统。

0