Linux系统的安全SSH登录的详细步骤和故障排查

Linux系统的安全SSH登录的详细步骤和故障排查

发布时间:2024-10-13 01:51:15

让我们深入探讨linux系统的安全ssh登录,包括详细的配置步骤、最佳实践和故障排查方法。ssh(secure shell)是远程登录linux系统的主要方式,确保其安全性至关重要。

# 生成ssh密钥对
ssh-keygen -t ed25519 -c "your_email@example.com"

# 将公钥复制到服务器
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_host

# ssh客户端配置 (~/.ssh/config)
host myserver
hostname 192.168.1.100
user myuser
identityfile ~/.ssh/id_ed25519
port 22222

# ssh服务器配置 (/etc/ssh/sshd_config)
port 22222
permitrootlogin no
passwordauthentication no
pubkeyauthentication yes
permitemptypasswords no
x11forwarding no
allowusers myuser
maxauthtries 3
logingracetime 60
clientaliveinterval 300
clientalivecountmax 2

# 重启ssh服务
sudo systemctl restart sshd

# 使用ssh登录
ssh myserver

# 设置防火墙规则 (使用ufw)
sudo ufw allow 22222/tcp
sudo ufw enable

# 使用fail2ban防止暴力攻击
sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local

# fail2ban配置示例
[sshd]
enabled = true
port = 22222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600

# 重启fail2ban服务
sudo systemctl restart fail2ban

 

现在,让我们详细介绍linux系统安全ssh登录的步骤和故障排查方法:

  1. ssh密钥对生成和使用 a. 生成ssh密钥对:
     
    ssh-keygen -t ed25519 -c "your_email@example.com"
    • 使用ed25519算法,它更安全且性能更好
    • 为密钥添加密码短语,增加安全性
    b. 将公钥复制到服务器:
     
    ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_host
    • 这会将公钥添加到服务器的~/.ssh/authorized_keys文件中
    c. 配置ssh客户端(~/.ssh/config):
     

    host myserver
    hostname 192.168.1.100
    user myuser
    identityfile ~/.ssh/id_ed25519
    port 22222

    • 这样可以简化连接命令,只需输入ssh myserver
  2. ssh服务器安全配置 a. 编辑ssh配置文件(/etc/ssh/sshd_config):
     

    port 22222 # 更改默认端口
    permitrootlogin no # 禁止root直接登录
    passwordauthentication no # 禁用密码认证
    pubkeyauthentication yes # 启用公钥认证
    permitemptypasswords no # 禁止空密码
    x11forwarding no # 禁用x11转发
    allowusers myuser # 只允许特定用户登录
    maxauthtries 3 # 最大尝试次数
    logingracetime 60 # 登录尝试的超时时间
    clientaliveinterval 300 # 客户端活跃信号间隔
    clientalivecountmax 2 # 最大活跃信号次数

    b. 重启ssh服务:
     
    sudo systemctl restart sshd
  3. 防火墙配置 a. 使用ufw(uncomplicated firewall):
     

    sudo ufw allow 22222/tcp
    sudo ufw enable

    b. 或者使用iptables:
     

    sudo iptables -a input -p tcp --dport 22222 -j accept
    sudo iptables-save > /etc/iptables/rules.v4

  4. 使用fail2ban防止暴力攻击 a. 安装fail2ban:
     
    sudo apt install fail2ban
    b. 配置fail2ban:
     

    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
    sudo nano /etc/fail2ban/jail.local

    在[sshd]部分添加:
     

    enabled = true
    port = 22222
    filter = sshd
    logpath = /var/log/auth.log
    maxretry = 3
    bantime = 3600

    c. 重启fail2ban服务:
     
    sudo systemctl restart fail2ban
  5. 其他安全增强措施 a. 启用两步验证(2fa):
    • 使用google authenticator或其他2fa工具
    b. 使用tcp包装器限制访问:
    • 编辑/etc/hosts.allow和/etc/hosts.deny文件
    c. 定期更新系统和ssh:
     
    sudo apt update && sudo apt upgrade
    d. 监控ssh日志:
     
    sudo tail -f /var/log/auth.log
  6. 故障排查 a. 无法连接到ssh服务器:
    • 检查网络连接: ping remote_host
    • 验证ssh服务是否运行: sudo systemctl status sshd
    • 检查防火墙规则: sudo ufw statussudo iptables -l
    • 尝试使用详细模式连接: ssh -v user@remote_host
    b. 密钥认证失败:
    • 检查服务器上的authorized_keys文件权限: chmod 600 ~/.ssh/authorized_keys
    • 确保.ssh目录权限正确: chmod 700 ~/.ssh
    • 验证私钥权限: chmod 600 ~/.ssh/id_ed25519
    • 检查selinux上下文(如果适用): restorecon -r -v ~/.ssh
    c. 被fail2ban封禁:
    • 检查fail2ban状态: sudo fail2ban-client status sshd
    • 解除封禁: sudo fail2ban-client set sshd unbanip
    d. 性能问题:
    • 检查系统负载: tophtop
    • 查看ssh连接数: netstat -tnpa | grep 'established.*sshd'
    • 检查ssh配置中的压缩设置
    e. 日志分析:
    • 查看认证日志: sudo grep sshd /var/log/auth.log
    • 使用journalctl查看系统日志: journalctl -u sshd
  7. 最佳实践 a. 使用强密码和密钥管理:
    • 使用密码管理器生成和存储复杂密码
    • 定期轮换ssh密钥
    b. 限制ssh访问:
    • 使用allowusersallowgroups限制可以ssh登录的用户
    • 考虑使用跳板机(bastion host)作为唯一的ssh入口点
    c. 使用ssh配置管理工具:
    • 考虑使用ansible等工具管理多台服务器的ssh配置
    d. 审计和日志管理:
    • 使用集中式日志管理系统,如elk stack(elasticsearch, logstash, kibana)
    • 定期审查ssh登录尝试和模式
    e. 教育和培训:
    • 确保所有有ssh访问权限的用户了解安全最佳实践
    • 制定并执行严格的安全策略
  8. 高级技巧 a. 使用ssh证书代替传统的ssh密钥:
    • 更容易管理,特别是在大规模环境中
    • 可以设置过期时间,增加安全性
    b. 实现端口敲门(port knocking):
    • 在允许ssh连接之前需要特定的网络请求序列
    c. 使用vpn作为ssh的前置条件:
    • 在允许ssh连接之前,要求用户首先连接到vpn
    d. 实现sshguard:
    • 类似fail2ban,但更轻量级,专注于ssh防护

这些深入的解释和示例应该能够帮助你全面理解和实施linux系统的安全ssh登录。记住,ssh安全是一个持续的过程,需要定期审查和更新你的安全措施。同时,在实施任何更改时,务必小心谨慎,以免意外锁定自己。

感谢提供:05互联