温馨提示×

怎样限制CentOS SSH的访问IP

小樊
45
2025-03-13 10:29:28
栏目: 智能运维
Centos服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

要限制 CentOS SSH 的访问 IP,您可以使用防火墙(如 firewalld 或 iptables)或者使用 TCP Wrappers

方法一:使用 firewalld

  1. 首先,确保您已经安装了 firewalld。如果没有,请运行以下命令安装:
sudo yum install firewalld
  1. 启动 firewalld 服务:
sudo systemctl start firewalld
  1. 允许 SSH 连接:
sudo firewall-cmd --permanent --add-service=ssh
  1. 限制访问 IP。将 192.168.1.100 替换为您希望允许访问的 IP 地址:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'
  1. 重新加载 firewalld 配置:
sudo firewall-cmd --reload

方法二:使用 iptables

  1. 允许特定 IP 访问 SSH:
sudo iptables -A INPUT -p tcp -s 192.168.1.100 --dport 22 -j ACCEPT

192.168.1.100 替换为您希望允许访问的 IP 地址。

  1. 保存 iptables 规则:
sudo service iptables save
  1. 重启 iptables 服务:
sudo systemctl restart iptables

注意:这些方法仅限制通过防火墙的访问。如果有人通过其他方式(如直接连接到服务器)尝试访问 SSH,这些方法将不起作用。因此,建议同时使用防火墙和其他安全措施(如禁用 root 登录、使用密钥认证等)。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:如何限制CentOS SSH访问IP

0