← Week 4 - Editors, tmux and SSH basics notes
Clone from GitHub over SSH in WSL (Permission denied (publickey))¶
A first git clone over SSH in a fresh WSL install fails with Permission denied (publickey). This note explains the cause, the fix, and how to use more than one GitHub account on the same machine.
Problem¶
$ git clone git@github.com:bartvanhoey/LinuxInSixMonths.git
Cloning into 'LinuxInSixMonths'...
The authenticity of host 'github.com (140.82.121.3)' can't be established.
ED25519 key fingerprint is: SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
The host-key prompt is normal on the first connection. Answer with the full word yes, not y. Check the fingerprint against GitHub's published SSH key fingerprints before you accept it.
Cause¶
WSL has its own home directory, so it does not see the SSH keys you made on Windows. A fresh install has no key at all:
ls -la ~/.ssh # only known_hosts
ssh-add -l # Could not open a connection to your authentication agent.
GitHub only lets you in if your client offers a private key whose public half is registered on your account. No key means Permission denied (publickey).
Fix: create a key and add it to GitHub¶
1 - Generate a key¶
ssh-keygen -t ed25519 -C "you@example.com"
Accept the default path (~/.ssh/id_ed25519). A passphrase is optional but recommended.
2 - Copy the public key¶
cat ~/.ssh/id_ed25519.pub
Copy the whole line. Only ever share the .pub file, never the private key.
3 - Add it to GitHub¶
On GitHub: Settings → SSH and GPG keys → New SSH key. Paste the line and save.
4 - Test and clone¶
ssh -T git@github.com
# Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
git clone git@github.com:bartvanhoey/LinuxInSixMonths.git
Other options¶
- Reuse the Windows key: if you already have a key in
C:\Users\<you>\.ssh, copy it into WSL. SSH refuses a private key that others can read, so fix the mode:
cp /mnt/c/Users/<you>/.ssh/id_ed25519* ~/.ssh/
chmod 600 ~/.ssh/id_ed25519
- HTTPS instead of SSH:
git clone https://github.com/bartvanhoey/LinuxInSixMonths.gitworks without login for a public repo. To push you need a personal access token orgh auth login.
Multiple GitHub accounts¶
You can use several accounts (for example personal and work) on one machine. Each account needs its own key, because GitHub rejects a key that is already on another account. A host alias in ~/.ssh/config then picks the key.
1 - One key per account¶
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_personal -C "personal"
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "work"
Add each .pub file to its own GitHub account.
2 - A host alias per account in ~/.ssh/config¶
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
chmod 600 ~/.ssh/config
IdentitiesOnly yes matters. Without it SSH may offer another key first, GitHub accepts it, and you are signed in as the wrong account.
3 - Use the alias instead of github.com¶
git clone git@github-personal:bartvanhoey/LinuxInSixMonths.git
git clone git@github-work:SomeOrg/some-repo.git
# an existing clone
git remote set-url origin git@github-work:SomeOrg/some-repo.git
Check each alias. Each should greet you with that account's username:
ssh -T git@github-personal
ssh -T git@github-work
4 - The right commit author per account¶
The SSH key decides who may push. The commit author comes from user.email, which is a separate setting. Keep each account's repos in its own folder and let git switch the identity by folder. In ~/.gitconfig:
[includeIf "gitdir:~/src/work/"]
path = ~/.gitconfig-work
And in ~/.gitconfig-work:
[user]
name = Your Name
email = you@work.example
Check it from inside a repo with git config user.email.
Troubleshooting¶
- Still
Permission denied (publickey): runssh -vT git@github.comand look forOffering public keylines. If none appear, SSH cannot find the key. Check the file name and theIdentityFilepath. Hi <other-account>!: the wrong key was used. AddIdentitiesOnly yesto the host alias and clone with the alias.UNPROTECTED PRIVATE KEY FILE!: the key is readable by others, typically after copying from/mnt/c. Runchmod 600on it.- Passphrase prompt on every command: start an agent and load the key once per session with
eval "$(ssh-agent -s)"andssh-add ~/.ssh/id_ed25519.