一、生成/添加SSH公钥

常见的 SSH 登录密钥使用 RSA 算法。RSA 经典且可靠,但性能不够理想。 只要你的服务器上 OpenSSH 版本大于 6.5(2014 年的古早版本),就可以利用 Ed25519 算法生成的密钥对,减少你的登录时间。如果你使用 SSH 访问 Git,那么就更值得一试。 Ed25519 的安全性在 RSA 2048 与 RSA 4096 之间,且性能在数十倍以上。

ssh-keygen -t ed25519 -f gitlab_ed25519 -C "xxxxx@xxxxx.com"

  1. mkdir -p ~/.ssh && cd ~/.ssh
  2. # 我在 GitHub
  3. ssh-keygen -t ed25519 -f my_github_ed25519 -C "me@github"
  4. # 我在 Gitee
  5. ssh-keygen -t ed25519 -f my_gitee_ed25519 -C "me@gitee"
  6. # 我在 GitLab
  7. ssh-keygen -t ed25519 -f my_gitlab_ed25519 -C "me@gitlab"
  8. # 我在企业
  9. ssh-keygen -t ed25519 -f my_company_ed25519 -C "email@example.com"
  • [-t ed25519] 表示使用 ed25519 算法。
  • [-b 4096] 表示 RSA 密钥长度 4096 bits (默认 2048 bits)。ed25519 算法不需要指定。
  • [-f my_id] 表示在【当前工作目录】下生成一个私钥文件 my_id (同时也会生成一个公钥文件 my_id.pub)。
  • [-C “email@example.com”] 表示在公钥文件中添加注释,即为这个公钥“起个别名”(不是 id,可以更改)。

    二、Git配置多个SSH-Key

    ~/.ssh目录下
    touch config

vi config
写入以下内容

  1. # gitee
  2. Host gitee.com
  3. HostName gitee.com
  4. PreferredAuthentications publickey
  5. IdentityFile ~/.ssh/gitee_ed25519
  6. # github
  7. Host github.com
  8. HostName github.com
  9. PreferredAuthentications publickey
  10. IdentityFile ~/.ssh/github_ed25519
  11. # gitlab.xxx.com
  12. Host gitlab.xxx.com
  13. Hostname gitlab.zhifuzi.com
  14. PreferredAuthentications publickey
  15. IdentityFile ~/.ssh/gitlab_ed25519