How to connect to a remote Windows computer via SSH

This article is converted by 简悦 SimpRead, original URL blog.csdn.net

Introduction

SSH (Secure Shell Protocol) is an encrypted network protocol used to log into remote computers over insecure networks. Although SSH was originally designed for Unix systems, Windows systems now also support SSH services. In this article, we will discuss how to set up an SSH server on Windows and connect to it from another computer using an SSH client.

Step 1: Install OpenSSH Server on Windows

Open the “Settings” app. Click on “Apps”.

On the right side, click “Optional Features”. (Or directly search for Optional Features in the settings search)

Click “Add a feature”.

Find and select “OpenSSH Server” from the list, then click “Install”. You can also install the OpenSSH Client at the same time.

Once installation is complete, the OpenSSH server will start automatically and will start automatically every time Windows boots.

Run the following command in PowerShell (Admin mode):

Run the command Get-WindowsCapability -Online | ? Name -like 'OpenSSH*' to check whether the OpenSSH server and client are installed. If installation is complete, it will show as below:

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

Step 2: Start the SSH Service

Run the command

net start sshd

Run the command ssh localhost to check the connection status. The image below shows a successful connection.

ssh localhost

Run the command Set-Service -Name sshd -StartupType 'Automatic' to set SSH service to start automatically on boot

Set-Service -Name sshd -StartupType 'Automatic'

If it appears as the image below, it means the ssh service may not be started. Run net start sshd to start the service; it might also be that the firewall is blocking port 22.

Solution:

To ensure the SSH server can accept external connections, you need to open port 22 in Windows Defender Firewall or any other third-party firewall software.

  1. Open the “Control Panel”.
  2. Select “System and Security”.
  3. Click “Windows Defender Firewall”.
  4. On the left, select “Allow an app or feature through Windows Firewall”.
  5. Find “OpenSSH Server” in the list and ensure it is allowed through the firewall.

Step 3: Use SSH Client to Connect to Windows

  1. From another computer, open the SSH client.
  2. Open PowerShell and use the following command to connect to the Windows SSH server:
ssh username@Windows_computer_IP_address

For example, if your Windows computer’s IP address is 192.168.1.100 and your username is john, you should enter:

ssh john@192.168.1.100

Then enter the password. (For the first connection, enter yes first, then enter the password)

Now, you should have successfully connected to the Windows computer and can execute commands.

Notes

  • By default, non-administrator users can also connect to Windows via SSH. To forbid non-administrator users from connecting, you may need to modify the sshd_config file.
  • Ensure your Windows password is sufficiently complex to prevent unauthorized access.
  • Consider changing the default SSH port (22) to increase security.

Troubleshooting

Common Issue 1

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! message appears

Cause:

This is a security warning from SSH. This warning usually occurs in the following situations:

  1. The target server’s public key has changed. This may be due to reinstalling the server, changing server hardware, or manually changing the server’s SSH key.
  2. Someone is attempting a Man-in-the-Middle attack on your connection. This is rare but cannot be completely ruled out.

Solution:

To resolve this, you should first determine the cause. If you are sure the server’s public key has legitimately changed, you can proceed as follows:

Open terminal or command prompt.

Use the following command to remove the old key entry for the specified IP address:

ssh-keygen -R 192.168.136.128       //Your SSH server IP

Then try connecting to the server again. On first connection, you will be prompted to accept the new public key. Confirm, and then connection will proceed normally.

However, if you have not made any changes to the server and you are concerned about this warning, you should investigate further to ensure your network environment is not under attack.

Summary

By following the above steps, you should be able to easily set up an SSH server on Windows and connect to it remotely. SSH provides you with a secure way to manage and control your Windows computer.