温馨提示×

SecureCRT如何支持Debian的SSH密钥认证

小樊
31
2025-12-20 17:34:29
栏目: 智能运维

SecureCRT与Debian的SSH密钥认证配置指南

一 服务器端准备

  • 安装并启动 OpenSSH 服务(Debian 常见服务名为 ssh):
    • 安装:sudo apt update && sudo apt install openssh-server
    • 检查状态:sudo systemctl status ssh
    • 如无则启动:sudo systemctl start ssh;开机自启:sudo systemctl enable ssh
  • 编辑 SSH 服务端配置 /etc/ssh/sshd_config,确保启用公钥认证并(可选)禁用密码:
    • 建议设置:
      • PubkeyAuthentication yes
      • AuthorizedKeysFile .ssh/authorized_keys(如需兼容旧环境可同时保留 .ssh/authorized_keys2
      • PasswordAuthentication no(在确认密钥可用后再关闭,避免被锁)
      • 可按需:PermitRootLogin no
  • 使配置生效:sudo systemctl restart ssh
  • 为登录用户准备目录与权限(以用户 debian 为例):
    • 目录:mkdir -p ~debian/.ssh && chmod 700 ~debian/.ssh
    • 授权文件:touch ~debian/.ssh/authorized_keys && chmod 600 ~debian/.ssh/authorized_keys
    • 属主:chown -R debian:debian ~debian/.ssh
  • 防火墙放行 SSH 端口(如为 22 或自定义端口):sudo ufw allow 22/tcpsudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT

二 使用SecureCRT生成密钥并在Debian部署公钥

  • 在 SecureCRT 中生成密钥对:
    • 菜单/会话属性中进入:Authentication → Public Key → Properties → Create Identity File
    • 选择算法(推荐 RSAECDSA),设置密钥长度(如 2048/4096 位),可设置通行短语(passphrase)
    • 生成后保存私钥(如 my_rsa),并导出/保存公钥(如 my_rsa.pub
  • 将公钥部署到 Debian 的 authorized_keys
    • 若 SecureCRT 导出的是 OpenSSH 格式公钥(文件内容以 ssh-rsa/ecdsa-sha2-… 开头),直接追加:
      • cat my_rsa.pub >> ~debian/.ssh/authorized_keys
    • 若导出的是 SecureCRT 的 SSH2(IETF SECSH) 格式(文件内容以 ---- BEGIN SSH2 PUBLIC KEY ---- 开头),需转换后追加:
      • ssh-keygen -i -f my_rsa.pub >> ~debian/.ssh/authorized_keys
  • 再次确认目录与文件权限为 700/.ssh600/authorized_keys,属主正确。

三 在SecureCRT中启用密钥登录

  • 新建或编辑会话:协议选 SSH2,填写 Hostname/IPPort
  • 在会话属性的 Authentication 页:
    • PublicKey 设为 Primary,Password 设为 Optional/不勾选
    • 点击 Properties 选择私钥文件(如 my_rsa),如设置了通行短语则在连接时输入
  • 保存会话并连接;若提示“所选身份验证方法失败”,优先检查服务端 authorized_keys 内容、权限与属主是否正确。

四 常见问题与排查

  • 权限与属主错误:
    • ~/.ssh 700~/.ssh/authorized_keys 600,且属主为登录用户;否则公钥认证会被拒绝
  • SELinux/AppArmor(如启用)限制:
    • 确保 ~/.sshauthorized_keys 的上下文/策略允许 sshd 读取
  • 服务端配置未生效或端口不对:
    • 确认 PubkeyAuthentication yesAuthorizedKeysFile 路径正确,且 PasswordAuthentication 状态符合预期
    • 修改后执行 sudo systemctl restart ssh;如使用非默认端口,确保客户端与防火墙一致
  • 公钥格式不匹配:
    • SecureCRT 的 SSH2 公钥需用 ssh-keygen -i -f 转换为 OpenSSH 格式再写入 authorized_keys
  • 首次连接被拒或卡顿:
    • 检查网络与端口连通性;如为云主机,确认安全组/防火墙放行对应端口。

五 进阶与安全建议

  • 禁用密码登录前务必先用密钥成功登录一次,并保留一个备用会话或控制台通道
  • 使用强密钥与通行短语;私钥文件权限设为 600,妥善保管
  • 可更改默认端口、限制 PermitRootLogin、仅允许特定用户或组登录,进一步提升安全性。

0