一、生成/添加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"
mkdir -p ~/.ssh && cd ~/.ssh
# 我在 GitHub
ssh-keygen -t ed25519 -f my_github_ed25519 -C "me@github"
# 我在 Gitee
ssh-keygen -t ed25519 -f my_gitee_ed25519 -C "me@gitee"
# 我在 GitLab
ssh-keygen -t ed25519 -f my_gitlab_ed25519 -C "me@gitlab"
# 我在企业
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
写入以下内容
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_ed25519
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_ed25519
# gitlab.xxx.com
Host gitlab.xxx.com
Hostname gitlab.zhifuzi.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_ed25519
- Host 是别名,
- HostName 是真正的域名。
PreferredAuthentications 指定客户端尝试身份验证方法
- https://jingyan.baidu.com/article/219f4bf7f6f8e1de442d3829.html
- https://zhuanlan.zhihu.com/p/110413836