Skip to content

SSH Server Setup in WSL2

Install

sudo apt update && sudo apt install -y openssh-server

Configure /etc/ssh/sshd_config

Key directives to set/uncomment:

Port 22
ListenAddress 0.0.0.0
PermitRootLogin no
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM yes

Port conflict note: If Windows OpenSSH Server is installed and bound to port 22, use a different port (e.g., 2222). Check from PowerShell:

Get-Service sshd   # if this returns "Running", Windows OpenSSH is using port 22

To disable Windows OpenSSH (recommended -- you want WSL2's sshd, not Windows'):

Stop-Service sshd
Set-Service -Name sshd -StartupType Disabled

Generate Host Keys (if missing)

sudo ssh-keygen -A

Start and Enable

With systemd enabled ([boot] systemd=true in /etc/wsl.conf):

sudo systemctl enable ssh
sudo systemctl start ssh

Without systemd:

sudo service ssh start

Verify

ss -tlnp | grep :22
# Should show: LISTEN  0  128  0.0.0.0:22  *  users:(("sshd",...))

Auto-Start on WSL2 Boot

If using systemd, systemctl enable ssh handles this.

If not using systemd, add to /etc/wsl.conf:

[boot]
command = service ssh start

Then wsl --shutdown from PowerShell and restart WSL2.