ssh into wsl2
I recently spilled water on my 14 year old Dell Inspiron laptop, which I run Ubuntu on and perform coding on. I also have a Chromebook laptop that does not have a lot of hard drive space or horsepower. I also have a newer laptop, but wife is often using it. So, I wanted to ssh into the newer laptop from the Chromebook. I previously set up wsl2 on the new laptop. It allows me to run Ubuntu from wsl2. I also have docker desktop installed on the new laptop, allowing me to kick off docker container applications. If you google ssh into wsl2 or use the link below, you'll find instructions to ssh into wsl2:
https://share.google/aimode/IRaXwFbkogMS3BjfP
I've also included the instructions from google (see below) for your convenience. It was pretty simple to set up and works great.
Steps to set up SSH in WSL2
Update and install OpenSSH server in WSL2:
Open your WSL terminal.
Run the following commands:
sudo apt update sudo apt install openssh-server
Configure the SSH server in WSL2:
Edit the SSH config file:
sudo nano /etc/ssh/sshd_config
Change the default Port to something else (e.g., 2222) to avoid conflicts with any potential Windows SSH server on port 22.
Ensure ListenAddress 0.0.0.0 is uncommented or added to listen on all available network interfaces.
(Optional but recommended) For password authentication, ensure PasswordAuthentication yes is set. For key-based authentication, ensure PubkeyAuthentication yes is set.
Save the changes and exit the editor.
Start and enable the SSH service in WSL2:
Start the SSH service:
sudo systemctl start ssh
Enable the service to start automatically when WSL launches (requires a recent WSL version with systemd support):
sudo systemctl enable ssh
Verify the status:
sudo systemctl status ssh
Connect from Windows (local access):
From a Windows Command Prompt or PowerShell, you can typically connect to the WSL instance using localhost or its internal IP address.
First, find the WSL internal IP address in the WSL terminal using ip addr | grep eth0. Then, from Windows, connect using: ssh <wsl_username>@<WSL2_IP_address> -p 2222
Configure Port Forwarding and Firewall (external/LAN access):
Find your Windows IP address: Open PowerShell on Windows and run ipconfig to find the IPv4 Address of your main network adapter.
Add a port proxy rule: Open a PowerShell or Command Prompt as an Administrator and run the following command, replacing <Windows_IP_address>, <WSL2_IP_address>, and <Port_number> (e.g., 2222) with your specific details:
netsh interface portproxy add v4tov4 listenport=2222 listenaddress=<Windows_IP_address> connectport=2222 connectaddress=<WSL2_IP_address>
Add a Windows Firewall rule: From the same Administrator prompt, run:
netsh advfirewall firewall add rule name="Open Port 2222 for WSL2" dir=in action=allow protocol=TCP localport=2222
You can now connect from an external machine using ssh <wsl_username>@<Windows_IP_address> -p 2222.
Comments