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.
- Open your WSL distribution.
- Update your package list:
sudo apt update
- Install the OpenSSH server:
sudo apt install openssh-server
- Edit the SSH configuration file:
sudo nano /etc/ssh/sshd_config
- Ensure the following lines are present and uncommented:
Port 2222 PasswordAuthentication no PubkeyAuthentication yes
- Save and exit the file (Ctrl+X, then Y, then Enter).
- Start the SSH service:
sudo service ssh start
- Open PowerShell as Administrator on Windows.
- Get your WSL IP address:
Note down this IP address.
wsl hostname -I
- Set up port forwarding:
Replace
netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=<WSL_IP_ADDRESS>
<WSL_IP_ADDRESS>
with the IP address you noted earlier.
- Open Windows Defender Firewall with Advanced Security.
- Click on "Inbound Rules" and then "New Rule".
- Choose "Port" and click Next.
- Select "TCP" and enter "2222" for the port number.
- Allow the connection and apply the rule to all profiles.
- Name the rule (e.g., "WSL SSH") and finish the wizard.
- Open Terminal on your Mac.
- Generate a new SSH key:
ssh-keygen -t rsa -b 4096
- Follow the prompts, using the default file location and adding a passphrase if desired.
- Display your public key:
cat ~/.ssh/id_rsa.pub
- Copy the output.
- In your WSL terminal:
mkdir -p ~/.ssh nano ~/.ssh/authorized_keys
- Paste your public key into this file, save, and exit.
- Set correct permissions:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
- Edit your SSH config file:
nano ~/.ssh/config
- Add the following:
Replace
Host wsl HostName <WINDOWS_IP> User <WSL_USERNAME> Port 2222 IdentityFile ~/.ssh/id_rsa
<WINDOWS_IP>
with your Windows machine's IP address and<WSL_USERNAME>
with your WSL username.
- In your Mac's terminal, connect using:
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
- Solution: In WSL, run
- Port forwarding is not set up correctly.
- Solution: Check with
netsh interface portproxy show v4tov4
- If empty, set up port forwarding as described in Step 2
- Solution: Check with
- 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: Verify the content of
~/.ssh/authorized_keys
in WSL
- Solution: Verify the content of
- Incorrect permissions on SSH files.
- Solution: In WSL, run:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
- Solution: In WSL, run:
- Incorrect filename for authorized keys.
- Solution: Ensure the file is named
authorized_keys
, notauthorized_users
or anything else
- Solution: Ensure the file is named
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
- Solution: Check SSH config in WSL (
- 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"
- Solution: Remove the old key from your Mac's
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:
- In WSL, get the new IP:
ip addr show eth0
- In Windows PowerShell (as admin), update the rule:
netsh 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>
- In WSL, get the new IP:
- Solution: Update the port forwarding rule with the new 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:
- In Windows PowerShell (as admin):
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\wsl.exe" -PropertyType String -Force
- Restart the SSH service:
Restart-Service sshd
- In Windows PowerShell (as admin):
- Solution: Set the default shell for SSH connections:
Remember, when troubleshooting SSH connections, the verbose mode (ssh -v wsl
) can provide helpful diagnostic information. Don't hesitate to use it when you encounter issues.
By being aware of these common issues and their solutions, you'll be better prepared to troubleshoot any problems that arise when setting up SSH access to your WSL environment from your Mac.
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.