SSH config file: Use correct IdentityFile

Posted on Sun 03 July 2022 in Linux

بِسْمِ ٱللَّٰهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ

I was recently trying to set up a new code-hosting repo at Codeberg using a new SSH key. I created the SSH key like so:

ssh-keygen -t rsa -b 4096 -C "somename@mail.org"

In order to test that the connection to Codeberg works, I ran:

ssh -vT git@codeberg.org

The debug output showed that SSH was attempting to use the first few keys it could find and none of them were the key I specified sample_key.

I found this answer from SuperUser that says the solution to fix the IdentityFile problem is to add the following to the ~/.ssh/config file:

#sample host

Host codeberg
 HostName codeberg.org
 User git
 IdentityFile ~/.ssh/sample_key

#at the end of the file, after every Host has been added, add this:

Host *
 ForwardAgent no
 IdentitiesOnly yes

But this did not work for me either! SSH still kept using the first few keys it could find and not using sample_key at all.

The fix for this solution is to add the correct URL for Host such that it matches HostName.

Example:

Host codeberg.org
 HostName codeberg.org
 User git
 IdentityFile ~/.ssh/sample_key

Below is a list of Host/HostNames that should work for various Git repo providers:

#gitlab

Host gitlab.com
 HostName gitlab.com
 ...

#codeberg

Host codeberg.org
 HostName codeberg.org
 ...

#bitbucket

Host bitbucket.org
 HostName bitbucket.org
 ...

#github

Host github.com
 HostName github.com
 ...

The full explanation of the difference was shared in a toot here, reproduced verbatim:

“Host” is the name you type on the command-line - if you have a block with “Host codeberg” then it will apply if you “ssh codeberg”

“Hostname” is the name that ssh actually connects to. If you had “Host foo / Hostname bar” then when you typed “ssh foo” then ssh would connect to bar.

You can leave “Hostname” out if you don’t want to override it, i.e. you can just have “Host foo” and specify other options for that host without giving a “Hostname” and it will just connect directly to “foo” — “Host” and “Hostname” is mainly useful in combination to create aliases for hosts.

“Host” can accept multiple names - you can have “Host foo bar baz / Hostname quux” and ssh will connect to quux when you ssh to foo, bar, or baz.

See https://github.com/jordemort/dotfiles/blob/main/home/.ssh/config.d/default for an example.

So you can actually use Host codeberg but then you will need to SSH like so:

ssh codeberg

Whichever option used, I hope this article helps everyone debug this issue quickly and easily.


If you don't know how to use RSS and want email updates on my new content, consider Joining my Newsletter

The original content of this blog is a Waqf solely for the Pleasure of Allah. You are hereby granted full permission to copy, download, distribute, publish and share this content without modification under condition that full attribution is given to this author by creating a link either above or below the content that links back to the original source of the content. For any questions or ambiguity, you are requested to contact me via email for clarification.