SSH to WSL2

·

2 min read

WSL2 (Windows Subsytem for Linux) allows us work in Linux environment.
And to access to WSL via SSH, we can follow these following steps:
Step 1 → 3: Configure in Server
Step 4: Connect in Client

  1. Install OpenSSH Server in WSL2
    Open WSL2 terminal and run:

     sudo apt install openssh-server
    
  2. Forward port for SSH
    Retrieve IP address of WLS2 in terminal via ip addr or ifconfig
    Ex: eth0 inet → 172.20.197.6

     ifconfig
      --------------------------------------------------------------------
      eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
              inet 172.20.197.6  netmask 255.255.240.0  broadcast 172.20.207.255
              inet6 fe80::215:5dff:fe17:a1ed  prefixlen 64  scopeid 0x20<link>
              ether 00:15:5d:17:a1:ed  txqueuelen 1000  (Ethernet)
              RX packets 6094  bytes 586476 (586.4 KB)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 72  bytes 5136 (5.1 KB)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
      lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
              inet 127.0.0.1  netmask 255.0.0.0
              inet6 ::1  prefixlen 128  scopeid 0x10<host>
              loop  txqueuelen 1000  (Local Loopback)
              RX packets 0  bytes 0 (0.0 B)
              RX errors 0  dropped 0  overruns 0  frame 0
              TX packets 0  bytes 0 (0.0 B)
              TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    

    Forward SSH port via netsh in Windows Command Line (port number 22)

    • listenport=22 (SSH port forwarded)

    • listenaddress=0.0.0.0 (All IP addresses)

    • connectport=22 (SSH port in WSL2)

    • connectaddress=172.20.197.6 (IP address of WSL2 above from eth0 inet)

    C:\Windows\System32>netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=172.20.197.6
  1. Start SSH service

     sudo service ssh start
    
  2. Connect from Client (another PC Command Line)
    ssh user_name@windows_ip_address

    • windows_ip_address: IP address of Windows host machine that contains WSL2, different from eth0.inet above
    C:\Users\littlenotes> ssh user001@10.17.35.182
    user001@10.17.35.182's password: **************

    Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.153.1-microsoft-standard-WSL2 x86_64)
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings
    Last login: Mon Aug 12 09:06:34 2024 from 172.20.197.1
    0|~>