使用非默认密钥名称(id_rsa除外)

it_*_*ure 8 git ssh github

我已将Github无密码登录设置如下.

ssh-keygen -t rsa  -P ''  
cat .ssh/id_rsa.pub  |xclip 
Run Code Online (Sandbox Code Playgroud)

我将公钥粘贴到Github网站上的ssh和gpg密钥中.

git init  
git config --global user.name "someone"  
git config --global user.email  "sbd@gmail.com"  
git config remote.origin.url git+ssh://git@github.com/someone/newstart.git  
ssh -T git@github.com  
git clone  git+ssh://git@github.com/someone/newstart.git   /tmp/newstart
Run Code Online (Sandbox Code Playgroud)

以上工作.

现在我想为Github使用不同的键名,而不是默认名称id_rsa.

ssh-keygen -t rsa  -P ''  -f  .ssh/id_rsa.github
cat .ssh/id_rsa.github.pub  |xclip 
Run Code Online (Sandbox Code Playgroud)

我将新的pub密钥粘贴到Github网站上的ssh和gpg密钥中.

git init  
git config --global user.name "someone"  
git config --global user.email  "sbd@gmail.com"  
git config remote.origin.url git+ssh://git@github.com/someone/newstart.git  
ssh -T git@github.com 
Run Code Online (Sandbox Code Playgroud)

以上不起作用.

git clone  git+ssh://git@github.com/someone/newstart.git   /tmp/newstart
Run Code Online (Sandbox Code Playgroud)

以上也行不通.

ssh -i .ssh/id_rsa.github -T git@github.com
Run Code Online (Sandbox Code Playgroud)

上面的命令可以成功连接到Github,但我无法键入以下命令来克隆存储库.

git clone  -i  .ssh/id_rsa.github  git+ssh://git@github.com/someone/newstart.git   /tmp/newstart
Run Code Online (Sandbox Code Playgroud)

我不能使用其他名称的pub键,例如id_rsa.github而不是默认名称id_rsa

Dan*_*owe 10

您需要告诉SSH将此密钥用于Github.把它放进去~/.ssh/config:

Host github.com
    IdentityFile ~/.ssh/id_rsa.github
    User git
Run Code Online (Sandbox Code Playgroud)

  • 这有效,谢谢!奇怪的是,GitHub 没有从 ssh 配置文件中获取“Hostname”指令(它们只采用 Host 指令,如上面的示例),而 Bitbucket.org(或 bitbucket.com)则采用“Host bitbucket \nHostName bitbucket.org”形式。 (3认同)