SSH Keygen

SSH 密钥生成和管理

生成 SSH 密钥

  1. 打开终端

    • Linux/macOS: 使用 Terminal 应用程序。
    • Windows: 使用 Git Bash 或类似程序。
  2. 运行 ssh-keygen

    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    • -t rsa: 指定密钥类型为 RSA。
    • -b 4096: 设置密钥长度为 4096 位。
    • -C: 后接邮箱地址,作为密钥的标识。
  3. 选择存储位置

    • 默认位置是 ~/.ssh/id_rsa
    • 可以指定不同的文件路径。
  4. 设置密码(可选)

    • 为密钥设置一个强密码,提高安全性。
  5. 检查公钥

    • 位置通常是 ~/.ssh/id_rsa.pub
    • 使用 cat ~/.ssh/id_rsa.pub 查看。

修改公钥中的邮箱地址

  1. 打开公钥文件

    • 命令:nano ~/.ssh/id_rsa.pub(或其他编辑器)
  2. 编辑邮箱地址

    • 替换文件末尾的邮箱地址。
  3. 保存并关闭文件

将公钥添加到服务器

  1. 复制公钥

    cat ~/.ssh/id_rsa.pub | pbcopy
  2. 登录到服务器

    • 使用 SSH 登录到目标服务器。
  3. 编辑 authorized_keys 文件

    • 打开 ~/.ssh/authorized_keys
    • 粘贴公钥内容。
  4. 保存更改

常见命令

  • ssh-keygen: 生成新的 SSH 密钥。
  • ssh-copy-id user@host: 自动将公钥复制到服务器。
  • ssh user@host: 使用 SSH 连接到服务器。

安全提示

  • 不要 公开你的私钥(id_rsa)。
  • 定期更换密钥。
  • 使用强密码保护密钥。