How to Configure Remote SSH Connection to WSL 2
There may be a case where you want to use WSL(Windows Subsystem for Linux) via a remote SSH connection.
Configuring a remote SSH connection to WSL is not necessarily straightforward because:
- You need to configure port forwarding from WSL to the host machine
- WSL does not have
systemd
, so you need to configure automatic booting of WSL and then SSH server inside it(because you will want to automate everything).
Well, let’s take a look at the configuration process step by step.
Install SSH server on the WSL instance.
|
|
Configure the SSH server to accept SSH connetions on port 2222(or any other port you prefer).
|
|
Reboot the SSH server.
|
|
Allow your default WSL user to start the SSH server without entering a password.
|
|
Configure firewall on the host machine(Windows 10 or 11) to allow incoming remote SSH connections.
Open Command Prompt as an administrator and run the following commands:
|
|
Create a batch script that automatically starts WSL and SSH server on host startup.
|
|
On the host OS(Windows), open start menu and search for “Task Scheduler”, where you can create a new scheduled task that runs the above batch script. Make sure that you configure the task run whether a user is logged in or not.
That’s pretty much. Please comment below if you got stuck in the process.
Happy coding!