If you work across multiple environments—like a Linux VM and WSL2 on Windows—you might encounter SSH authentication issues when accessing GitHub from WSL2, even if your Linux VM’s SSH setup works flawlessly. This guide shows how to transfer your SSH keys from your Linux VM to WSL2 for seamless GitHub access.
The Problem
I generated SSH keys on my Linux VM and connected to GitHub without issues. However, when cloning a repo from WSL2, I got:
git clone git@github.com:alok-38/Introduction-to-Software-Engineering.git
Cloning into 'Introduction-to-Software-Engineering'...
The authenticity of host 'github.com (20.207.73.82)' 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.
This means the SSH agent in WSL2 couldn’t find or authenticate with the correct private key.
The Solution: Copy Your SSH Keys to WSL2
Here’s a simple, step-by-step way to get your keys from your Linux VM to WSL2 and make GitHub happy.
- Step 1: Confirm SSH Access from Linux VM Make sure your Linux VM has working SSH keys and can connect to GitHub.
ssh -T git@github.com
# You should see:
# Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
If this works, your Linux VM setup is good.
- Step 2: Create .ssh Directory in WSL2 Open your WSL2 terminal and create the .ssh directory if it doesn’t exist:
mkdir -p ~/.ssh
- Step 3: Copy SSH Keys from Linux VM to WSL2
Use
scp
(secure copy) to transfer your private and public keys from the Linux VM to WSL2. Replace
192.168.45.100
with your Linux VM’s IP address.
scp alok@192.168.45.100:/home/alok/.ssh/id_rsa ~/.ssh/id_rsa
scp alok@192.168.45.100:/home/alok/.ssh/id_rsa.pub ~/.ssh/id_rsa.pub
You’ll need to enter the Linux VM password when prompted.
- Step 4: Secure Your Keys in WSL2 Set the correct permissions to keep your private key safe:
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
- Step 5: Test SSH Authentication from WSL2 Try authenticating with GitHub from WSL2:
ssh -T git@github.com
You should see a success message similar to the one in Step 1.
- Step 6: Clone Your Repository Now, try cloning your repo:
git clone git@github.com:alok-38/Introduction-to-Software-Engineering.git
Why Does This Happen?
WSL2 runs a Linux kernel on Windows but maintains its own filesystem and environment. SSH keys generated in a VM are not automatically accessible in WSL2 unless explicitly copied over. By securely copying your keys and setting proper permissions, you enable WSL2 to use your existing GitHub authentication setup.
Top comments (0)