How to set up a computer so git can push to and pull from GitHub on the command line without asking you to log in each time. Written for WSL (Ubuntu); the same steps work on macOS and Linux.
You make a key pair on your computer. The private key stays on the computer; the public key goes on GitHub. When git talks to GitHub over SSH, the two are matched and no password is needed.
Keys belong to one environment. WSL and Windows each have their own ~/.ssh, so a key made in WSL doesn't help Git for Windows, and the reverse. Do these steps in each place you use git.
ls ~/.ssh
If you see id_ed25519 and id_ed25519.pub, you already have a key. Skip to step 3, or make a new one if you don't remember its passphrase.
ssh-keygen -t ed25519 -C "you@example.com"
- Use your GitHub email address. It's only a label.
- Press Enter to accept the default file location.
- Enter a passphrase. It protects the key if the computer is stolen; step 3 means you type it once per session rather than on every push. An empty passphrase works too, with no prompts at all, but anyone with the file can use it.
An agent holds the unlocked key in memory. WSL doesn't start one on its own, and a plain ssh-agent would only serve one terminal. keychain starts one agent and shares it across all terminals. Install it:
sudo apt install keychain
Add this line to the end of ~/.zshrc:
eval "$(keychain --eval --quiet id_ed25519)"
Then create or edit ~/.ssh/config so SSH uses this key for GitHub:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
Open a new terminal. It asks for the passphrase once; after that, no prompts until you restart WSL or the computer.
Print the public key and copy all of it:
cat ~/.ssh/id_ed25519.pub
On GitHub, go to your profile picture > Settings > SSH and GPG keys > New SSH key. Give it a title naming the computer (e.g. "Laptop WSL"), leave Key type as Authentication Key, paste the key, and click Add SSH key.
Never paste or share the file without .pub. That's the private key.
ssh -T git@github.com
The first time, it asks whether to trust GitHub's host key. Type yes. You should see:
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
Clone with the SSH URL (the SSH tab under the green Code button):
git clone git@github.com:<owner>/<repo>.git
A repo you already cloned with an https:// URL keeps asking for credentials. Switch it to SSH from inside the repo:
git remote -v
git remote set-url origin git@github.com:<owner>/<repo>.git
Permission denied (publickey). The key isn't on GitHub, or SSH isn't offering it. Checkssh-add -llists the key; if not, runssh-add ~/.ssh/id_ed25519. Make sure the key on GitHub matches~/.ssh/id_ed25519.pub.- Still asked for a username and password. The repo's remote is HTTPS. See step 6.
- Asked for the passphrase in every terminal. Check the
keychainline is in~/.zshrcand thatkeychainis installed (which keychain). bad permissions/UNPROTECTED PRIVATE KEY FILE. Runchmod 700 ~/.ssh && chmod 600 ~/.ssh/id_ed25519.