温馨提示×

ubuntu securecrt如何配密钥

小樊
42
2026-09-16 04:32:47
栏目: 智能运维

下面以 Ubuntu(作为 SSH 服务端)+ SecureCRT(作为客户端) 为例,说明如何配置 密钥登录


一、在 Ubuntu 上生成密钥(推荐在服务器或本地 Linux 做)

1. 生成密钥对

ssh-keygen -t rsa -b 4096

或(更推荐)

ssh-keygen -t ed25519

一路回车即可,默认生成在:

  • 私钥:~/.ssh/id_rsa
  • 公钥:~/.ssh/id_rsa.pub

2. 将公钥写入授权文件

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

二、将私钥导入 SecureCRT

方法一:直接用 OpenSSH 私钥(推荐)

  1. 把 Ubuntu 上的私钥(如 id_rsa)复制到 Windows
  2. 打开 SecureCRT
    Options → Global Options → SSH2 → Identity or certificate file
  3. 选择复制过来的 id_rsa
  4. 连接时即可使用密钥登录

如果 SecureCRT 提示格式不支持,用方法二转换


方法二:转换为 SecureCRT 支持的格式

在 Ubuntu 或 Windows(有 OpenSSH)执行:

ssh-keygen -p -m RFC4716 -f id_rsa

或在 Windows 用 SecureCRT 自带工具:

  • Tools → Convert Private Key to SecureCRT Format

三、SecureCRT 新建会话使用密钥

  1. File → Connect → New Session
  2. Protocol 选 SSH2
  3. Hostname:Ubuntu IP
  4. Username:Ubuntu 用户名
  5. Authentication 中:
    • 勾选 PublicKey
    • 取消 Password(可选)
  6. Public Key 属性 → 选择私钥文件

四、Ubuntu 服务端必要配置

编辑:

sudo vim /etc/ssh/sshd_config

确保以下配置:

PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no   # 禁用密码(可选)

重启 SSH:

sudo systemctl restart ssh

五、常见问题

1. 权限错误导致无法登录

确保:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

2. 仍提示输入密码

  • 检查用户名是否正确
  • 查看日志:
sudo tail -f /var/log/auth.log

3. SELinux / 家目录加密

如果是加密家目录,需将 authorized_keys 放到非加密路径并修改 sshd_config


如果你愿意,我可以:

  • 给你 Windows + SecureCRT 图文步骤
  • 或帮你 只保留密钥登录、彻底禁用密码

你当前是 Ubuntu 桌面还是服务器?Windows 还是 Mac 用 SecureCRT?

0