Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SSH for GitHub Passwordless: How It Works

· 6 min read · Home Tech

In this article
  1. Background: Why Passwordless SSH for GitHub?
  2. How SSH Key Authentication Works
  3. Setting Up SSH Keys for GitHub
  4. Choosing Between SSH Keys and HTTPS Tokens
  5. Common Problems and Troubleshooting
  6. Security Considerations
  7. Alternatives to SSH Keys
  8. Frequently Asked Questions

Yes, you can use SSH keys to authenticate with GitHub without entering your password or a personal access token on every push or pull. This article explains how SSH key authentication works with GitHub, how to set it up, and how to troubleshoot common issues.

Background: Why Passwordless SSH for GitHub?

When you interact with a GitHub repository over HTTPS, Git asks for your username and password (or a personal access token) each time you push. This becomes tedious and can break automation scripts. SSH keys solve this by using a cryptographic key pair: a private key stored on your machine and a public key uploaded to GitHub. When you connect, GitHub verifies that you possess the private key corresponding to the public key on file, and if so, grants access without a password. This is the same mechanism used for secure shell access to servers, but GitHub uses it specifically for Git operations over the SSH protocol (port 22, or 443 for SSH over HTTPS).

How SSH Key Authentication Works

SSH key authentication relies on asymmetric cryptography. You generate a key pair: a private key (kept secret) and a public key (shared). The private key never leaves your computer. When you run git push to an SSH remote, Git uses your SSH client to initiate a connection to github.com. GitHub sends a challenge that can only be answered with your private key. Your SSH client signs the challenge with the private key, and GitHub verifies the signature using the public key you uploaded. If it matches, you are authenticated and the Git operation proceeds without a password prompt.

Setting Up SSH Keys for GitHub

To enable passwordless SSH with GitHub, follow these steps:

  1. Check for existing keys: ls -la ~/.ssh (look for id_ed25519 or id_rsa).
  2. Generate a new key if needed: ssh-keygen -t ed25519 -C "your_email@example.com" (press Enter to accept default file location and optionally set a passphrase).
  3. Add the key to the SSH agent: eval "$(ssh-agent -s)" then ssh-add ~/.ssh/id_ed25519.
  4. Copy the public key: cat ~/.ssh/id_ed25519.pub.
  5. On GitHub, go to Settings → SSH and GPG keys → New SSH key, paste the public key, and save.
  6. Test the connection: ssh -T git@github.com — you should see a message like "Hi username! You've successfully authenticated...".
  7. Change your remote URL from HTTPS to SSH: git remote set-url origin git@github.com:username/repo.git.

After this, your Git commands will use SSH and no longer ask for a password.

Choosing Between SSH Keys and HTTPS Tokens

Both SSH keys and HTTPS personal access tokens (PATs) provide passwordless authentication, but they differ in several ways:

  • Convenience: SSH keys are automatically used by Git once configured; HTTPS PATs must be entered or stored in a credential manager.
  • Security: SSH private keys are protected by file permissions and optionally a passphrase. PATs can be scoped to specific permissions and revoked individually.
  • Portability: SSH keys are tied to a machine; PATs can be used on multiple machines and in CI/CD pipelines.
  • GitHub CLI: The gh CLI can use either, but it often defaults to HTTPS with a token.

For a single developer on a personal machine, SSH keys are often simpler. For teams and automation, PATs or deploy keys might be more appropriate.

Common Problems and Troubleshooting

Here are frequent issues and their fixes:

  • Permission denied (publickey): Ensure your public key is added to GitHub and that your private key is loaded into the SSH agent (ssh-add -l to list). Also check that you're using the correct remote URL.
  • Port 22 connection refused: Some networks block SSH port 22. GitHub offers SSH over HTTPS on port 443. Configure ~/.ssh/config with Host github.com and Port 443.
  • Agent admitted failure to sign: This happens when the SSH agent isn't running or the key isn't added. Run eval "$(ssh-agent -s)" and ssh-add again.
  • Multiple keys: If you have multiple SSH keys, specify which one to use in ~/.ssh/config with IdentityFile.
  • Passphrase prompts: If you set a passphrase on your key, you'll be asked for it each time unless you use ssh-add to store it in the agent.

Security Considerations

SSH keys are powerful; anyone with your private key can access your GitHub account. Protect them:

  • Keep your private key secret and never share it.
  • Use a strong passphrase when generating the key to add an extra layer of protection.
  • Regularly review the SSH keys listed in your GitHub settings and remove any you don't recognize.
  • Use different keys for different machines or services to limit damage if one is compromised.
  • Consider using a hardware security key (like a YubiKey) for storing SSH keys, though this is more advanced.

GitHub also supports SSH keys with a certificate authority for organizations, but that's beyond individual use.

Alternatives to SSH Keys

If SSH keys aren't suitable, consider these alternatives:

  • HTTPS with credential helper: Git can store your PAT in the system credential manager, so you only enter it once.
  • GitHub CLI: The gh auth login command sets up authentication and can configure Git to use it.
  • Deploy keys: For a single repository, a deploy key (an SSH key with read-only or write access to one repo) can be added directly to the repo settings.
  • Personal access tokens: You can generate a PAT and use it as a password for HTTPS operations. This is common in CI/CD.

Each method has trade-offs in security and convenience; choose based on your workflow.

Frequently Asked Questions

Q: Do I need to use SSH for GitHub, or can I stay with HTTPS?
A: You can stay with HTTPS, but you'll need to use a personal access token instead of your password. SSH is an alternative that many find more convenient after initial setup.

Q: What if I already have an HTTPS remote? Can I switch to SSH?
A: Yes, simply run git remote set-url origin git@github.com:username/repo.git to change the remote URL. Your existing repository will then use SSH.

Q: How do I add my SSH key to the agent permanently on macOS?
A: Add ssh-add --apple-use-keychain ~/.ssh/id_ed25519 to your shell profile, or use ssh-add -K on older versions. This stores the passphrase in your keychain.

Q: Can I use the same SSH key for multiple GitHub accounts?
A: Technically yes, but GitHub doesn't allow the same public key on two accounts. You'd need separate keys and configure ~/.ssh/config with different hosts (e.g., github-work and github-personal).

Q: What is the difference between SSH keys and GPG keys?
A: SSH keys are for authentication (proving who you are) to GitHub. GPG keys are for signing commits and tags to verify that they came from you. They serve different purposes; you can use both.

Q: Why do I get "Permission denied (publickey)" even after adding the key?
A: Ensure you added the public key (not the private key) to GitHub, and that the private key is in the default location or specified in your SSH config. Also, verify the remote URL uses git@github.com and not https://.