Ubuntu 上使用 SecureCRT 启用公钥认证
一 准备与生成密钥
- 在本地生成密钥对(任意一台能连到 Ubuntu 的机器均可,常见为你的Windows/macOS电脑):
- 生成 RSA 密钥(推荐 4096 位):ssh-keygen -t rsa -b 4096 -C “your_email@example.com”
- 生成后默认得到私钥 ~/.ssh/id_rsa 与公钥 ~/.ssh/id_rsa.pub
- 或在 SecureCRT 内生成:
- 菜单 Tools → Create Key Pair…,选择 RSA,设置密钥长度(建议 2048/4096),可设置通行短语(passphrase),生成后将私钥(如 Identity)与公钥(Identity.pub)妥善保存。
二 将公钥部署到 Ubuntu 服务器
- 推荐方式(自动):ssh-copy-id user@remote_host(将本地的 id_rsa.pub 追加到远端 ~/.ssh/authorized_keys)
- 手动方式:
- 在服务器创建目录并设置权限:mkdir -p ~/.ssh && chmod 700 ~/.ssh
- 追加公钥内容:echo “你的公钥内容” >> ~/.ssh/authorized_keys
- 设置权限:chmod 600 ~/.ssh/authorized_keys
- 说明:user 为你的登录用户名,remote_host 为服务器 IP/域名。
三 在 SecureCRT 中启用公钥认证
- 打开或新建会话 → 会话属性 → 连接 → SSH2 → 认证/用户身份验证:
- 认证方法勾选 Public Key
- 点击 Browse 选择私钥文件(如 ~/.ssh/id_rsa 或 SecureCRT 生成的 Identity)
- 如私钥设置了通行短语,连接时会提示输入
- 会话属性 → SSH2 → 密钥交换:确保启用 Diffie-Hellman/ECDH 等常用算法,避免协商失败。
四 服务器端 SSH 配置与重启
- 编辑 /etc/ssh/sshd_config,确保以下关键项:
- PubkeyAuthentication yes
- AuthorizedKeysFile .ssh/authorized_keys
- PasswordAuthentication no(可选,禁用密码登录前务必确认密钥可正常登录)
- RSAAuthentication yes(如使用 RSA 密钥)
- 使配置生效:sudo systemctl restart sshd。
五 验证与常见问题
- 验证连接:
- 在客户端执行 ssh -v user@remote_host,若看到 “Offering public key … Server accepts key …” 表示密钥认证成功
- 或在 SecureCRT 连接日志中确认使用了公钥
- 常见问题排查:
- 权限错误:确保服务器上 ~/.ssh 为 700、authorized_keys 为 600
- 配置未生效:确认已重启 sshd,且客户端确实选择了正确的私钥
- 算法协商失败:在 SecureCRT 的 SSH2 → 密钥交换中启用 ECDH/Diffie-Hellman 等算法。