如何从两台机器访问一个Git存储库? [英] How can I access one Git repo from two machines?
问题描述
我有一个工作仓库,通常可以从我的工作机上访问.在那台机器上,我已经在Git Bash中使用ssh-keygen
创建了SSH密钥对.两台计算机上的密钥对都是id_rsa
和id_rsa.pub
.
I have a repo for work, normally accessed from my work machine. On that machine, I have created an SSH key-pair using ssh-keygen
in Git Bash. The key-pair on both machines is id_rsa
and id_rsa.pub
.
如果我以工作用户身份登录,并尝试将主密钥添加到工作用户帐户中,则会被告知有人已经注册了该密钥.如果我生成第二个主键,即id_rsa2
和id_rsa.pub2
,则验证失败.
If I log in as my work user, and try to add my home key to the work user account, I get told somebody else has already registered that key. If I generate a second home key, i.e. id_rsa2
and id_rsa.pub2
, I get an authentication failure.
据此,我推测默认情况下,Git在通过回购主机(顺便是BitBucket)进行身份验证时仅查找并使用原始的id_rsa.pub
文件.我猜我必须以某种方式告诉Git用于哪个存储库或其他东西的密钥,但是现在我迷路了,问如何执行此操作.
From this, I surmise that Git, by default, only looks for and uses the original id_rsa.pub
file when authenticating with the repo host, which incidentally, is BitBucket. I'm guessing I somehow have to tell Git which key to use for which repo, or something, but right now I am lost and asking how to do this.
推荐答案
您可以使用~/.ssh/config
文件使用多个ssh私钥.
You can use multiple private ssh keys using a ~/.ssh/config
file.
Host fromhome
HostName yourServer
User git
IdentityFile "~/.ssh/id_rsa2"
Host fromwork
HostName yourServer
User git
IdentityFile "~/.ssh/id_rsa"
该文件可以引用两个私钥,并允许您使用2个不同的ssh url.
That file can reference both private keys and let you use 2 different ssh urls.
在家中:
cd /path/to/local/repo
git remote set-url origin fromhome:<user>/yourrepo
下班时间:
cd /path/to/local/repo
git remote set-url origin fromwork:<user>/yourrepo
话虽这么说,无论是在家中还是工作中,您都只需要一组公钥/私钥,因此不需要配置文件.
如果您在家中重新生成私钥id_rsa
私钥,则可以在BitBucket端注册其新的公钥.
在家里,.ssh/config
仅在您已经有用于其他用途的私有/公共ssh密钥时才有用.
That being said, from home or work, you only need one set of public/private key, so you don't need the config file.
If you regenerate a private id_rsa
private key at home, you will be able to register its new public key on BitBucket side.
At home, the .ssh/config
is only useful if you had already a private/public ssh key used for something else.
Rahul Gupta 问:
为什么我不能只将
.ssh
目录复制到第二台计算机上并使用它?
Why can't I just copy the
.ssh
directory to the second machine and use the same?
这被认为是不好的做法:应该保留私钥... 私有,例如不被复制".
另外,您可以轻松地撤消私钥(通过从.authorized_keys
ssh服务器中删除其授权密钥).如果在许多计算机上使用了私钥,则通过在服务器端删除一个公钥,您将以比预期的方式更多的方式撤消访问.
This is considered as bad practice: a private key is supposed to remain... private, as in "not copied around".
Plus you can easily revoke a private key (by removing its authorized key from the .authorized_keys
ssh server). If the private key was used on many machines, you would revoke access for way more than you intended by removing one public key on the server side.
这篇关于如何从两台机器访问一个Git存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!