centos

怎样用CentOS iptables阻止特定IP

小樊
59
2025-07-22 15:11:27
栏目: 智能运维

要使用 CentOS 的 iptables 阻止特定 IP 地址,您可以按照以下步骤操作:

  1. 打开终端。

  2. 输入以下命令以阻止特定 IP 地址(将 x.x.x.x 替换为您要阻止的 IP 地址):

sudo iptables -A INPUT -s x.x.x.x -j DROP

这将把来自指定 IP 地址的所有数据包丢弃,从而阻止该 IP 地址访问您的服务器。

  1. 若要查看当前的 iptables 规则,请输入以下命令:
sudo iptables -L
  1. 如果您想保存更改,以便在系统重启后仍然生效,您需要安装 iptables-services 软件包(如果尚未安装):
sudo yum install iptables-services
  1. 启动并启用 iptables 服务:
sudo systemctl start iptables
sudo systemctl enable iptables

现在,您已经成功阻止了特定 IP 地址访问您的 CentOS 服务器。如果您以后想取消阻止该 IP 地址,只需删除相应的 iptables 规则即可:

sudo iptables -D INPUT -s x.x.x.x -j DROP

0
看了该问题的人还看了