ubuntu

Ubuntu SSH如何使用私钥登录

小樊
49
2025-08-27 12:19:16
栏目: 智能运维

  1. 生成密钥对(若未生成):
    在本地终端执行:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    按提示回车使用默认路径和密码(可选),生成 id_rsa(私钥)和 id_rsa.pub(公钥)。

  2. 上传公钥到服务器

    • 推荐方式
      ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote_host
      输入服务器密码后,公钥自动添加到 ~/.ssh/authorized_keys
    • 手动方式
      cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
      确保服务器端 ~/.ssh 权限为 700authorized_keys 权限为 600
  3. 配置服务器(可选)
    编辑 /etc/ssh/sshd_config,确保以下参数:

    PubkeyAuthentication yes  
    PasswordAuthentication no  # 禁用密码登录(增强安全)  
    

    重启SSH服务:sudo systemctl restart ssh

  4. 使用私钥登录
    执行命令:
    ssh -i ~/.ssh/id_rsa user@remote_host
    若私钥有密码,需输入密码;否则直接登录。

说明

0
看了该问题的人还看了