Windows Subsystem for Linux (WSL) is a powerful feature that allows you to run a Linux environment directly on Windows. This guide will walk you through the process of setting up SSH access to your WSL environment from a Mac.
sudo apt update
sudo apt install openssh-server
sudo nano /etc/ssh/sshd_config
Port 2222
PasswordAuthentication no
PubkeyAuthentication yes
sudo service ssh start
netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=$((wsl hostname -I).trim())
ssh-keygen -t rsa -b 4096
cat ~/.ssh/id_rsa.pub
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
nano ~/.ssh/config
Host wsl
HostName <WSL_IP>
User <WSL_USERNAME>
Port 2222
IdentityFile ~/.ssh/id_rsa
Replace <WSL_IP> with your WSL’s IP address and <WSL_USERNAME> with your WSL username.
ssh wsl
You should now be connected to your WSL environment!
Despite following the steps above, you might encounter some issues. Here are some common problems and how to resolve them:
Symptom: SSH connection attempt results in a timeout.
Possible causes and solutions:
WSL SSH service is not running.
Solution: In WSL, run sudo service ssh start
Port forwarding is not set up correctly.
Solution: Check with netsh interface portproxy show v4tov4
Windows Firewall is blocking the connection.
Solution: Verify the firewall rule for port 2222 is active
Symptom: Connection establishes, but you get a “Permission denied (publickey)” error.
Possible causes and solutions:
The public key is not properly added to authorized_keys.
Solution 1: Verify the content of ~/.ssh/authorized_keys in WSL
Solution 2: use ssh-copy-id to update ~/.ssh/authorized_keys on linux:
/etc/ssh/sshd_config, set PasswordAuthentication to yesssh-copy-id from your mac
ssh-copy-id -i ~/.ssh/id_rsa -n -p 2222 <your user name>@<your windows machine name or ip address>
Incorrect permissions on SSH files.
Solution: In WSL, run:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Incorrect filename for authorized keys.
Solution: Ensure the file is named authorized_keys, not authorized_users or anything else
Symptom: You get a “Connection refused” error.
Possible causes and solutions:
SSH server is not running on the specified port.
Solution: Check SSH config in WSL (/etc/ssh/sshd_config) to ensure it’s set to use port 2222
WSL instance is not running.
Solution: Open a WSL terminal on your Windows machine to start the instance
Symptom: You get a “Host key verification failed” error.
Possible causes and solutions:
The host key has changed (common if you’ve reinstalled WSL).
Solution: Remove the old key from your Mac’s known_hosts file:
ssh-keygen -R "[your_windows_ip]:2222"
Symptom: Connection worked before, but suddenly stops working.
Possible causes and solutions:
WSL IP address has changed after a restart.
Solution: Update the port forwarding rule with the new IP:
ip addr show eth0netsh interface portproxy delete v4tov4 listenport=2222 listenaddress=0.0.0.0
netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=<NEW_WSL_IP>
Symptom: You connect successfully but get an unexpected shell environment.
Possible causes and solutions:
Windows OpenSSH is not using the WSL shell.
Solution: Set the default shell for SSH connections:
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\wsl.exe" -PropertyType String -Force
Restart-Service sshdRemember, when troubleshooting SSH connections, the verbose mode (ssh -v wsl) can provide helpful diagnostic information.
Symptom: You get an error message like “Connection closed by [your_ip_address] port 2222”. Possible causes and solutions:
Stale SSH known_hosts entries: Your Mac might have old or incorrect SSH key information for the WSL instance.
Solution: Delete the ~/.ssh/known_hosts file or the specific outdated entry within that file on your Mac. You might also have a ~/.ssh/known_hosts.old file that could be causing issues if it was recently renamed. Try removing or renaming this file as well. The next time you connect, you will be prompted to accept the new host key.
If you have suggestions for improving this guide, please feel free to create an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.