多个github账号,配置ssh-key

70 阅读1分钟

1.本地生成不同账号的密钥信息

邮箱替换成自己github账号的绑定邮箱

ssh-keygen -t rsa -C "xxx1@xx.com"  #账号1

ssh-keygen -t rsa -C "xxx2@xx.com"  #账号2

2.输入文件名

# Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]

3.将公钥添加到不同的github账号下

分别登陆自己github账号,然后将生成的公钥(.pub结尾的文件)添加到对应的账号下:

4.配置config文件

单个账号时候不需要配置,默认即可,多个账号时候需要进行配置 查看 ~/.ssh 目录下 是否存在config文件,不存在的话新建一个,填写对应的配置信息

1. 查看ssh目录
cd ~/.ssh

2. vi config
3. 填写对应配置
Host coder.github.com  // 别名
HostName github.com // 服务器地址
User javage // github用户名
IdentityFile  ~/.ssh/coder // 对应的秘钥地址

Host tester.github.com
HostName github.com
User tester
IdentityFile  ~/.ssh/tester
4. 输入enter :wq保存退出

5.添加key信息到ssh-agent中

因为默认只读取的默认生成的id_rsa,为了能读取到修改后的密钥信息

ssh-add ~/.ssh/xxxx1 // 对应的密钥文件

ssh-add ~/.ssh/xxxx2

在执行命令中如果提示:

Could not open a connection to your authentication agent.

需要执行如下命令:

ssh-agent bash

6.测试是否配置正确

ssh -T git@xxx.github.com // config中的host