git 配置多个SSH Key

一台机器配置多个项目托管仓库,需要配置多个key

1、生成ssh-key 以github为例

$ ssh-keygen -t rsa -C "your_email@example.com” -f ~/.ssh/github_rsa

此时,.ssh目录有两个文件:github_rsa ,github_rsa.pub

2、添加私钥

$ ssh-add ~/.ssh/github_rsa

成功提示:
Identity added:~/.ssh/github_rsa (~/.ssh/github_rsa)

错误提示:
Could not open a connection to your authentication agent
解决方案:
1、$ ssh-agent bash
2、然后再运行ssh-add命令。

辅助命令:
$ ssh-add -l 查看私钥列表
$ ssh-add -D 清空私钥列表

3、修改配置(~/.ssh/config)

如无创建
touch config

添加以下内容
#github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_rsa

4、为不同的项目设置单独的name和email

git config user.name yourname
git config user.email your-email@address.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。