Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to SSH into Bash and WSL2 on Windows From an External Machine

August 9, 2024
in A, Blog, WSL
0
ADVERTISEMENT

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
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

You have Exceeded the Maximum Number of Allowable Transactions Microsoft 365

Next Post

How to Install CentOS 9 Stream in WSL in Windows

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory