Skip to content

Instantly share code, notes, and snippets.

@decagondev
Created July 2, 2025 23:05
Show Gist options
  • Save decagondev/f5878283b16f1be9ff34892111b9ae8d to your computer and use it in GitHub Desktop.
Save decagondev/f5878283b16f1be9ff34892111b9ae8d to your computer and use it in GitHub Desktop.

Setting Up an SSH Server on WSL

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.

1. Update WSL Linux Distribution

Open your WSL terminal and ensure your system is up to date:

sudo apt update && sudo apt upgrade -y

2. Install OpenSSH Server

Install the OpenSSH server package:

sudo apt install openssh-server -y

3. Configure SSH Server

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).

4. Start SSH Server

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.

5. Check SSH Service Status

Verify the SSH server is running:

sudo service ssh status

You should see output indicating the service is active.

6. Find WSL IP Address

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.

7. Allow SSH Through Windows Firewall

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

8. Test SSH Connection

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.

9. (Optional) Set Up Key-Based Authentication

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

10. (Optional) Persist SSH Server Across WSL Restarts

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 run wsl -e ~/.start-ssh.sh).

Notes

  • 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment