公司有个git服务,自己写的代码也放在oschina上,那怎么才能配置两个ssh key呢,保证两个服务同时可用,下面就说下怎么配置多个ssh key。
电脑上原先已经有一个ssh key了,是公司的,就不在说怎么生成了,现在说下怎么再生成一个key且不覆盖旧的。
bogon:.ssh my$ ssh-keygen -t rsa -C "xxxx@xxx.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/my/.ssh/id_rsa): id_rsa_oschina//在这里写新的文件名
id_rsa_oschina already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_oschina.
Your public key has been saved in id_rsa_oschina.pub.
The key fingerprint is:
SHA256:fYx/nP9Nxxxxxxxdd/6cKReriyoAxxxxxxxP1p54V4c xxxx@xxx.com
The key's randomart image is:
//省略
bogon:.ssh my$
注意一定要写文件名啊,要不就把旧的覆盖掉了。
现在试试是否可以连接oschina了
bogon:.ssh my$ ssh -T git@git.oschina.net
Permission denied (publickey).
说明不可以,下面做个配置,让两个key都可以用。
bogon:.ssh my$ cd ~/.ssh/
bogon:.ssh my$ vim config
//以下是文件内容
# Default github user(company@xxx.com)
Host git.company.com
HostName git.company.com
User my
IdentityFile /Users/my/.ssh/id_rsa
# second user(xxxx@xxx.com)
Host git.oschina.net
HostName git.oschina.net
User my
IdentityFile /Users/my/.ssh/id_rsa_oschina
//保存文件
下面测试下是否可以连接oschina
bogon:.ssh my$ ssh -T git@git.company.com
Welcome to GitLab, my!
bogon:.ssh my$ ssh -T git@git.oschina.net
Welcome to Git@OSC, my!
OK,两个git服务都可用了,其实最主要的就是~/.ssh/config文件,配置好这个就大功告成。