Enable SSH Access to WSL from Anywhere and on Any Device
Setup SSH Server on WSL (Ubuntu) on Windows 11 and Enable SSH Access to WSL from Anywhere and on Any Device Using Tailscale
It has been a prolonged quest to discover a permanent and refined solution for enabling SSH access to the Windows Subsystem for Linux (WSL) from a remote computer. My pursuit has been well-documented here.
- I initially believed that installing the OpenSSH Server on Windows 11 would allow me to access WSL, as outlined in the tutorial “Setup SSH Server on Windows 11”. Unfortunately, it did not function properly as “Launching Windows Subsystem for Linux from session zero does not currently work (for example from an ssh connection)”
- I had to resort to an inelegant solution, which involved creating a network port proxy rule on a Windows computer that maps port 22 to the WSL distribution (e.g., Ubuntu 22.04) session, as detailed in the tutorial “Enable SSH Access to WSL from a Remote Computer”.
I even created a conditional statement to determine which tutorial to follow:
If the command prompt returns an error when running “wsl -version”, follow the tutorial for “Setup SSH Server on Windows 11.” Else, follow the tutorial for “Enable SSH Access to WSL from a Remote Computer.”
I suggest using a complex password for your WSL distro before proceeding with the tutorial, rather than a simple one.
I believe I have finally discovered a permanent and refined solution. The added benefit is that I am now able to access WSL sessions from any location and device, using the third-party service Tailscale (https://tailscale.com/).
The tutorial is divided into the following sections:
- Updating WSL to the latest version
- Enabling systemd
- Installing OpenSSH Server
- Validating OpenSSH Service (i.e., sshd)
- Tailscale Overview
- Signing up for Tailscale account for free
- Installing Tailscale
- Validating Tailscale SSH Connection
Tutorial
Updating WSL to the latest version
To update to the latest version of WSL, issue the following command in either PowerShell or the Command Prompt:
wsl --update
Once the update is complete, validate it by checking the version of WSL using the following steps:
wsl --version
which should return information similar to the following:
Enabling systemd
The latest version of WSL supports “systemd” and we should utilize it fully. From the WSL distribution terminal (e.g., Ubuntu 22.04), edit the /etc/wsl.conf file.
sudo nano /etc/wsl.conf
then, add the following:
[boot]
systemd=true
Exit out of the nano editor by using ‘CTRL+O’ to save and ‘CTRL+X’ to exit. Close the WSL distribution terminal (e.g., Ubuntu 22.04). From either PowerShell or the Command Prompt, issue a command to shut down all instances of running WSL distributions:
wsl --shutdown
Installing OpenSSH Server
To install the OpenSSH Server, issue the following commands from a new WSL distribution terminal (e.g., Ubuntu 22.04):
sudo apt update
sudo apt install openssh-server
To confirm the installation, check the status of SSH by issuing the following command:
sudo service ssh status
which should return the following information:
If the SSH service is not running, you can start it by issuing the following command through systemd:
sudo systemctl start sshd.service
Validating OpenSSH Service (i.e., sshd)
Let’s verify if the sshd is functioning as intended by connecting to the localhost.
ssh sungkim@localhost
Enter your password when prompted, this will connect you to the same WSL session via SSH. Type “exit” to end the SSH session. You should also verify that the sshd is working properly from a host computer (e.g., Windows computer). First, obtain the IP address of the WSL distribution session (e.g., Ubuntu 22.04) by issuing the following command (you may need to install net-tools):
ifconfig
which should return the IP address of the WSL distribution session (e.g., Ubuntu 22.04).
From either PowerShell or the Command Prompt, let’s connect to the WSL distribution session (e.g., Ubuntu 22.04) using its IP address via SSH:
ssh sungkim@172.18.181.124
Enter your password when prompted, and you will be connected to the WSL distribution session (e.g., Ubuntu 22.04), and it will appear as if you are using the WSL distribution session (e.g., Ubuntu 22.04) directly.
You have successfully installed the OpenSSH Server on the WSL distribution session (e.g., Ubuntu 22.04)!
Tailscale Overview
Tailscale is a peer-to-peer mesh network VPN service based on the WireGuard open-source project.
The Tailscale approach avoids centralization where possible, resulting in both higher throughput and lower latency as network traffic can flow directly between machines. Additionally, decentralization improves stability and reliability by reducing single points of failure.
Tailscale is simple and effortless. The service handles complex network configuration on your behalf so that you don’t have to. Network connections between devices pierce through firewalls and routers as if they weren’t there, allowing for direct connections without the need to manually configure port forwarding. It allows for connection migration so that existing connections stay alive even when switching between different networks (e.g., wired, cellular, Wi-Fi, etc) per Tailscale website (What is Tailscale? · Tailscale).
Signing up for Tailscale account for free
Go to Tailscale website Tailscale · Best VPN Service for Secure Networks and sign up for the free service.
Installing Tailscale
Install Tailscale in your WSL distribution from Tailscale website: Download · Tailscale
curl -fsSL https://tailscale.com/install.sh | sh
After completing the installation, follow the instructions to run “sudo tailscale up” and then follow the instruction to authorize your machine. Your machine should now appear in the Tailscale admin console as shown below:
It is necessary to add at least two machines since this operates on a peer-to-peer mesh network. If your machine is not part of the mesh network, it will not be able to connect to other machines in the mesh network.
Validating Tailscale SSH Connection
From a remote machine that is part of the mesh network, use SSH to connect to the WSL distribution that is also part of the mesh network using its IP address provided by Tailscale.
ssh sungkim@100.119.250.53
If you have successfully SSHed into a Windows computer with a WSL distribution session, you will be presented with an Ubuntu interface.
You have successfully SSHed into WSL distribution session from a remote machine!
Tailscale is supported on all major platforms, so if you want you can use Apple iPad to code (Coding on iPad using VSCode, Caddy, and code-server · Tailscale):
- Windows
- macOS
- iOS
- Android
- Linux
I hope you have enjoyed this article. If you have any questions or comments, please provide them here.
Resources
The following resources were used in this article: