Table of Contents
In this post, we will demonstrate how to SSH into Bash and WSL2 on Windows from an external machine.
Install OpenSSH server on Linux distro
1️⃣ Update the system then install OpenSSH server on the Linux instance on WSL.
sudo apt update -y
sudo apt install openssh-server -y
2️⃣ Once done, open the configuration file then change some settings.
sudo nano /etc/ssh/sshd_config
# edit /etc/ssh/sshd_config with the following changes
Port 22
ListenAddress 0.0.0.0
PasswordAuthentication yes
Or you can run the below commands in bash to make the changes into the config file automatically.
sudo sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g' /etc/ssh/sshd_config
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config
3️⃣ Restart sshd service and make the service starts on boot automatically.
sudo systemctl restart ssh && sudo systemctl enable ssh && sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-06-12 08:33:19 +07; 7s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 967 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 968 (sshd)
Tasks: 1 (limit: 9526)
Memory: 1.7M
CGroup: /system.slice/ssh.service
└─968 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
Jun 12 08:33:19 DESKTOP-EBL2UQ1 systemd[1]: Starting OpenBSD Secure Shell server...
Jun 12 08:33:19 DESKTOP-EBL2UQ1 sshd[968]: Server listening on 0.0.0.0 port 22.
Jun 12 08:33:19 DESKTOP-EBL2UQ1 systemd[1]: Started OpenBSD Secure Shell server.
Forward port from Windows into Linux WSL distro
The portproxy server listens for messages sent to a specific port and IPv4 address and maps a port and IPv4 address to send the messages received after establishing a separate TCP connection.
1️⃣ Run the following command in a PowerShell window ( do not run inside the bash in Linux)
netsh interface portproxy add v4tov4 `
listenport=22 `
listenaddress=0.0.0.0 `
connectport=22 `
connectaddress=172.31.138.23
- listenaddress: The IPv4 address for which to listen (“0.0.0.0” any IPv4 address).
- listenport: The IPv4 port on which to listen (“22”, you can choose any port you want).
- connectaddress: The IPv4 address to which to connect (“172.31.138.23”, this should be the address of your Linux WSL distro).
- connectport: The IPv4 port to which to connect (“22”, must match the port your set for your sshd on your Linux WSL distro).
2️⃣ Check the Port Forwarding:
PS C:\Users\admin> netsh interface portproxy show all
Listen on ipv4: Connect to ipv4:
Address Port Address Port
--------------- ---------- --------------- ----------
0.0.0.0 22 172.31.138.23 22
3️⃣ Add a new firewall rule to enable the port on the host machine:
netsh advfirewall firewall add rule `
name="Open Port 22 - WSL 2" dir=in action=allow protocol=TCP localport=22
Now, you can test the connection to the WSL instance using SSH.
ssh bonben@{ip_of_the_host_machine}
![7tPrMFoCyRK9AJyylUuRODKl6YatUDvfIFif1HYgJhc6lNT72B0CoIdXq3g2](https://s3.amazonaws.com/s3.bonben365.com/files/2023/7tPrMFoCyRK9AJyylUuRODKl6YatUDvfIFif1HYgJhc6lNT72B0CoIdXq3g2.jpg)
Not a reader? Watch this related video tutorial: