Github添加公钥

首先需要设置git用户信息

1
2
git config --global user.name "yourname"
git config --global user.email "youremail"

一、对于单个SSH key

1、执行cd ~/.ssh命令,如果存在该目录,表明之前生成过SSH Key,利用ll命令即可以查看

1
2
cd ~/.ssh
ll

2、若没有,则可以生成新的SSH key

1
ssh-keygen -t rsa -C "username@example.com"

3、在~/.ssh目录下有 id_rsa(私钥)和 id_rsa.pub(公钥)两个文件,其中id_rsa.pub文件里存放的即是公钥key

4、添加私钥

1
ssh-add ~/.ssh/id_rsa

5、登录到GitHub,点击右上方的头像,选择settings ,点击Add SSH key,把id_rsa.pub的内容复制到里面即可

二、对于一台电脑针对多个Github账户添加多个公钥:

1、生成多个SSH key

1
2
ssh-keygen -t rsa -C "hbcdec@gmail.com” -f ~/.ssh/id1_rsa
ssh-keygen -t rsa -C "hbcdec@gmail.com” -f ~/.ssh/id2_rsa

2、添加私钥

1
2
ssh-add ~/.ssh/id1_rsa
ssh-add ~/.ssh/id2_rsa

3、修改配置文件

1
vim config
1
2
3
4
5
6
7
8
#personal
Host personal
HostName github.com
PreferredAuthentications publickey IdentityFile ~/.ssh/id1_rsa
#work
Host work
HostName github.com
PreferredAuthentications publickey IdentityFile ~/.ssh/id2_rsa

4、添加不同公钥到对应GitHub账户

5、测试

1
ssh -T git@personal

如果提示

1
Hi xxx You've successfully authenticated, but GitHub does not provide shell access.

说明连接成功。