Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Windows 10 / Configure SSH Tunnel (Port Forwarding) on Windows

March 15, 2024 Windows 10Windows 11Windows Server 2019

Configure SSH Tunnel (Port Forwarding) on Windows

You can use the built-in OpenSSH server on Windows to forward ports through an SSH tunnel (SSH tunneling). Port forwarding in SSH allows you to tunnel (forward) application ports from your local computer to a remote server and vice versa. Port forwarding over an SSH tunnel is widely used in Linux/Unix environments, and now you can take advantage of this feature in Windows as well. In this example, we will show how to tunnel an RDP connection traffic over OpenSSH on a Windows Server host.

Contents:
  • Securing RDP with the SSH Tunnel (Local TCP Forwarding)
  • How to Create SSH Tunnel on Windows with Putty?
  • Remote TCP Forwarding (Reverse SSH) to a Local Computer

What is an SSH Tunneling?

An SSH tunnel provides a secure, encrypted TCP connection between a local host and a remote SSH server. SSH Port Forwarding allows you to tunnel a connection from a local port on your local machine to any TCP port on a remote server (or vice versa) over SSH.

Port forwarding in SSH tunnels is used for:

  • Bypass firewalls;
  • Opening backdoors to private networks;
  • VPN scenarios replacements for secure remote connection;
  • Protect traffic of legacy applications (protocols) that transmit data in clear text (without encryption).
You can only forward TCP traffic/port to the SSH tunnel (UDP and ICMP protocols are not supported).

SSH tunneling is mostly used in scenarios when you need to connect to a remote computer behind the firewall. For example, you have a Windows Server with only SSH port open (TCP 22). All other ports are blocked by a hardware firewall or Windows Defender Firewall. Your task is to connect to the Windows Server using the RDP client. It would seem an impossible task because the Remote Desktop port 3389 is blocked by the firewall. However, you can access any ports on a remote host through an SSH tunnel.

Here are the typical usage scenarios of SSH tunneling:

  • Local TCP forwarding is a local port forwarding to a remote server;
  • Remote TCP forwarding is a remote port forwarding to a local computer;
  • Double SSH tunnel – allows connecting computers without allocated pubic IP addresses or behind NAT/firewall through an SSH server (if OpenVPN server solution is not applicable).

Securing RDP with the SSH Tunnel (Local TCP Forwarding)

In this mode, you create a local TCP port on your computer. All connections to this port will be forwarded to the specified port on a remote server via the SSH tunnel. In this example, we will create a local port 8888, and the connection to it will be redirected to RDP port 3389 on a remote Windows host. The general connection diagram looks like this:

windows 10 ssh tunnel

We need an SSH client for port forwarding. You can use a third-party client (like Putty), but I will use the built-in SSH client on Windows. Run the following command in the PowerShell console to install the OpenSSH client on Windows 10/11 or Windows Server 2022/2019:

Get-WindowsCapability -Online | ? Name -like 'OpenSSH.Client*'

To create an SSH tunnel with a remote host 192.168.1.90, run the command:

ssh -L 8888:192.168.1.90:3389 [email protected]

The following connection string is used in this example: LOCAL_PORT:DESTINATION_IP:DESTINATION_PORT and USER@DESTINATION_IP (the username and address of the remote SSH server)

To make the SSH tunnel run in the background, add the –f parameter.

windows 10 connect rsp via ssh tunneling

To connect to a Remote Desktop via the SSH tunnel, you need to connect to the local port 8888 of your computer using the RDP client (mstsc.exe):

127.0.0.1:8888

mstsc rdp port forwarding

Login to the remote computer and work safely in the RDP session. You can use the Get-NetTCPConnection cmdlet or the TCPView tool to make sure that the RDP connection has been established locally (the RDP connection was initiated by the SSH server running locally):

Get-NetTCPConnection -State Established|where {$_.localport -eq "3389"}|fl

check connection source ip and port in windows

You can check that the TCP/3389 port on the server is not directly accessible from your computer. You can check the availability of a port using the Test-NetConnection cmdlet:

Test-NetConnection 192.168.1.90 -port 3389

TcpTestSucceeded       : False

Other computers of your local network can also use this tunnel to simultaneously connect to an RDP server even if the direct connection is not allowed (both via SSH and RDP). To do it, they must use the RDP client to connect to port 8888 on your computer (with the SSH tunnel created):

mstsc.exe /v 10.10.1.220:8888

mstsc rdp port forwarding via ssh tunnel on windows 10

Securing an RDP connection with an SSH tunnel can be a good VPN alternative for accessing public Windows hosts. In this case, you don’t need to directly open the RDP/3389 port on the Windows host. It is enough to open only the SSH/22 port, which will protect you from RDP brute force attacks and the exploitation of 0-day RDP vulnerabilities.

You can use the sshpass tool to provide password automation for SSH login. You can run this tool through the WSL2 subsystem for Windows.

Install the sshpass in Ubuntu WSL:

$ sudo apt-get -y install ssphass

Run the Remote Desktop Connection client (mstsc.exe) and save the connection settings to the localhost-3389.rdp file:

Computer: localhost:8888
User name: remoteusername

To automatically connect to a remote RDP host with a saved SSH password, use the following bat file:

start /min wsl sshpass -p "password" ssh -L 8888:192.168.31.90:3389 [email protected]
powershell sleep 5
start mstsc C:\script\localhost-3389.rdp

Or (preferably) set up SSH key authentication.

How to Create SSH Tunnel on Windows with Putty?

Let’s look at how to create an SSH tunnel on Windows using the popular SSH client Putty.

  1. Run PuTTY and navigate to Connection -> SSH -> Tunnels;
  2. Specify the local port number in the Source port (in our example, it is 8888);
  3. Specify the IP address of the SSH server and the port on the remote host to forward the connection: 192.168.31.90:3389
  4. Select Local destination and click Add;create ssh tunnel forwarded port in putty
  5. To avoid opening a remote host shell when connecting through a tunnel, enable the Don’t start a shell or command at all option in the SSH section;putty Don’t start a shell or command at all
  6. Return to the Session tab, specify the name or IP address of the remote SSH host and the connection port number (22 by default). Specify the session name in the Saved Session field and click Save;
  7. Now you can start the saved SSH tunnel in Putty directly from the Windows 10 taskbar.putty run saved session

Remote TCP Forwarding (Reverse SSH) to a Local Computer

There is another SSH tunnel use case — remote TCP forwarding. You can allow a remote server to access a local port on your computer or a port on another computer on your local network through an  SSH tunnel. For example, you want an external server (192.168.1.90) to access your Intranet site (not published on the Internet). To create a reverse tunnel, use the following command:

ssh -R 8080:internalwww:80 [email protected]

Now, to access the internal website from a remote SSH server, just type the address http://localhost:8080 in the browser.

On all Windows versions, you can create the port forwarding rules using the netsh interface portproxy command.

With SSH tunnels, you can create port forwarding chains. You can enable or disable SSH tunneling in the OpenSSH configuration file (sshd_config) using the following directives:

AllowStreamLocalForwarding yes
AllowTcpForwarding remote
PermitTunnel no
These configuration directives aren’t available in the current version of OpenSSH for Windows.
1 comment
8
Facebook Twitter Google + Pinterest
previous post
Configure Google Chrome Settings with Group Policy
next post
How to Permanently Disable Microsoft Defender Antivirus on Windows 11 and 10

Related Reading

How to Cancel Windows Update Pending Restart Loop

May 6, 2025

View Windows Update History with PowerShell (CMD)

April 30, 2025

Change BIOS from Legacy to UEFI without Reinstalling...

April 21, 2025

Remove ‘Your License isn’t Genuine’ Banner in MS...

April 21, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

1 comment

Christian December 26, 2021 - 9:59 am

Hello, it worked perfectly with win 10 20h2 and rdp. Unfortunately it does not work with vnc.
i use ultravnc server as machine 3,port 5903 on the server and want on the client watch on port 5900. So I use the option – L 5900:localhost:5903, like the working rdp option. But with the vncviewer i get always “end of stream”.

Do you have any ideas?
Thanks

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Allow Multiple RDP Sessions on Windows 10 and 11
  • How to Repair EFI/GPT Bootloader on Windows 10 or 11
  • How to Restore Deleted EFI System Partition in Windows
  • Network Computers are not Showing Up in Windows 10/11
  • How to Run Program without Admin Privileges and Bypass UAC Prompt
  • Fix: BSOD Error 0x0000007B (INACCESSABLE_BOOT_DEVICE) on Windows
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top