# CentOS中怎么使用SSH限制IP登录
## 前言
在Linux服务器管理中,SSH(Secure Shell)是最常用的远程管理协议。但开放SSH端口也意味着潜在的安全风险,恶意攻击者可能通过暴力破解等方式尝试入侵服务器。限制允许连接的IP地址是提升SSH安全性的有效手段之一。本文将详细介绍在CentOS系统中通过多种方式实现SSH的IP访问限制。
---
## 方法一:通过hosts.allow和hosts.deny文件限制
### 1. 配置文件路径
/etc/hosts.allow /etc/hosts.deny
### 2. 配置示例
```bash
# 允许特定IP访问SSH(hosts.allow)
sshd: 192.168.1.100
sshd: 203.0.113.0/24
# 拒绝所有其他IP(hosts.deny)
sshd: ALL
修改后立即生效,无需重启服务。
iptables -L -n
# 允许特定IP访问22端口
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT
# 拒绝其他所有IP的SSH连接
iptables -A INPUT -p tcp --dport 22 -j DROP
service iptables save # CentOS 6
firewall-cmd --reload # CentOS 7+
vi /etc/hosts.allow
sshd : 192.168.1.100 : allow
sshd : 192.168.1.0/255.255.255.0 : allow
sshd : ALL : deny
tcpdchk -v
vi /etc/ssh/sshd_config
AllowUsers *@192.168.1.100
AllowGroups *@192.168.1.0/24
DenyUsers *@10.0.0.0/8
systemctl restart sshd
Match Address 192.168.1.*
PasswordAuthentication no
AllowUsers admin
yum install epel-release
yum install fail2ban
# /etc/fail2ban/jail.local
[sshd]
enabled = true
maxretry = 3
bantime = 3600
systemctl start fail2ban
ssh username@server -v
telnet server_ip 22
tail -f /var/log/secure
journalctl -u sshd -f
sshd_config中的Port参数/var/log/secure日志cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
本文介绍了五种限制SSH IP访问的方法,实际部署时建议:
1. 生产环境优先使用sshd_config+iptables组合
2. 临时限制可使用hosts.allow/deny
3. 高安全环境建议部署Fail2Ban
通过合理配置IP限制策略,可显著降低SSH服务的安全风险。 “`
注:实际字数为约850字,您可以通过以下方式扩展: 1. 增加每种方法的详细原理说明 2. 添加更多实际配置案例 3. 补充故障排查章节 4. 加入性能影响分析
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。