-
Open your wsl ubuntu terminal
-
install open-ssh server
sudo apt update sudo apt install openssh-server
-
Configure the ssh server:
sudo vim /etc/ssh/sshd_configPermitRootLogin no- disallows root login for securityPasswordAuthentication yes- if you want to allow password loginPubkeyAuthentication yes- if you want to use key-based authentication (recommended)Port 2222- Windows ssh server already uses port 22, so we need to pick a different one.ListenAddress 0.0.0.0
-
Activate and start the ssh server:
sudo systemctl start ssh sudo systemctl enable ssh -
Set mirrored networking mode in
.wslconfig[wsl2] networkingMode=mirrored -
Expose port
2222in the wsl2 firewall in PRIVILEGED PowerShell terminal:New-NetFirewallHyperVRule -Name "SSH" -DisplayName "SSH" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 2222
-
Restart wsl by shutting it down (
wsl --shutdown) and then starting it again (wsl)
-
Generate new ssh keys on your remote machine by following this guide from GitHub
-
Add your keys to your wsl's user
~/.ssh/authorized_keys- Copy the contents of the public key you created in step 1.
- Run:
public_key_placeholder >> ~/.ssh/authorized_keys- make sure to replacepublic_key_placeholderwith the actual contents of the public key
-
[Optional] Add ssh config
- Edit your ssh config file:
vim ~/.ssh/config - Add the following config (make sure to replace placeholder values):
Host wsl_hostname_placeholder Hostname wsl_hostname_placeholder Port 2222 User wsl_user_placheolder UseKeychain yes AddKeysToAgent yes IdentityFile ~/.ssh/id_rsa - Edit your ssh config file:
-
Now you can ssh to your wsl2 instance:
ssh wsl_hostname_placeholder- If you didn't add a ssh config, then you need specify those options:
ssh wsl_user_placheolder@wsl_hostname_placeholder -p 2222
- If you didn't add a ssh config, then you need specify those options: