温馨提示×

CentOS如何提升SSH安全性

小樊
41
2025-11-24 03:26:13
栏目: 智能运维

在CentOS系统中提升SSH安全性的方法有很多,以下是一些常见的建议:

1. 使用强密码

  • 更改默认密码:确保所有用户的密码都是强密码。
  • 定期更换密码:建议每90天更换一次密码。

2. 禁用root登录

  • 编辑/etc/ssh/sshd_config文件:
    sudo nano /etc/ssh/sshd_config
    
  • 找到并修改以下行:
    PermitRootLogin no
    
  • 重启SSH服务:
    sudo systemctl restart sshd
    

3. 限制SSH访问

  • 使用防火墙:配置iptables或firewalld来限制SSH端口(默认22)的访问。
    sudo firewall-cmd --permanent --add-port=22/tcp
    sudo firewall-cmd --reload
    
  • IP白名单:只允许特定IP地址访问SSH。
    sudo nano /etc/hosts.deny
    
    添加:
    sshd: ALL
    
    然后在/etc/hosts.allow中添加允许的IP:
    sshd: 192.168.1.1 192.168.1.2
    

4. 使用SSH密钥认证

  • 生成SSH密钥对:
    ssh-keygen -t rsa -b 4096
    
  • 将公钥复制到目标服务器:
    ssh-copy-id user@remote_host
    
  • 编辑/etc/ssh/sshd_config文件,确保以下行未被注释:
    PubkeyAuthentication yes
    
  • 重启SSH服务:
    sudo systemctl restart sshd
    

5. 使用Fail2Ban

  • 安装Fail2Ban:
    sudo yum install fail2ban
    
  • 配置Fail2Ban来阻止恶意IP地址:
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
    sudo nano /etc/fail2ban/jail.local
    
    添加或修改以下内容:
    [sshd]
    enabled = true
    port = ssh
    filter = sshd
    logpath = /var/log/secure
    maxretry = 3
    bantime = 600
    
  • 启动Fail2Ban服务:
    sudo systemctl start fail2ban
    sudo systemctl enable fail2ban
    

6. 更新SSH版本

  • 确保使用的是最新版本的OpenSSH:
    sudo yum update openssh-server
    

7. 使用SELinux

  • 如果启用了SELinux,确保它配置正确以增强安全性。

8. 监控和日志

  • 定期检查SSH日志文件/var/log/secure,寻找异常活动。
  • 使用工具如logwatch来自动化日志分析。

9. 使用SSH隧道和代理

  • 对于敏感操作,考虑使用SSH隧道或代理来增加一层保护。

通过实施这些措施,可以显著提高CentOS系统上SSH的安全性。记得定期审查和更新你的安全策略。

0