github的多环境应用介绍

ssh认证中公钥如同家里大门,私钥就是钥匙,一个大门可以有多把钥匙,大门可以随便展示,钥匙却不能,必须给那些需要的人。其中,公钥的默认名字是id_rsa.pub,私钥的默认名字是id_rsa,它的权限是600。
在使用githubssh的认证时就是通过~/.ssh/id_rsa下有私钥与github上的公钥内容id_rsa.pub比对,成功就是认证打通。
如果换电脑用linux,那么现在有两种方法,
方法一:把A上的密钥拷贝到B上,为了不影响vps的其他使用,名字不使用id_rsa,这里名字改为,id_rsa.github(相当于为一个大门,再配一把钥匙)

步骤如下:

cp A上~/.ssh/id_rsa 到B上 ~/.ssh/id_rsa.github,并把权限改为600

在B上创建~/.ssh/config,写入以下内容,保存退出。

它声名了目标主机是github.com,用户git,认证方式和文件,

PS:为方便连接其他主机时也可用这种方法,在同一机器上连接多个github账号也可使用这种方法。http://rsylareclipse.blog.163.com/blog/static/18550144020121285148377/

Host github.com
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.github

测试下能成功

jpuyy@www:~/.ssh$ ssh -T git@github.com
Hi jpuyy! You’ve successfully authenticated, but GitHub does not provide shell access.

方法二:用ssh-keygen重新生成ssh公钥/密钥对(相当于重新开辟一个大门并配好钥匙)

这和在A上第一次使用时方法一样,但为了不使用默认名id_rsa,可用如下命令

ssh-keygen -C “jpuyy.com@gmail.com” -f ~/.ssh/id_rsa.github

这时把id_rsa.github.pub的内容copy到对应github.com的ssh公钥里

原文地址:https://www.cnblogs.com/guochaoxxl/p/6823105.html