GitHub上的SSH配置似乎是一场噩梦.我有多个GitHub帐户,但我可以拥有多个SSH密钥.在GitHub SSH配置部分,他们提到了这一点:
============
ssh-keygen -t rsa -C"your_email@example.com"使用提供的电子邮件作为标签创建新的ssh密钥
生成公钥/私钥rsa密钥对.
我们强烈建议保留默认设置,因此当系统提示您"输入要保存密钥的文件"时,只需按Enter继续.
#输入保存密钥的文件(/Users/you/.ssh/id_rsa):[按enter键]
为什么我总是要使用id_rsa文件?它将覆盖我现有的密钥.无论如何,我在这里给出一个新名称并生成密钥.我执行将其添加到代理程序的所有其他步骤,在GitHub SSH密钥部分中进行更新.
完成所有这些步骤后,我进入最后一步,即:
ssh -vT git@github.com
Hi animesh11! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3128, received 1976 bytes, in 0.5 seconds
Bytes per second: sent 6077.0, received 3838.9
debug1: Exit status 1
Run Code Online (Sandbox Code Playgroud)
一切都是笨拙的海鲂,但不知何故git clone仍然抱怨:
MacBook-Pro:Documents animeshsaxena$ git clone git@github.com:regentmarkets/devbox.git
Cloning into 'devbox'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I am up to my wits end why the ssh -vT works and simple cloning doesn't. It doesn't seem logical, why would github put this step in the manual if they know it's freaking useless.
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我使用.ssh config为每个特定用户设置不同的配置.
例如,在用户root下的.ssh文件夹中编辑(或创建)配置文件,并添加类似于此的内容:
Host user1-github
HostName github.com
User git
IdentityFile ~/.ssh/user1_rsa
Host user2-github
HostName github.com
User git
IdentityFile ~/.ssh/user2_rsa
Run Code Online (Sandbox Code Playgroud)
凡user1_rsa和user2_rsa是的输出ssh-keygen -t rsa -C "user1@example.com"和ssh-keygen -t rsa -C "user2@example.com"
然后,当测试时只需使用ssh -vT user1-github和ssh -vT user2-github.
此外,当克隆repos使用git clone user1-github:username1/project.git或git clone user2-github:username2/project.git.
作为对Serban 解决方案的补充,这里提供了另一种使用.ssh/config文件来区分用户的方法。有点hacky,但不需要更改主机名。
假设您有一个专业帐户和一个个人帐户,并且您像这样设置您的密钥:
$ ssh-keygen -t rsa -f ~/.ssh/perso_rsa -C "nick@perso.org"
$ ssh-keygen -t rsa -f ~/.ssh/pro_rsa -C "name.surname@pro.company.com"
Run Code Online (Sandbox Code Playgroud)
现在大多数时候,您可以使用某种东西来区分这两种身份。通常,在处理 git 存储库时,您的 git 配置与您要使用git config user.email的身份相关联,例如,将返回两个电子邮件地址之一,该地址与此存储库的身份相关联。
基于此,我们可以在 中使用Match exec <cmd>过滤指令.ssh/config,并编写如下内容:
Match host github.com exec "[ $(git config user.email) = nick@perso.org ]"
IdentityFile ~/.ssh/perso_rsa
Host github.com
IdentityFile ~/.ssh/pro_rsa
Run Code Online (Sandbox Code Playgroud)
只需将常规git@github.com:user/project.git用于两个身份。
其工作原理如下: 当主机为 时github.com,使用第一个匹配规则来确定IdentityFile要使用的。对于第一个规则,exec如果该命令返回零退出状态,则运行该命令并选择该规则。我们的测试(传递给用户的 shell)检查是否git config user.email返回nick@perso.org. 如果是这种情况,则检查返回 true、规则匹配并被~/.ssh/perso_rsa选中。如果不是,则第二条规则将作为 的包罗万象github.com,并被~/.ssh/pro_rsa选择。
我们可以为第二个身份添加另一个检查,但只有两条规则是不必要的,“包罗万象”的行为应该足够了。
我们还可以调整检查以测试user.email来自 git 的其他参数。例如,我们可以检查当前工作目录的名称。有关man ssh_config更多详细信息,请参阅Match exec。
附加说明(归功于 Acumenus):
$(git config user.email))计算为多个单词,则需要双方括号,因为单个括号会中断。理想情况下,我们希望在双引号之间加上引号,但是它们已经用于参数 forexec并且我没有找到逃避它们的方法。user.email条目git config -l)。要使用另一个密钥克隆存储库,一种解决方案是首先创建存储库,然后添加远程和 pull: mkdir foo; cd foo; git init; git config ...; git remote add origin <URL>; git pull。$HOME/.ssh/id_rsa.host如有必要,该指令可用于同时匹配多个以逗号分隔的主机 ( Match host github.com,gitlab.com exec ...)。我一直遇到这个问题,但这对我有用(顺便说一句,我在 Mac 上)
使用github的流程,我生成了一个新的ssh密钥并将其添加到我在github上的个人资料中
我config在文件夹中添加了一个文件.ssh,并引用了我的工作 ssh 密钥和我的个人 ssh 密钥,如其他答案中所述。
Host github.com
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/work_key
Host personal
HostName github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/personal_key
Run Code Online (Sandbox Code Playgroud)
然后,我将两个密钥添加到我的 ssh-agent中ssh-add:
ssh-add --apple-use-keychain ~/.ssh/work_key
ssh-add --apple-use-keychain ~/.ssh/personal_key
我验证了两个密钥均已添加ssh-add -L
然后我验证了与和 的连接,两者都有效!ssh -T git@github.comssh -T git@personal
之后,我可以使用我的个人密钥通过以下命令克隆存储库:(将我从 github 存储库复制的内容git clone git@personal:my_username/repo.git替换为git@github.com:...git@personal:...
很多这些答案和其他 SO 问题以及文档,让我得到了我需要的东西!
| 归档时间: |
|
| 查看次数: |
870 次 |
| 最近记录: |