This guide provides step-by-step instructions to set up an SSH server on Windows Subsystem for Linux (WSL) using a Linux distribution like Ubuntu.
Open your WSL terminal and ensure your system is up to date:
sudo apt update && sudo apt upgrade -y
Install the OpenSSH server package:
sudo apt install openssh-server -y
Edit the SSH server configuration file to ensure it works correctly in WSL:
sudo nano /etc/ssh/sshd_config
- Ensure or add the following lines (adjust as needed):
Port 22 ListenAddress 0.0.0.0 PasswordAuthentication yes
- Save and exit (
Ctrl+O
,Enter
,Ctrl+X
).
Start the SSH service and enable it to run on boot:
sudo service ssh start
sudo systemctl enable ssh
Note: WSL doesn't use systemd
by default in some versions, so enabling may not persist across WSL restarts. You may need to start the service manually each time WSL restarts.
Verify the SSH server is running:
sudo service ssh status
You should see output indicating the service is active.
WSL assigns a dynamic IP address. Find it with:
ip addr show eth0 | grep inet
Look for the inet
line (e.g., inet 172.18.x.x
). Note this IP for connecting.
On your Windows host, open PowerShell as Administrator and allow port 22:
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
From another machine (or the Windows host), test the connection:
ssh <username>@<wsl-ip-address>
Replace <username>
with your WSL Linux user and <wsl-ip-address>
with the IP from step 6. If prompted, accept the host key and enter your password.
For secure access without passwords:
- Generate an SSH key pair on your client machine:
ssh-keygen -t rsa -b 4096
Категория: Copy the public key to the WSL server:
ssh-copy-id <username>@<wsl-ip-address>
- Disable password authentication in
/etc/ssh/sshd_config
:PasswordAuthentication no
- Restart the SSH service:
sudo service ssh restart
WSL doesn't persist services like a regular Linux system. To auto-start the SSH server:
- Create a script in WSL:
echo "sudo service ssh start" > ~/.start-ssh.sh chmod +x ~/.start-ssh.sh
- Configure WSL to run it on startup by adding to your Windows user's shell (e.g., in
.bashrc
or via Windows Task Scheduler to runwsl -e ~/.start-ssh.sh
).
- WSL IP addresses change on reboot. Use
wsl --distribution <distro> --exec ip addr
or a script to fetch the IP dynamically. - If you encounter connection issues, ensure the SSH port (22) isn't blocked by your network or firewall.
- For remote access, configure your router for port forwarding if accessing outside your local network.