Skip to content

Instantly share code, notes, and snippets.

@PamoDev
Forked from ndraiman/ssh_into_wsl2.md
Last active April 11, 2025 10:23
Show Gist options
  • Save PamoDev/51a405eff0dd3d77ddc4f7821e059b9f to your computer and use it in GitHub Desktop.
Save PamoDev/51a405eff0dd3d77ddc4f7821e059b9f to your computer and use it in GitHub Desktop.

Revisions

  1. PamoDev revised this gist Apr 11, 2025. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,6 @@
    wsl.exe hostname -I
    ```
    > As of now, it seems like the WSL2 Instance is having a static IP, a lot of shenanigans wasnt needed that is described elsewhere, for setting up the ssh-agent

    5. Restart wsl by shutting it down (`wsl --shutdown`) and then starting it again (`wsl`)


    @@ -48,4 +47,8 @@
    IdentityFile ~/.ssh/id_rsa
    ```
    4. Now you can ssh to your wsl2 instance: `ssh wsl_hostname_placeholder`
    1. If you didn't add a ssh config, then you need specify those options: `ssh wsl_user_placheolder@wsl_hostname_placeholder -p 2222`
    1. If you didn't add a ssh config, then you need specify those options: `ssh wsl_user_placheolder@wsl_hostname_placeholder -p 2222`

    ### On System Reboot

    the WSL Instance is inactive at boot. As soon as a WSL Instance is started, the ssh-agent will be available. You can automate the startup or set `Windows Terminal` to start on boot with the WSL Shell.
  2. PamoDev revised this gist Apr 11, 2025. 1 changed file with 6 additions and 7 deletions.
    13 changes: 6 additions & 7 deletions ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,8 @@

    ## Install WSL2 SSH server

    1. Open your wsl ubuntu terminal
    1. Open your wsl terminal
    2. install open-ssh server

    ```sh
    sudo apt update
    sudo apt install openssh-server
    @@ -20,12 +19,12 @@
    sudo systemctl start ssh
    sudo systemctl enable ssh
    ```
    4. Set [mirrored networking mode](https://learn.microsoft.com/en-us/windows/wsl/networking#mirrored-mode-networking) in `.wslconfig`

    ```
    [wsl2]
    networkingMode=mirrored
    4. Set a fixed Hostname for the WSL Instance in (`C:\Windows\System32\drivers\etc`) for the Ip that is shown
    ```powershell
    wsl.exe hostname -I
    ```
    > As of now, it seems like the WSL2 Instance is having a static IP, a lot of shenanigans wasnt needed that is described elsewhere, for setting up the ssh-agent

    5. Restart wsl by shutting it down (`wsl --shutdown`) and then starting it again (`wsl`)


  3. @ndraiman ndraiman revised this gist Apr 29, 2024. 1 changed file with 1 addition and 6 deletions.
    7 changes: 1 addition & 6 deletions ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -26,12 +26,7 @@
    [wsl2]
    networkingMode=mirrored
    ```
    5. Expose port `2222` in the wsl2 firewall in **PRIVILEGED PowerShell terminal**:

    ```powershell
    New-NetFirewallHyperVRule -Name "SSH" -DisplayName "SSH" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 2222
    ```
    6. Restart wsl by shutting it down (`wsl --shutdown`) and then starting it again (`wsl`)
    5. Restart wsl by shutting it down (`wsl --shutdown`) and then starting it again (`wsl`)


    ## Using SSH Keys to login
  4. @ndraiman ndraiman revised this gist Apr 29, 2024. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -15,18 +15,23 @@
    3. `PubkeyAuthentication yes` - if you want to use key-based authentication (recommended)
    4. `Port 2222` - Windows ssh server already uses port 22, so we need to pick a different one.
    5. `ListenAddress 0.0.0.0`
    3. Set [mirrored networking mode](https://learn.microsoft.com/en-us/windows/wsl/networking#mirrored-mode-networking) in `.wslconfig`
    3. Activate and start the ssh server:
    ```sh
    sudo systemctl start ssh
    sudo systemctl enable ssh
    ```
    4. Set [mirrored networking mode](https://learn.microsoft.com/en-us/windows/wsl/networking#mirrored-mode-networking) in `.wslconfig`

    ```
    [wsl2]
    networkingMode=mirrored
    ```
    4. Expose port `2222` in the wsl2 firewall in **PRIVILEGED PowerShell terminal**:
    5. Expose port `2222` in the wsl2 firewall in **PRIVILEGED PowerShell terminal**:

    ```powershell
    New-NetFirewallHyperVRule -Name "SSH" -DisplayName "SSH" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 2222
    ```
    5. Restart wsl by shutting it down (`wsl --shutdown`) and then starting it again (`wsl`)
    6. Restart wsl by shutting it down (`wsl --shutdown`) and then starting it again (`wsl`)


    ## Using SSH Keys to login
  5. @ndraiman ndraiman revised this gist Apr 27, 2024. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@

    1. Open your wsl ubuntu terminal
    2. install open-ssh server

    ```sh
    sudo apt update
    sudo apt install openssh-server
    @@ -15,11 +16,13 @@
    4. `Port 2222` - Windows ssh server already uses port 22, so we need to pick a different one.
    5. `ListenAddress 0.0.0.0`
    3. Set [mirrored networking mode](https://learn.microsoft.com/en-us/windows/wsl/networking#mirrored-mode-networking) in `.wslconfig`

    ```
    [wsl2]
    networkingMode=mirrored
    ```
    4. Expose port `2222` in the wsl2 firewall in **PRIVILEGED PowerShell terminal**:
    ```powershell
    New-NetFirewallHyperVRule -Name "SSH" -DisplayName "SSH" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 2222
    ```
    @@ -35,6 +38,7 @@
    3. [Optional] Add ssh config
    1. Edit your ssh config file: `vim ~/.ssh/config`
    2. Add the following config (make sure to replace placeholder values):
    ```
    Host wsl_hostname_placeholder
    Hostname wsl_hostname_placeholder
  6. @ndraiman ndraiman revised this gist Apr 27, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,8 @@
    [wsl2]
    networkingMode=mirrored
    ```
    4. Expose port `2222` in the wsl2 firewall:
    ```sh
    4. Expose port `2222` in the wsl2 firewall in **PRIVILEGED PowerShell terminal**:
    ```powershell
    New-NetFirewallHyperVRule -Name "SSH" -DisplayName "SSH" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 2222
    ```
    5. Restart wsl by shutting it down (`wsl --shutdown`) and then starting it again (`wsl`)
  7. @ndraiman ndraiman revised this gist Apr 27, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@
    3. `PubkeyAuthentication yes` - if you want to use key-based authentication (recommended)
    4. `Port 2222` - Windows ssh server already uses port 22, so we need to pick a different one.
    5. `ListenAddress 0.0.0.0`
    3. Set mirrored networking mode in `.wslconfig`
    3. Set [mirrored networking mode](https://learn.microsoft.com/en-us/windows/wsl/networking#mirrored-mode-networking) in `.wslconfig`
    ```
    [wsl2]
    networkingMode=mirrored
  8. @ndraiman ndraiman revised this gist Apr 27, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Guide
    # Guide: How to SSH Into WSL2

    ## Install WSL2 SSH server

  9. @ndraiman ndraiman created this gist Apr 27, 2024.
    48 changes: 48 additions & 0 deletions ssh_into_wsl2.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    # Guide

    ## Install WSL2 SSH server

    1. Open your wsl ubuntu terminal
    2. install open-ssh server
    ```sh
    sudo apt update
    sudo apt install openssh-server
    ```
    2. Configure the ssh server: `sudo vim /etc/ssh/sshd_config`
    1. `PermitRootLogin no` - disallows root login for security
    2. `PasswordAuthentication yes` - if you want to allow password login
    3. `PubkeyAuthentication yes` - if you want to use key-based authentication (recommended)
    4. `Port 2222` - Windows ssh server already uses port 22, so we need to pick a different one.
    5. `ListenAddress 0.0.0.0`
    3. Set mirrored networking mode in `.wslconfig`
    ```
    [wsl2]
    networkingMode=mirrored
    ```
    4. Expose port `2222` in the wsl2 firewall:
    ```sh
    New-NetFirewallHyperVRule -Name "SSH" -DisplayName "SSH" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 2222
    ```
    5. Restart wsl by shutting it down (`wsl --shutdown`) and then starting it again (`wsl`)
    ## Using SSH Keys to login
    1. Generate new ssh keys on your remote machine by following [this guide from GitHub](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)
    2. Add your keys to your wsl's user `~/.ssh/authorized_keys`
    1. Copy the contents of the public key you created in step 1.
    2. Run: `public_key_placeholder >> ~/.ssh/authorized_keys` - make sure to replace `public_key_placeholder` with the actual contents of the public key
    3. [Optional] Add ssh config
    1. Edit your ssh config file: `vim ~/.ssh/config`
    2. Add the following config (make sure to replace placeholder values):
    ```
    Host wsl_hostname_placeholder
    Hostname wsl_hostname_placeholder
    Port 2222
    User wsl_user_placheolder
    UseKeychain yes
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_rsa
    ```
    4. Now you can ssh to your wsl2 instance: `ssh wsl_hostname_placeholder`
    1. If you didn't add a ssh config, then you need specify those options: `ssh wsl_user_placheolder@wsl_hostname_placeholder -p 2222`