MacOS Terminal:如何使用seccond ssh密钥? [英] MacOS Terminal: how to use a seccond ssh key?
问题描述
我正在MacOS上运行,并希望使用第二个ssh-key进行SSH/Git登录.
我在〜/.ssh/
I'm working on MacOS and want an SSH/Git login with a seccond ssh-key.
I have two key pairs in ~/.ssh/
id_rsa
id_rsa.pub
id_rsa.gitlab
id_rsa.gitlab.pub
id_rsa和id_rsa.pub是较旧的密钥.该键工作正常.但是,在需要第二把钥匙的地方,我无法登录.它尝试第一个密钥失败.
id_rsa and id_rsa.pub are the older key. This key is working fine. But where the second key is needed, i can't login. It trys the first key and fails.
运行此命令ssh-add /Users/kevin/.ssh/id_rsa.gitlab
会有所帮助.但是重新启动后,它不再起作用了,我必须再次运行该命令.
Running this command ssh-add /Users/kevin/.ssh/id_rsa.gitlab
helps. But after a reboot, it dosen't work anymore and i must run the command again.
在每次重新启动后运行此命令是否有更好的解决方案?
Is there a better solution as run this command after each reboot?
推荐答案
使用多个ssh键的方法是使用〜/.ssh/config
The way to use several ssh keys is to use a ~/.ssh/config
Host serverKey1
Hostname server1 # or ip address of server1
User serverUser
IdentityFile ~/.ssh/id_rsa
Host serverKey2
Hostname server1 # or ip address of server1
User serverUser
IdentityFile ~/.ssh/id_rsa.gitlab
然后,您需要在您的ssh网址中使用scp语法:
You then need to use an scp-syntax for your ssh url:
git clone serverKey1:/my/repo
git clone serverKey2:/my/repo
serverKey1
和serverKey2
是~/.ssh/config
文件中的条目,以便传递给ssh正确的服务器,用户并标识文件(公用和专用密钥)
serverKey1
and serverKey2
are entries in your ~/.ssh/config
file in order to pass to ssh the right server, user and identify file (public and private keys)
如果这些私钥受密码保护,则您仍需要添加它们.
If those private keys are password-protected, you would still need to add them.
您可以在此atlassian帮助页面上看到更多信息:"配置多个SSH身份适用于GitBash,Mac OSX和Linux ".
You can see more at this atlassian help page: "Configure multiple SSH identities for GitBash, Mac OSX, & Linux".
这篇关于MacOS Terminal:如何使用seccond ssh密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!