Using SSH with GitHub: Setup, Multiple Accounts, and Troubleshooting

December 24, 2024

SSH provides a convenient way to authenticate Git operations with GitHub. A complete setup involves creating a key pair, registering the public key, and configuring the SSH client to select the correct identity. This article also covers hardware security keys, multiple accounts, and networks that block SSH's default port.

The shell examples use Bash on Linux, macOS, or Windows Git Bash. Native Windows PowerShell uses different commands for managing its SSH agent; consult the platform-specific instructions when using that client.1

Choose and generate an SSH key

For a new software key, use Ed25519. On a legacy system without Ed25519 support, GitHub documents RSA with a 4096-bit key as an alternative:1

ssh-keygen -t ed25519 -C "[email protected]"

# Alternative for a client without Ed25519 support:
ssh-keygen -t rsa -b 4096 -C "[email protected]"

The email argument is a label. When prompted, choose a file location and a passphrase. If a key already exists at the proposed location, choose another filename instead of overwriting it. A passphrase protects the saved private key; the matching .pub file is the public key you will register with GitHub.

GitHub stopped accepting DSA (ssh-dss) keys on March 15, 2022. RSA keys generated after November 2, 2021 must use SHA-2 signatures. RSA key support and support for the older SHA-1 signature algorithm are separate issues; an older client may need upgrading.1

Hardware security keys

OpenSSH 8.2 introduced support for FIDO authenticators through the ed25519-sk and ecdsa-sk key types. These require a compatible authenticator, such as a supported YubiKey, rather than one particular brand. Support depends on both the device and the SSH client.2

Insert the security key and generate a key pair:

ssh-keygen -t ed25519-sk -C "[email protected]"

# Use this alternative if the authenticator does not support Ed25519:
ssh-keygen -t ecdsa-sk -C "[email protected]"

Follow the device prompts, including touching the key when requested. Register the resulting public key with GitHub in the same way as a software key. Keep the local key files as well as the device: in the default non-resident setup, the local private-key file contains a handle needed to use the authenticator.2

The -sk suffix describes hardware-backed authentication, not a blanket guarantee that one algorithm is more secure in every deployment. Choose a supported combination of client, authenticator, and key type.

Load the key and register it with GitHub

For a Bash session using an OpenSSH agent, start the agent if one is not already available, then load the private key:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Use your actual filename if you selected a custom name or a hardware-backed key. The agent can make the loaded key available to subsequent SSH operations, reducing repeated passphrase prompts.1

Display the public key:

cat ~/.ssh/id_ed25519.pub

In GitHub account settings, open SSH and GPG keys, select New SSH key, choose Authentication Key, and paste the public key. Give it a title that identifies the machine or device. Upload only the public key, never the private-key file.3

SSH authentication and commit signing are separate uses. If you also want to use this key for signing commits, GitHub requires adding it separately as a signing key.3 For signing configuration and verification troubleshooting, see Signing Git Commits on GitHub.

Verify the connection and clone a repository

Test authentication:

ssh -T git@github.com

On the first connection, compare the displayed host-key fingerprint with GitHub's published fingerprints before accepting it. A successful response identifies your GitHub account and explains that shell access is unavailable. This is expected: the connection is for Git operations. The test can exit with status 1 even when authentication succeeds.4

The SSH username in this URL is git; GitHub identifies your account through the key. The repository owner appears in the repository path:

git clone git@github.com:OWNER/REPOSITORY.git

For example, the owner may be an organization rather than your personal username. The authenticated account must have access to that repository.

Select different keys for multiple accounts

Two GitHub accounts use the same server and SSH username. Give each identity a local host alias in ~/.ssh/config so that the repository URL selects the intended key:5

Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal
    IdentitiesOnly yes

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

Create the two keys and register each public key with its corresponding account. Test the aliases and use them in repository URLs:

ssh -T git@github-personal
ssh -T git@github-work

git clone git@github-personal:PERSONAL-OWNER/REPOSITORY.git
git clone git@github-work:WORK-ORGANIZATION/REPOSITORY.git

For an existing checkout, update its remote URL:

git remote set-url origin git@github-work:WORK-ORGANIZATION/REPOSITORY.git

IdentitiesOnly yes limits authentication to configured identities instead of trying every identity offered by the agent. IdentityFile entries can accumulate across matching configuration blocks, so review broad Host * rules if an unexpected key is still selected. Put specific host settings before general defaults.5

Use port 443 when port 22 is blocked

A connection timeout on port 22 may indicate network filtering rather than an invalid key. GitHub.com provides an SSH endpoint at ssh.github.com:443. Test it before changing the configuration:6

ssh -T -p 443 git@ssh.github.com

If authentication succeeds, you can clone through that endpoint directly:

git clone ssh://[email protected]:443/OWNER/REPOSITORY.git

To keep using ordinary GitHub SSH URLs, configure:

Host github.com
    HostName ssh.github.com
    Port 443
    User git

Then test again:

ssh -T git@github.com

For the account aliases above, set HostName ssh.github.com and Port 443 inside each alias block while keeping its IdentityFile. A block matching github.com does not automatically match github-work or github-personal.5

This is SSH traffic on the port normally used for HTTPS, not an HTTPS connection. A proxy or firewall may still block it. GitHub's documentation excludes GitHub Enterprise Server and Enterprise Cloud with data residency from this workaround.6

Handle known_hosts prompts

Switching ports may produce a first-connection prompt for [ssh.github.com]:443, even if github.com is already known. Verify the fingerprint against GitHub's official list before accepting the new entry. Do not treat a changed-key warning as a reason to disable host-key verification.46

Look up GitHub's IP ranges

For network diagnostics or firewall configuration, GitHub exposes service IP ranges through its Meta API:7

curl https://api.github.com/meta

Query the endpoint for current values rather than copying an address from an old error message or documentation example. These ranges can change.

References

Sources collected in the original drafts

The drafts also collected these discussions. Their useful topics are incorporated above, with current technical claims referenced to the official documentation.

  1. How to fix "ssh: connect to host github.com port 22: Connection timed out" for git push/pull commands? (n.d.). Stack Overflow. https://stackoverflow.com/questions/15589682/how-to-fix-ssh-connect-to-host-github-com-port-22-connection-timed-out-for-g (including the answer collected at https://stackoverflow.com/a/52817036).

  2. Longair, M. (2011, October 28). [Answer about selecting separate SSH identities for one server]. Stack Overflow. https://stackoverflow.com/a/7927828

    Key excerpt: “Even if the user and host are the same, they can still be distinguished in ~/.ssh/config.” (Longair, 2011, para. 1).

  3. gertvdijk. (2012, November 21). [Answer about preventing SSH from offering unintended keys]. Server Fault. https://serverfault.com/a/450807

    Key excerpt: “Basically, specifying IdentityFiles just adds keys to a current list the SSH agent already presented to the client.” (gertvdijk, 2012, paragraph beginning “Basically”).

  4. bk2204. (2022, January 29). [Answer about choosing an SSH key algorithm for Git]. Information Security Stack Exchange. https://security.stackexchange.com/a/259198

    Key excerpt: “Typically, I would recommend Ed25519 keys assuming the servers you're using support it.” (bk2204, 2022, para. 1).

  5. CircleCI. (n.d.). GitHub SSH Deprecation Information & Resolutions. https://support.circleci.com/hc/en-us/articles/4417542273179-GitHub-SSH-Deprecation-Information-Resolutions

Footnotes

  1. GitHub. (n.d.). Generating a new SSH key and adding it to the ssh-agent. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent ↩ ↩2 ↩3 ↩4

  2. OpenSSH. (2020, February 14). OpenSSH 8.2 release notes. Retrieved February 19, 2025, from https://www.openssh.org/txt/release-8.2 ↩ ↩2

  3. GitHub. (n.d.). Adding a new SSH key to your GitHub account. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account ↩ ↩2

  4. GitHub. (n.d.). Testing your SSH connection. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection ↩ ↩2

  5. OpenBSD. (n.d.). ssh_config(5): OpenSSH client configuration file. OpenBSD manual pages. Retrieved February 19, 2025, from https://man.openbsd.org/ssh_config ↩ ↩2 ↩3

  6. GitHub. (n.d.). Using SSH over the HTTPS port. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port ↩ ↩2 ↩3

  7. GitHub. (n.d.). REST API endpoints for meta data. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/rest/meta/meta#get-github-meta-information ↩

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2024-12-24). Using SSH with GitHub: Setup, Multiple Accounts, and Troubleshooting. https://caiborui.com/blog/2024-12-24-en-github-ssh

@misc{borui2024-dca8dfb3,
  author = {borui},
  title = {{Using SSH with GitHub: Setup, Multiple Accounts, and Troubleshooting}},
  year = {2024},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2024-12-24-en-github-ssh}
}