DEV Community

alok-38
alok-38

Posted on

1

How to Use Your Linux VM’s SSH Keys for GitHub Access in WSL2

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.
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Step 5: Test SSH Authentication from WSL2 Try authenticating with GitHub from WSL2:
ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️