Windows remote desktop via SSH tunnel

This post shows how to connect Windows Server that can’t be accessed directly by remote desktop.
As shown in the following figure, Windows Client access Windows Server by remote desktop via SSH Server.

Servers

  • Windows Client
    Remote desktop client
    Operating system is Windows 10
  • SSH Server
    SSH Server can be accessed from Windows Client directly
    If operating system is Windows Server 2019, the following post shows how to install ssh server.
    >> How to install OpenSSH server on Windows Server 2019 in offline environment
  • Windows Server
    Windows Server can be accessed from SSH Server directly however Windows Client can’t access directly.
    Operating system is Windows Server 2019

How to connect

Create SSH tunnel

Run the following command on Windows Client.

ssh -L 33389:192.168.1.1:3389 <username>@x.x.x.x
  username : User of SSH Server

SSH Client is installed on Windows 10 by default.

Enter password of ssh user.

<username>@x.x.x.x 's password:

Minimize the command prompt window not close.

Connect by remote desktop

On the Remote Desktop Connection, enter the following information and connect.

Computer : localhost:33389
User name : User name of Windows Server that is destination

The foregoing is how to connect Windows Server that can’t be accessed directly by remote desktop with ssh tunnel.

Use batch file

Create batch file to connect easily.

This is at your own risk because user name and password are stored as plain text in a batch file.

Install Windows Subsystem for Linux (WSL)

Install sshpass on Ubuntu on Windows Subsystem for Linux (WSL) according to the following steps to enter the ssh password automatically.

(1) Refer to the following post , install Windows Subsystem for Linux on Windows Client.
>> Install WSL2 on Windows 10 and use Ubuntu

(2) Run the following command on Ubuntu on WSL to install sshpass.

sudo apt-get -y install ssphass

Create rdp file

On the Remote Desktop Connection, enter the following information and Save as.

Computer : localhost:33389
User name : User name of Windows Server that is destination

For example save as C:\rdp\localhost-33389.rdp

Create batch file

Create a batch file with the following contents and save as ssh-rdp.bat

start /min wsl sshpass -p "<password>" ssh -L 33389:192.168.1.1:3389 <username>@x.x.x.x
powershell sleep 5
start mstsc C:\rdp\localhost-33389.rdp
password : Password of SSH Server
username : User name of SSH Server

Run wsl command on Windows to run Linux command on WSL.
Create ssh tunnel.

start /min wsl sshpass -p "<password>" ssh -L 33389:192.168.1.1:3389 <username>@x.x.x.x

Wait a few seconds until ssh tunnel is created.

powershell sleep 5

Run the rdp file.

start mstsc C:\rdp\localhost-33389.rdp

That’s about it.