Establishing SSH Connections from WSL to Ubuntu Desktop: A Step-by-Step Guide
A detailed guide on setting up a secure SSH connection from Windows Subsystem for Linux (WSL) to an Ubuntu desktop, covering installation, SSH key setup, firewall configuration, and troubleshooting.
This guide provides a step-by-step walkthrough to establish a secure SSH connection from a Windows Subsystem for Linux (WSL) environment to an Ubuntu desktop. By the end of this tutorial, you’ll be able to connect seamlessly using SSH keys for secure, password-less login.
✅ Prerequisites
Before starting, ensure the following:
- Your Ubuntu desktop and Windows device (with WSL installed) are connected to the same local network.
- You have administrative access on both systems.
- You have WSL properly installed on your Windows machine (preferably WSL 2).
🔧 Step 1: Install OpenSSH on Both Systems
📍 On Ubuntu Desktop
First, ensure the SSH server is installed and running:
- Update your package list and install OpenSSH Server:
1 2
sudo apt update sudo apt install openssh-server
- Enable and start the SSH service:
1 2
sudo systemctl enable ssh sudo systemctl start ssh
- Verify the SSH service status:
1
sudo systemctl status ssh
The output should include:
1
Active: active (running)
📍 On WSL (Laptop/Windows)
- Verify if SSH client is installed (it usually comes pre-installed):
1
ssh -V
- Install OpenSSH client if not available:
1 2
sudo apt update sudo apt install openssh-client
✅ Step 2: Find the IP Address of Your Ubuntu Desktop
To connect via SSH, you need your desktop’s IP address. On the Ubuntu desktop, run:
1
ip addr show
Look for the IP address under your network adapter (likely eth0 or wlan0). Example output:
1
inet 192.168.1.100/24
Take note of the IP address (e.g., 192.168.1.100), as you will need it later.
✅ Step 3: Set Up SSH Keys for Secure Login
🔐 Generate SSH Key Pair on WSL
On your WSL terminal, generate a new RSA SSH key pair:
1
ssh-keygen -t rsa -b 4096 -f ~/.ssh/custom_ssh_key
where
- t rsa: Specifies the RSA algorithm.
- b 4096: Generates a 4096-bit key for strong encryption.
- f ~/.ssh/custom_ssh_key: Saves the key pair with a custom filename.
Press Enter for default prompts. You can also add a passphrase for extra security.
📋 Copy the Public Key to the Ubuntu Desktop
- Use ssh-copy-id to transfer the public key to the desktop:
1
ssh-copy-id -i ~/.ssh/custom_ssh_key.pub username@desktop_ip
Replace:
- username with your Ubuntu desktop’s username.
- desktop_ip with your desktop’s IP address (e.g., 192.168.1.100).
- Manually copy the key if ssh-copy-id isn’t available:
- On WSL, display the public key:
1
cat ~/.ssh/custom_ssh_key.pub
- On the Ubuntu desktop, paste the key into ~/.ssh/authorized_keys:
1 2 3
echo "paste_the_copied_key_here" >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys chmod 700 ~/.ssh
✅ Step 4: Allow SSH Through the Firewall (on Ubuntu Desktop)
- Enable the UFW firewall and allow SSH:
1
2
sudo ufw allow ssh
sudo ufw enable
- Verify firewall status:
1
sudo ufw status
The output should show:
1
OpenSSH ALLOW Anywhere
✅ Step 5: Configure SSH on WSL for Easy Access
- Edit the SSH configuration file on WSL:
1
nano ~/.ssh/config
- Add a configuration block for your Ubuntu desktop:
1 2 3 4
Host ubuntu-desktop HostName 192.168.1.100 User username IdentityFile ~/.ssh/custom_ssh_key
Replace: - 192.168.1.100 with your desktop’s IP address. - username with your desktop username.
- Save and exit (CTRL + X, then Y and Enter).
✅ Step 6: Establish the SSH Connection
Now, you can easily connect from WSL to your Ubuntu desktop:
1
ssh ubuntu-desktop
You should now access your desktop’s terminal directly from your WSL environment without needing to enter a password.
✅ Step 7: Troubleshooting Common Issues
If the connection fails, try the following checks:
🔎 1. Check SSH Service Status
On the Ubuntu desktop, verify that SSH is running:
1
sudo systemctl status ssh
🔎 2. Test Network Connectivity
From WSL, ping your desktop’s IP:
1
ping 192.168.1.100
🔎 3. Verify SSH Port Accessibility
Check if port 22 is open:
1
nc -zv 192.168.1.100 22
You should see:
1
Connection to 192.168.1.100 port 22 [tcp/ssh] succeeded!
🔎 4. Check Firewall Settings
Ensure that the Ubuntu firewall isn’t blocking SSH:
1
sudo ufw status verbose
Allow SSH through the firewall if necessary:
1
2
sudo ufw allow 22
sudo ufw reload
🔎 5. Inspect SSH Logs
On the Ubuntu desktop, review SSH logs for errors:
1
sudo journalctl -u ssh
✅ Step 8: Secure Your SSH Server (Optional)
For added security, consider editing the SSH configuration file on your Ubuntu desktop:
- Open the SSH daemon configuration file:
1
sudo nano /etc/ssh/sshd_config
- Change or add the following settings:
1 2 3
PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes
- After saving, restart the SSH service:
1
sudo systemctl restart ssh
🎯 Conclusion
After following these steps, you should have a fully functional, secure SSH connection from your WSL terminal to your Ubuntu desktop. This setup allows for secure file transfers, remote execution of commands, and seamless integration between your Windows and Linux environments.