在设置github的时候,官方的说明文档要求备份当前的id_rsa,然后生成一份新的私钥用于github的登陆。如果真这样做,那么新的私钥是无法再继续登陆之前的机器的。这种方法有点暴力…
还好ssh可以让我们通过不同的私钥来登陆不同的域。
首先,在新增私钥的时候,通过指定不同的文件名来生成不同的私钥文件
ssh-keygen -t rsa -f ~/.ssh/id_rsa.work -C "Key for Work stuff" ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "Key for GitHub stuff"
新增ssh的配置文件,并修改权限
touch ~/.ssh/config chmod 600 ~/.ssh/config
修改config文件的内容
Host *.workdomain.com IdentityFile ~/.ssh/id_rsa.work User lee Host github.com IdentityFile ~/.ssh/id_rsa.github User git
这样在登陆的时候,ssh会根据登陆不同的域来读取相应的私钥文
.ssh: lee$ ssh -vT [email protected] OpenSSH_5.2p1, OpenSSL 0.9.8r 8 Feb 2011 debug1: Reading configuration data /Users/lee/.ssh/config debug1: Applying options for github.com debug1: Reading configuration data /etc/ssh_config debug1: Connecting to github.com [207.97.227.239] port 22. debug1: Connection established. debug1: identity file /Users/lee/.ssh/id_rsa.github type 1 debug1: Remote protocol version 2.0, remote software version OpenSSH_5.1p1 Debian-5github2 debug1: match: OpenSSH_5.1p1 Debian-5github2 pat OpenSSH*
原文链接:李玉鹏的blog -- http://www.leeyupeng.com/2011/11/multiple-ssh-private-keys/