Connecting to GitHub over SSH is one of those things every developer ends up doing, and most tutorials overcomplicate it. The whole flow is five commands on the local machine plus a single paste into the GitHub UI. It takes about five minutes the first time and saves you from typing your username and password on every push.
This walkthrough covers Ubuntu 22.04, but the same flow works on any current Ubuntu release.
Key facts:
- Generate a key pair with ssh-keygen on the local machine.
- Copy the public key with cat into your clipboard.
- Add it to GitHub under Settings, then SSH and GPG keys.
- Test the connection with a real git clone.
- The default key filename is id_rsa unless you choose otherwise.
Step 1: Generate an SSH key pair
Open a terminal and run:
“`bash
Ssh-keygen
“`
You will be asked three things. Where to save the file: press Enter to accept the default, which saves the key as id_rsa and id_rsa.pub in ~/.ssh. A passphrase: optional but recommended. Anything else: press Enter.
By convention, id_rsa is the private key and id_rsa.pub is the public key. The private key never leaves your machine. The public key is what you paste into GitHub.
If you already have a key in that location, ssh-keygen will warn you before overwriting it. Pick a different filename if you want to keep the existing key.
Step 2: Copy the public key
Show the contents of the public key file:
“`bash
Cat /home/$USER/.ssh/id_rsa.pub
“`
The whole string, starting with ssh-rsa and ending with your hostname or email, is what you need. Copy it from the terminal.
A common mistake is copying only the first line, which usually works but sometimes breaks if the key is unusually long. The text is one line in most cases.
Step 3: Add the key to GitHub
Sign in to GitHub in your browser. Click your avatar in the top-right corner and open Settings. In the left sidebar, find SSH and GPG keys and click it. Click the New SSH Key button.
Give the key a label, like "Ubuntu 22.04 work laptop", so you can identify it later. Paste the public key into the Key field. Click Add SSH Key to confirm.
GitHub will email you when a new key is added, which is your safety net if anyone else tries to.
Step 4: Test the connection
The clean way to confirm everything works is to clone a private repository:
“`bash
Git clone [email protected]:username/repo-name.git
“`
If the clone starts pulling files, you are done. If GitHub prompts you to confirm the host fingerprint, type yes and continue.
A failure at this step usually means either the wrong key was pasted, or the key was added to a personal account instead of the right organization.
Say you are migrating from HTTPS
Say you have been authenticating with a personal access token over HTTPS and the token expiration is starting to bite. Adding an SSH key takes a few minutes and removes the expiration problem entirely. After it is set up, every push and pull uses your private key, no token juggling needed.
For local repos that already point at the HTTPS remote, you can switch them in place. Open .git/config and replace the https URL with the ssh URL, or run git remote set-url origin [email protected]:username/repo-name.git.
What to watch next
If you juggle multiple machines, generate a separate key on each and label them carefully inside GitHub. Lost keys can be removed from the GitHub settings page at any time, which is the right move the moment a laptop retires.
For stronger security, move from RSA to ed25519. GitHub supports it, and it is both faster and harder to brute-force. The setup flow is identical, just swap ssh-keygen -t ed25519 for the default command.
Key takeaways
- Five commands on the local side, one paste on GitHub, and you are done.
- Label every key you add so you can revoke the right one later.
- Switch to ed25519 if you are setting up SSH keys for the first time.
Frequently asked questions
Do I need to enter a passphrase?
It is optional. A passphrase protects the key if someone gets access to your machine, but it does mean typing the passphrase every time you push. Ssh-agent can cache it for you.
Why does GitHub still ask for my password?
That usually means the clone URL is still using https instead of [email protected]. Switch the remote URL and try again.
Can I use the same SSH key on multiple machines?
Yes, but it is cleaner to generate a separate key per machine. That way, revoking one machine's access does not break the others.
What is the difference between id_rsa and id_ed25519?
Id_rsa is the default RSA key. Id_ed25519 is a newer elliptic-curve key that is shorter, faster, and recommended for new setups.
How do I remove an old SSH key from GitHub?
Go to Settings, then SSH and GPG keys, find the key you want to delete, and click Delete. You may need to type your GitHub password to confirm.