Adding SSH Public Key in Windows for WSL

To add an SSH public key to Windows for WSL (Windows Subsystem for Linux) on the same machine, you’ll typically follow these steps:

Generate SSH Key Pair: If you haven’t already, you’ll need to generate an SSH key pair (public and private keys). You can do this using the ssh-keygen command. Open a terminal (Command Prompt or PowerShell) in Windows and run the following command:

    ssh-keygen -t rsa -b 2048
    

    This command will generate a new RSA SSH key pair with a bit length of 2048. Follow the prompts to specify a location to save the keys and optionally set a passphrase.

    Copy Public Key to WSL: After generating the SSH key pair, you need to copy the public key to your WSL distribution. You can use a utility like scp to securely copy the public key to your WSL home directory. For example:

    scp C:\Users\YourUsername\.ssh\id_rsa.pub user@localhost:/home/user/.ssh/
    

    Replace C:\Users\YourUsername\.ssh\id_rsa.pub with the path to your public key file, user with your WSL username, and localhost with the hostname or IP address of your WSL distribution.

    Add Public Key to authorized_keys File in WSL: Once the public key is copied to WSL, you need to add it to the authorized_keys file in your WSL ~/.ssh/ directory. You can do this by running the following commands in WSL:

    cat id_rsa.pub >> ~/.ssh/authorized_keys

    This command appends the contents of the public key file (id_rsa.pub) to the authorized_keys file.

      Verify SSH Access: After adding the public key to the authorized_keys file in WSL, you should be able to SSH into your WSL distribution from Windows without being prompted for a password. You can test SSH access by running:

      ssh user@localhost

      Replace user with your WSL username.

        By following these steps, you’ll be able to add an SSH public key to Windows for WSL on the same machine and enable passwordless SSH access between them. This setup allows for seamless interaction between Windows and WSL using SSH.

        Leave a comment

        I’m Omkar

        Welcome to Tech Blog.