Issue
I have set the Github passwordless login as below.
ssh-keygen -t rsa -P ''
cat .ssh/id_rsa.pub |xclip
I pasted the public key into ssh and gpg keys on the Github site.
git init
git config --global user.name "someone"
git config --global user.email "[email protected]"
git config remote.origin.url git+ssh://[email protected]/someone/newstart.git
ssh -T [email protected]
git clone git+ssh://[email protected]/someone/newstart.git /tmp/newstart
The above works.
Now I want to use a different key name for Github instead of its default name id_rsa.
ssh-keygen -t rsa -P '' -f .ssh/id_rsa.github
cat .ssh/id_rsa.github.pub |xclip
I pasted the new pub key into ssh and gpg keys on the Github site.
git init
git config --global user.name "someone"
git config --global user.email "[email protected]"
git config remote.origin.url git+ssh://[email protected]/someone/newstart.git
ssh -T [email protected]
The above does not work.
git clone git+ssh://[email protected]/someone/newstart.git /tmp/newstart
The above also does not work.
ssh -i .ssh/id_rsa.github -T [email protected]
The command above can connect to Github successfully, but I can't type the following command to clone the repository.
git clone -i .ssh/id_rsa.github git+ssh://[email protected]/someone/newstart.git /tmp/newstart
Can't I use a pub key with another name, such as id_rsa.github instead of the default name id_rsa?
Solution
You need to tell SSH to use this key for Github. Put this in ~/.ssh/config
:
Host github.com
IdentityFile ~/.ssh/id_rsa.github
User git
EDIT:
I want to address those in the comments saying that Github or Gitlab "ignore" the HostName
and only use Host
. That's not quite correct.
The opening line of these blocks, the Host
line, is one or more hostname patterns that define when the rest of the block is applied. For example, you could say:
Host bitbucket bitbucket.org holymoly
HostName bitbucket.org
User git
What this says is:
- If ssh is called with a target hostname of
bitbucket
,bitbucket.org
, orholymoly
, then apply the following two properties: - First, connect to
bitbucket.org
no matter which of the 3 names the user specified on the commandline; they are just aliases - Second, the username for this connection is
git
.
This is the distinction between Host
and HostName
.
Note that in the above, if bitbucket.org
weren't even in the Host
line, you could still tell ssh to connect to holymoly
and you'd end up connected to bitbucket.org.
Host
is a list of names and/or patterns that you can specify in your SSH command, that will trigger this block to match at all.
HostName
is the real hostname to use, in the event that the one in Host
is not the real hostname. If you don't supply a HostName
, then the value of Host
is used as the default value.
You can use patterns for Host
as well.
Host *.mydomain.com
HostName proxy.mydomain.com
Here, no matter what you put as the hostname, if it's in the mydomain.com
domain name, it will actually connect to the proxy hostname specified.
I hope that helps explain the difference, and makes my original answer less confusing.
Answered By - Dan Lowe Answer Checked By - David Marino (WPSolving Volunteer)