Introduction
In this article, we will discuss how to set up an SSH server on Ubuntu running on WSL2 (Windows Subsystem for Linux 2) and troubleshoot issues when it's not working. We will cover the key concepts related to SSH, WSL2, and port forwarding. By the end of this article, you will have a good understanding of how to configure and troubleshoot SSH on Ubuntu running on WSL2.
What is SSH?
SSH (Secure Shell) is a protocol used for secure remote access to a computer over a network. It allows users to connect to a remote server, execute commands, and transfer files securely. SSH uses encryption to protect the data transmitted between the client and the server, making it a secure alternative to other remote access protocols.
Setting up SSH Server on Ubuntu WSL2
To set up an SSH server on Ubuntu running on WSL2, follow these steps:
- Update the package list:
sudo apt update- Install the OpenSSH server:
sudo apt install openssh-server- Check the status of the SSH server:
sudo systemctl status sshIf the SSH server is not running, start it using the following command:
sudo systemctl start sshTo ensure that the SSH server starts automatically when the system boots up, use the following command:
sudo systemctl enable sshConfiguring Port Forwarding
To configure port forwarding on Windows host for the Ubuntu WSL2 SSH server, follow these steps:
- Open the Windows Terminal as an administrator.
- Run the following command to open the Windows host's firewall settings:
wsl --shutdown- Run the following command to open the Windows Defender Firewall with Advanced Security:
wf.msc- In the Windows Defender Firewall with Advanced Security window, click on "Inbound Rules" and then click on "New Rule".
- Select "Port" and click "Next".
- Select "TCP" and enter the port number (e.g., 25252) and click "Next".
- Select "Allow the connection" and click "Next".
- Select "Domain", "Private", and "Public" and click "Next".
- Enter a name and description for the rule and click "Finish".
Troubleshooting SSH Server on Ubuntu WSL2
If you are unable to connect to the SSH server on Ubuntu WSL2, here are some troubleshooting steps:
- Check the status of the SSH server on Ubuntu WSL2 using the
sudo systemctl status sshcommand.
- Check if the SSH server is listening on the correct port using the
sudo netstat -tuln | grep sshcommand.
- Check if the port forwarding is configured correctly on the Windows host.
- Check if the Windows host's firewall is blocking the incoming SSH connection.
Setting up an SSH server on Ubuntu WSL2 and configuring port forwarding on the Windows host can be a challenging task. However, by following the steps outlined in this article, you should be able to set up and troubleshoot the SSH server on Ubuntu WSL2. It is important to ensure that the SSH server is running and listening on the correct port, and that the port forwarding and firewall settings are configured correctly on the Windows host.
References
- Ubuntu WSL2 documentation: https://ubuntu.com/wsl
- OpenSSH server documentation: https://www.openssh.com/
- Windows Defender Firewall with Advanced Security documentation: https://docs.microsoft.com/en-us/windows/security/threat-protection/windows-firewall/windows-firewall-with-advanced-security