Specify private key in git

When using Github or a Gitlab instance, I typically use a specific key.

By default however, Gitlab/hub will only try to use the default name of ~/.ssh/id_rsa, and then fail if that key isn't the one specified in your Gitlab/hug profile.

On Github, I added the public key that the corresponding private key is ~/.ssh/git.

Even though I added the public key to my Github account, and have the corresponding private key available, attempting to clone a Github respository fails:

git clone git@github.com:unifi-utilities/unifios-utilities.git
Cloning into 'unifios-utilities'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Using ssh -Tv git@github.com to see what was going shows that git by default, never tries to use my ~/.ssh/git private key:

ssh -Tv git@github.com
OpenSSH_8.9p1 Ubuntu-3ubuntu0.1, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to github.com [140.82.114.3] port 22.
debug1: Connection established.
debug1: identity file /home/tom/.ssh/id_rsa type 0
debug1: identity file /home/tom/.ssh/id_rsa-cert type -1
debug1: identity file /home/tom/.ssh/id_ecdsa type -1
debug1: identity file /home/tom/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/tom/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/tom/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/tom/.ssh/id_ed25519 type -1
debug1: identity file /home/tom/.ssh/id_ed25519-cert type -1
debug1: identity file /home/tom/.ssh/id_ed25519_sk type -1
debug1: identity file /home/tom/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/tom/.ssh/id_xmss type -1
debug1: identity file /home/tom/.ssh/id_xmss-cert type -1
debug1: identity file /home/tom/.ssh/id_dsa type -1
debug1: identity file /home/tom/.ssh/id_dsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1
debug1: Remote protocol version 2.0, remote software version babeld-901a823a
debug1: compat_banner: no match: babeld-901a823a
debug1: Authenticating to github.com:22 as 'git'
debug1: load_hostkeys: fopen /home/tom/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
debug1: load_hostkeys: fopen /home/tom/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'github.com' is known and matches the ED25519 host key.
debug1: Found key in /home/tom/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/tom/.ssh/id_rsa RSA SHA256:hUnPatk4fBRknwsjuYoalfM77pVkAvKwDiZ0FVjvxZs
debug1: Will attempt key: /home/tom/.ssh/id_ecdsa
debug1: Will attempt key: /home/tom/.ssh/id_ecdsa_sk
debug1: Will attempt key: /home/tom/.ssh/id_ed25519
debug1: Will attempt key: /home/tom/.ssh/id_ed25519_sk
debug1: Will attempt key: /home/tom/.ssh/id_xmss
debug1: Will attempt key: /home/tom/.ssh/id_dsa
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/tom/.ssh/id_rsa RSA SHA256:hUnPatk4fBRknwsjuYoalfM77pVkAvKwDiZ0FVjvxZs
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/tom/.ssh/id_ecdsa
debug1: Trying private key: /home/tom/.ssh/id_ecdsa_sk
debug1: Trying private key: /home/tom/.ssh/id_ed25519
debug1: Trying private key: /home/tom/.ssh/id_ed25519_sk
debug1: Trying private key: /home/tom/.ssh/id_xmss
debug1: Trying private key: /home/tom/.ssh/id_dsa
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

Specifying the private key to use

At this point, there are two ways to specify my private key:

  • Edit the ~/.ssh/config file to point to the appropriate private key.
  • Use an enviornment variable to point to the appropriate private key.

Edit the ~/.ssh/config file

To specify the use of the ~/.ssh/git key, I added this section for Github:

Host github.com
	Hostname github.com
	IdentityFile ~/.ssh/gitlab
	IdentitiesOnly yes

Note: The IdentitiesOnly yes tells SSH to only use the key specified in the ~/.ssh/config file, otherwise SSH will try every key in ~/.ssh/.

Use an enviornment variable

If your connection to a Gitlab/hub instance is a one time event, or for some strange reason you can't edit ~/.ssh/config, then you can tell git to use a specific key using an enviornment variable:

GIT_SSH_COMMAND='ssh -i /home/tom/.ssh/gitlab' git clone git@github.com/somerespository/project.git

Results

Testing

After adding the appropriate entries in my ~/.ssh/config file, re-testing access to Github works:

ssh -Tv git@github.com
OpenSSH_8.9p1 Ubuntu-3ubuntu0.1, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /home/tom/.ssh/config
debug1: /home/tom/.ssh/config line 1: Applying options for github.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to github.com [140.82.112.3] port 22.
debug1: Connection established.
debug1: identity file /home/tom/.ssh/gitlab type 0
debug1: identity file /home/tom/.ssh/gitlab-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1
debug1: Remote protocol version 2.0, remote software version babeld-901a823a
debug1: compat_banner: no match: babeld-901a823a
debug1: Authenticating to github.com:22 as 'git'
debug1: load_hostkeys: fopen /home/tom/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
debug1: load_hostkeys: fopen /home/tom/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'github.com' is known and matches the ED25519 host key.
debug1: Found key in /home/tom/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /home/tom/.ssh/gitlab RSA SHA256:QQqtVqy9aZs+zY/N6kChFfrfDvADMWZiRnMlykdL3fY explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/tom/.ssh/gitlab RSA SHA256:QQqtVqy9aZs+zY/N6kChFfrfDvADMWZiRnMlykdL3fY explicit
debug1: Server accepts key: /home/tom/.ssh/gitlab RSA SHA256:QQqtVqy9aZs+zY/N6kChFfrfDvADMWZiRnMlykdL3fY explicit
Authenticated to github.com ([140.82.112.3]:22) using "publickey".
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: filesystem
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: client_input_hostkeys: searching /home/tom/.ssh/known_hosts for github.com / (none)
debug1: client_input_hostkeys: searching /home/tom/.ssh/known_hosts2 for github.com / (none)
debug1: client_input_hostkeys: hostkeys file /home/tom/.ssh/known_hosts2 does not exist
debug1: Sending environment.
Learned new hostkey: RSA SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
Learned new hostkey: ECDSA SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM
Adding new key for github.com to /home/tom/.ssh/known_hosts: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
Adding new key for github.com to /home/tom/.ssh/known_hosts: ecdsa-sha2-nistp256 SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM
debug1: update_known_hosts: known hosts file /home/tom/.ssh/known_hosts2 does not exist
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi trnitz! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3564, received 3112 bytes, in 0.1 seconds
Bytes per second: sent 39746.6, received 34705.7
debug1: Exit status 1

Cloning a repository after editing the ~/.ssh/config file

git clone git@github.com:unifi-utilities/unifios-utilities.git
Cloning into 'unifios-utilities'...
remote: Enumerating objects: 3098, done.
remote: Counting objects: 100% (765/765), done.
remote: Compressing objects: 100% (405/405), done.
remote: Total 3098 (delta 370), reused 627 (delta 295), pack-reused 2333
Receiving objects: 100% (3098/3098), 114.40 MiB | 13.63 MiB/s, done.
Resolving deltas: 100% (1514/1514), done.

Cloning a repository using an enviornment variable

GIT_SSH_COMMAND='ssh -i /home/tom/.ssh/gitlab' git clone git@github.com:unifi-utilities/unifios-utilities.git
Cloning into 'unifios-utilities'...
remote: Enumerating objects: 3098, done.
remote: Counting objects: 100% (765/765), done.
remote: Compressing objects: 100% (405/405), done.
remote: Total 3098 (delta 370), reused 628 (delta 295), pack-reused 2333
Receiving objects: 100% (3098/3098), 114.40 MiB | 27.68 MiB/s, done.
Resolving deltas: 100% (1513/1513), done.