SecureCRT 在 Debian 上配置密钥认证
一 服务器端准备
- 安装并启动 OpenSSH 服务(Debian 常见服务名为 ssh):
- 安装:sudo apt update && sudo apt install openssh-server
- 启动/开机自启:sudo systemctl start ssh && sudo systemctl enable ssh
- 检查状态:sudo systemctl status ssh
- 修改 SSH 服务端配置 /etc/ssh/sshd_config,确保启用公钥认证并指定公钥文件:
- 建议设置:
- PubkeyAuthentication yes
- AuthorizedKeysFile .ssh/authorized_keys(如需兼容旧版也可同时保留 .ssh/authorized_keys2)
- 可选安全项:PasswordAuthentication no(在确认密钥可登录后再关闭密码)
- 可选:PermitRootLogin no
- 创建用于登录的普通用户(示例用户名为 alice):
- 添加用户:sudo adduser alice
- 切换到该用户并创建密钥目录:su - alice && mkdir -p ~/.ssh
- 设置目录与文件权限(权限不当会导致公钥认证失败):
- chmod 700 ~/.ssh
- touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys
- 如由 root 代为配置,需修正属主:chown -R alice:alice ~/.ssh
- 重启 SSH 服务使配置生效:sudo systemctl restart ssh。
二 在 SecureCRT 中生成密钥并部署公钥到 Debian
- 生成密钥对(SecureCRT 内建向导):
- 菜单:Options → Session Options → SSH2 → Authentication → Public Key → Properties → Create Identity File,选择算法 RSA(或 Ed25519),设置密钥长度(如 2048/4096),可设置通行短语(推荐),完成后保存私钥(如 Identity)与公钥(如 Identity.pub)。
- 将公钥转换为 OpenSSH 格式并追加到服务器:
- 将 Identity.pub 传到 Debian(任意方式,如 SFTP/粘贴),在服务器上对应用户家目录执行:
- 转换并追加:ssh-keygen -i -f Identity.pub >> ~/.ssh/authorized_keys
- 再次确认权限:chmod 600 ~/.ssh/authorized_keys && chmod 700 ~/.ssh
- 说明:SecureCRT 默认生成的是 SSH2/OpenSSH 不兼容格式,需用 ssh-keygen -i 转换后才能被 OpenSSH 识别。
三 在 SecureCRT 中配置会话使用私钥连接
- 新建会话:File → New Session → Protocol: SSH2,填写主机、端口(默认 22)、用户名(如 alice)。
- 指定私钥:在会话属性 Session Options → SSH2 → Authentication → Public Key 中,点击 Add… 选择私钥文件(如 Identity),如设置了通行短语需在此输入。
- 关闭回退到密码:取消勾选 “Try password authentication if public key fails”,避免被要求输入密码。
- 保存并连接:连接时若提示输入通行短语,输入即可完成登录。
四 测试与加固
- 先用新会话测试密钥登录,确认无需密码(或仅输入通行短语)即可进入;建议保留一个已验证可用的终端窗口,再实施加固。
- 确认无误后,在 /etc/ssh/sshd_config 中将 PasswordAuthentication 设为 no,并再次重启 SSH:sudo systemctl restart ssh。
- 如修改了 SSH 端口(例如 2222),需在 SecureCRT 会话中同步端口,并确保防火墙放行对应端口。
五 常见故障排查
- 出现 Permission denied (publickey):
- 检查服务器上 ~/.ssh 与 ~/.ssh/authorized_keys 权限是否为 700/600,属主是否为登录用户。
- 确认公钥已正确转换并追加到 authorized_keys(使用 ssh-keygen -i -f Identity.pub >> ~/.ssh/authorized_keys)。
- 确认 sshd_config 中 PubkeyAuthentication yes 且 AuthorizedKeysFile 路径正确。
- 客户端提示找不到私钥或无法加载:
- 在 SecureCRT 会话属性的 Public Key 中重新指定私钥路径,确保私钥文件可读。
- 已禁用密码但仍无法登录:
- 临时将 PasswordAuthentication 改回 yes 以排查;或开启新终端测试新会话,避免当前会话断开后无法恢复。