rad*_*six 242 ssh github ssh-keys
我在使用两个不同的SSH密钥/ GitHub帐户以便一起玩时遇到一些麻烦.我有以下设置:
Repos可从一个帐户使用 git@github.com:accountname
Repos可从其他帐户使用 git@github.com:anotheraccount
每个帐户都有自己的SSH密钥.已添加两个SSH密钥,我已创建配置文件.我不相信配置文件是正确的.我不太确定如何指定使用的repos git@github.com:accountname
应该使用id_rsa
并且git@github.com:anotheraccount
应该使用id_rsa_anotheraccount
.
Jim*_*bel 303
安迪莱斯特的反应是准确的,但我找到了一个重要的额外步骤,我需要做的是让这个工作.在尝试设置两个配置文件时,一个用于个人配置,一个用于工作,我~/.ssh/config
大致如下:
Host me.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/me_rsa
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/work_rsa
Run Code Online (Sandbox Code Playgroud)
我的工作资料没有采取,直到我做了ssh-add ~/.ssh/work_rsa
.之后,与github的连接使用了正确的配置文件.以前他们违约了第一张公钥.
对于无法在使用时打开与身份验证代理的连接ssh-add
,
请检查:https:
//stackoverflow.com/a/17695338/1760313
ste*_*ano 168
我最近不得不这样做,不得不筛选所有这些答案和他们的评论,最终将信息拼凑在一起,所以为了您的方便,我将把它全部放在这里,在一篇文章中:
第1步:ssh键
创建您需要的任何密钥对.在这个例子中,我将默认/原始'id_rsa'(这是默认值)和我的新'id_rsa-work'命名为:
ssh-keygen -t rsa -C "stefano@work.com"
Run Code Online (Sandbox Code Playgroud)
步骤2:ssh config
通过创建/修改〜/ .ssh/config设置多个ssh配置文件.请注意略有不同的"主机"值:
# Default GitHub
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# Work GitHub
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_work
Run Code Online (Sandbox Code Playgroud)
第3步:ssh-add
您可能需要也可能不需要执行此操作.要检查,请运行以下列出身份指纹:
$ ssh-add -l
2048 1f:1a:b8:69:cd:e3:ee:68:e1:c4:da:d8:96:7c:d0:6f stefano (RSA)
2048 6d:65:b9:3b:ff:9c:5a:54:1c:2f:6a:f7:44:03:84:3f stefano@work.com (RSA)
Run Code Online (Sandbox Code Playgroud)
如果您的条目不存在,请运行:
ssh-add ~/.ssh/id_rsa_work
Run Code Online (Sandbox Code Playgroud)
第4步:测试
为了测试你是否正确完成了这一切,我建议进行以下快速检查:
$ ssh -T git@github.com
Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@work.github.com
Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.
Run Code Online (Sandbox Code Playgroud)
请注意,您必须根据您要使用的密钥/标识更改主机名(github/work.github).但现在你应该好好去!:)
Gub*_*ron 39
假设alice
是一个github.com用户,拥有2个或更多私有存储库repoN
.对于此示例,我们将仅使用两个名为repo1
and的存储库repo2
https://github.com/alice/repo1
https://github.com/alice/repo2
您需要从这些存储库中提取,而无需在服务器或多个服务器上输入密码.git pull origin master
例如,您想要执行,并且您希望在不要求密码的情况下实现此目的.
你不喜欢处理ssh-agent,你发现(或者你现在正在发现)~/.ssh/config
一个文件让你的ssh客户端知道要使用什么私钥,具体取决于主机名和用户名,简单的配置条目看起来像这个:
Host github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/alice_github.id_rsa
IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)
所以你继续创建你的(alice_github.id_rsa, alice_github.id_rsa.pub)
密钥对,然后你也去了你的存储库的.git/config
文件,你修改了你的遥控器的URL是origin
这样的:
[remote "origin"]
url = "ssh://git@github.com/alice/repo1.git"
Run Code Online (Sandbox Code Playgroud)
最后你去了存储库Settings > Deploy keys
部分并添加了内容alice_github.id_rsa.pub
此时,您可以在git pull origin master
不输入密码的情况下执行操作.
因此,你的直觉是获取该密钥并将其添加到repo2
部署密钥,但github.com会出错并告诉您该密钥已被使用.
现在你去生成另一个密钥(ssh-keygen -t rsa -C "alice@alice.com"
当然没有使用密码),所以这不会变得一团糟,你现在可以像这样命名你的密钥:
repo1
密钥对: (repo1.alice_github.id_rsa, repo1.alice_github.id_rsa.pub)
repo2
密钥对: (repo2.alice_github.id_rsa, repo2.alice_github.id_rsa.pub)
您现在将新的公钥放在repo2
github.com 上的部署密钥配置中,但现在您有一个ssh问题需要处理.
github.com
域上,ssh如何判断使用哪个密钥?你的.ssh/config
文件指向github.com
并且它不知道在拉动时使用哪个键.
所以我在github.com上找到了一个技巧.您可以告诉您的ssh客户端每个存储库位于不同的github.com子域中,在这些情况下,它们将是repo1.github.com
和repo2.github.com
首先是编辑.git/config
repo克隆上的文件,所以它们看起来像这样:
对于repo1
[remote "origin"]
url = "ssh://git@repo1.github.com/alice/repo1.git"
Run Code Online (Sandbox Code Playgroud)
对于repo2
[remote "origin"]
url = "ssh://git@repo2.github.com/alice/repo2.git"
Run Code Online (Sandbox Code Playgroud)
然后,在您的.ssh/config
文件上,现在您将能够为每个子域输入配置:)
Host repo1.github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/repo1.alice_github.id_rsa
IdentitiesOnly yes
Host repo2.github.com
HostName github.com
User git
IdentityFile /home/alice/.ssh/repo2.alice_github.id_rsa
IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)
现在,您git pull origin master
无需从两个存储库输入任何密码.
如果你有多台机器,你可以将钥匙复制到每台机器上并重复使用它们,但我建议你做腿部工作,每台机器生成1个钥匙和回购.您将拥有更多要处理的密钥,但如果受到攻击,您将不那么容易受到攻击.
Eri*_*ang 20
我在github上有两个帐户,这是我做的(上linux
)使它工作.
ssh-keygen
,正确命名,以便让生活更轻松.ssh-add path_to_private_key
的〜/ .ssh /配置
Host github-kc
Hostname github.com
User git
IdentityFile ~/.ssh/github_rsa_kc.pub
# LogLevel DEBUG3
Host github-abc
Hostname github.com
User git
IdentityFile ~/.ssh/github_rsa_abc.pub
# LogLevel DEBUG3
Run Code Online (Sandbox Code Playgroud)
为repo设置远程URL:
github-kc
:github-abc
~/.ssh/config
:Host
选项github-kc
:
git@
github- <identify_specific_user>
主机可以是任何可以识别主机和帐户的值,它不需要是真正的主机,例如
git remote set-url origin git@github-kc:kuchaguangjie/pygtrans.git
在github上为我的本地笔记本电脑识别我的一个帐户,
当为git repo设置远程url时,这是后面的值,这就是Host
repo映射到Host的方式,例如Hostname
github.com
]User
git
用于github,IdentityFile
git LogLevel
为github,DEBUG3
linux
ssh-keygen
提供最详细的信息.And*_*ter 17
使用以下IdentityFile
参数~/.ssh/config
:
Host github.com
HostName github.com
IdentityFile ~/.ssh/github.rsa
User petdance
Run Code Online (Sandbox Code Playgroud)
编辑 ssh 配置文件的一种可能更简单的替代方法(如所有其他答案中所建议的)是将单个存储库配置为使用不同的(例如非默认)ssh 密钥。
在要使用不同密钥的存储库中,运行:
git config core.sshCommand 'ssh -i ~/.ssh/id_rsa_anotheraccount'
Run Code Online (Sandbox Code Playgroud)
如果您的密钥受密码保护并且您不想每次都输入密码,则必须将其添加到 ssh-agent。以下是ubuntu和macOS 的操作方法。
还应该可以使用全局 git config 和条件包含将这种方法扩展到多个存储库(参见示例)。
就我而言,上述解决方案都没有解决我的问题,但 ssh-agent 可以。基本上,我做了以下事情:
使用 ssh-keygen 生成密钥对,如下所示。它将生成一个密钥对(在本例中.\keyfile
为 和.\keyfile.pub
)
ssh-keygen -t rsa -b 4096 -C "yourname@yourdomain" -f keyfile
上传keyfile.pub
到git提供商
ps -ef | grep ssh-agent
它是否已经在运行)ssh-add .\keyfile
以添加凭据git clone git@provider:username/project.git
我花了很多时间来理解所有的步骤。所以让我们一步一步地描述:
ssh-keygen -t rsa
. proj1.id_rsa
毫无疑问地给它一个替代选择,因为你不需要密码。在 中添加新部分.ssh/config
:
Host proj1.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/proj1.id_rsa
Run Code Online (Sandbox Code Playgroud)考虑第一部分并注意proj1.github.com
我们稍后会回到该部分。
ssh-add ~/.ssh/proj1.id_rsa
proj1.github.com
(正是配置文件中的主机)。
git clone git@proj1.github.com
.不要惹房东
小智 5
I find solution of this problem and try to explain it.
We should make change in both file ~/.ssh/config
and in ~/.gitconfig
Let's show.
You will need one key for each different account you will use on either GitHub or BitBucket.
Whichever site you have more identities with determines how many keys you will need.
A single key can act both as a GitHub and BitBucket key but cannot be associated with more than one BitBucket or GitHub account.
If you already have created a key in ~/.ssh/id_rsa (the default location), you may use that in place of the ~/.ssh/msmith key in my examples or you can leave that key and add additional keys for the other identities.
Create the keys and ssh-add them (make sure to enter a secure password and do not just leave it blank)
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/key1_rsa -C "msmith@example.com"
Run Code Online (Sandbox Code Playgroud)
Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): ************ Enter same passphrase again: Your identification has been saved in /Users/me/.ssh/key1_rsa. Your public key has been saved in /Users/me/.ssh/key1_rsa.pub. The key fingerprint is: ...
$ ssh-add ~/.ssh/key1_rsa
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/key2_rsa -C "jblige@example.com"
Run Code Online (Sandbox Code Playgroud)
Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): ************ Enter same passphrase again: Your identification has been saved in /Users/me/.ssh/key2_rsa. Your public key has been saved in /Users/me/.ssh/key2_rsa.pub. The key fingerprint is: ...
$ ssh-add ~/.ssh/key2_rsa
Run Code Online (Sandbox Code Playgroud)
Create a file in ~/.ssh/config (if it does not already exist). You must make sure it is readable only by the owner and the group and public bits are set off.
touch ~/.ssh/config
chmod 600 ~/.ssh/config
Run Code Online (Sandbox Code Playgroud)
We now need to add SSH configuration that specifies the github and bitbucket hostnames but with a suffix appended to qualify which key to use. We set the HostName to the correct github.com or bitbucket.org address.
注意:Linux 用户应该省略 UseKeychain yes 或添加 IgnoreUnknown UseKeychain (感谢 Soulofmischief)
~/.ssh/config
...
Host github.com-msmith
HostName github.com
UseKeychain yes
AddKeysToAgent yes
User git
IdentityFile ~/.ssh/msmith_rsa
IdentitiesOnly yes
Host bitbucket.org-msmith
HostName bitbucket.org
UseKeychain yes
AddKeysToAgent yes
User git
IdentityFile ~/.ssh/msmith_rsa
IdentitiesOnly yes
Host github.com-jblige
HostName github.com
UseKeychain yes
AddKeysToAgent yes
User git
IdentityFile ~/.ssh/jblige_rsa
IdentitiesOnly yes
Host bitbucket.org-jblige
HostName bitbucket.org
UseKeychain yes
AddKeysToAgent yes
User git
IdentityFile ~/.ssh/jblige_rsa
IdentitiesOnly yes
...
Run Code Online (Sandbox Code Playgroud)
登录每个用户的 GitHub,并将 ~/.ssh/xxxxx.pub 中的密钥添加到相应用户授权的 SSH 密钥中。
有关更多信息,请参阅: https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html
或者
https://help.github.com/en/articles/adding-a-new-ssh-key-to-your-github-account
您将需要一个目录,其中与给定键对应的所有代码都将被签出。
我更喜欢将所有这些目录保存在我的 home ~/src 的一个目录中,并根据与密钥关联的帐户名来命名它们
mkdir -p ~/src/msmith
mkdir -p ~/src/jblige
Run Code Online (Sandbox Code Playgroud)
在每个目录中放置一个 .gitconfig 文件。
~/src/msmith/.gitconfig
...
[user]
email = msmith@example.com
[url "git@bitbucket.org-msmith"]
insteadOf = git@bitbucket.org
[url "git@github.com-msmith"]
insteadOf = git@github.com
~/src/jblige/.gitconfig
...
[user]
email = jblige@example.com
signingkey = ABCD1234
[url "git@bitbucket.org-jblige"]
insteadOf = git@bitbucket.org
[url "git@github.com-jblige"]
insteadOf = git@github.com
[commit]
gpgsign = true
Run Code Online (Sandbox Code Playgroud)
这样,我就为两个密钥使用了正确的电子邮件地址,甚至为 jblige 设置了自动提交签名。我还将原始 SSH 连接的所有主机名重写为我在 SSH 配置文件中创建的正确后缀主机名。
有关 GPG 签名的更多信息,请参阅:
https://help.github.com/en/articles/signing-commits
或者
https://confluence.atlassian.com/bitbucketserver/using-gpg-keys-913477014.html
要激活 ~/src/* 中的 .gitconfig 文件,请编辑主目录中的 .gitconfig 文件,并为引用其所在目录的每个 .gitconfig 文件添加 includeif 语句
~/.gitconfig
...
[includeif "gitdir:~/src/msmith/"]
path = ~/src/msmith/.gitconfig
[includeif "gitdir:~/src/jblige/"]
path = ~/src/jblige/.gitconfig
Run Code Online (Sandbox Code Playgroud)
不要忘记 [includeif "gitdir:... 语句中的尾部斜杠。
然后,您使用 SSH 克隆地址克隆代码(即 git@bitbucket.org... 或 git@github.com...,而不是 https://bitbucket.org... 也不是 https://github.com。 ..) 进入与您要用于该克隆的密钥相对应的目录。
$ cd ~/src/msmith
$ git clone git@github.com:someuser/somerepo.git
...
Run Code Online (Sandbox Code Playgroud)
由于重写,git 实际上会尝试使用与 SSH 配置文件中的配置相对应的后缀地址进行克隆,但由于 SSH 配置,它在实际连接到主机时将使用原始主机名,确保您使用正确的密钥。
所有向这些存储库的提交/拉取/推送都将使用相应的配置/密钥/帐户。
归档时间: |
|
查看次数: |
101352 次 |
最近记录: |