Tuesday, August 11, 2020

Enabling SSH Access to WSL2 from DIfferent Computer

WSL2 is such a blessing to Windows. As much as I really love Linux, in some laptops, Linux simply aren't supported well. It's no Linux fault, though... Many hardware vendors simply don't provide Linux drivers for their hardware, making the experience on Linux being sub-par when compared to its Windows counterpart. As an example, I own an ROG ASUS Laptop, GL503 that I use for entertainment purposes. Linux didn't work properly out of the box on this laptop whose 120hz screen requires it to run on NVIDIA GPU all the time. Luckily, WSL2 comes to the rescue! From being an entertainment-only machine, I can finally use this laptop to do some development work! What a convenient!

Okay, that's enough chit-chatting.. This time, for a development purposes, I need to SSH from a different machine, into the WSL2 instance running on my Windows machine. This turns out to be a bit tricky. So here's how you do it:

1. Instal openssh server on WSL2: `sudo apt install openssh-server`

2. Modify openssh server configuration

```

# 1. Update port from 22 to something else, i.e. 8828. The reason is because port 22 already reserved by Windows
# 2. Uncomment and change ListenAddress to 0.0.0.0
# 3. Uncomment and change PasswordAuthentication to yes
sudo vim /etc/ssh/sshd_config

# Generate host keys
sudo ssh-keygen -A

# Restart openssh server
sudo service ssh restart

# Note WSL2 ipaddress
ifconfig

```

  • Now, try to SSH into the WSL from Windows (of the same computer), using the WSL2 ip address from before. You can do that using Putty or terminal. If it fails, use -vvv flag to see why it fails. Make sure this works before proceeding to the next step
  • Now, forward connection from Windows' ip address into WSL ip address by running command like this: `netsh interface portproxy add v4tov4 listenport=8828 listenaddress=0.0.0.0 connectport=8828 connectaddress=172.27.136.236` 
  • Then try to SSH into the WSL machine again, but this time use Windows ip address. Only proceed if this was successful
  • Now, you would think it's over right? Not quite... Last step is to change Windows firewall setting to allow SSH to port 8828 from outside [1]
    • Navigate to Control Panel, System and Security and Windows Firewall.
    • Select Advanced settings and highlight Inbound Rules in the left pane.
    • Right click Inbound Rules and select New Rule.
    • Add the port you need to open and click Next.
    • Add the protocol (TCP or UDP) and the port number into the next window and click Next.
    • Select Allow the connection in the next window and hit Next.
    • Select the network type as you see fit and click Next.
    • Name the rule something meaningful and click Finish
  • That's it!


Hope this is helpful and could save you some time!


Reference:
1. Changing firewall setting on Windows