Ssh-подключения к windows-серверу из linux mac os с использованием power shell

Secure Shell (SSH) is widely used for communicating with remote servers. SSH command is usually used in Linux and Unix-based systems. But we can use SSH in Windows with PowerShell.

Powershell is a command-line shell for scripting developed by the Microsoft team. As a powerful tool, you can install and integrate many services into PowerShell. You can use SSH in PowerShell by installing OpenSSH or with WSL (Linux Subsystem for Windows). You can connect to the remote server, transfer files and execute the command line directly in PowerShell.

In this blog post, I’ll discuss two different methods to use SSH in Windows PowerShell.

Table of Contents

Installing and Configuring OpenSSH in Powershell

OpenSSH is the most popular SSH client for connecting remote servers. Although It is the default SSH client in Linux and macOS, it does not provide an official client for the Windows operating system. But you can use SSH in Windows Powershell, all thanks to the PowerShell team, who tweak OpenSSH for Windows.

Download and Install OpenSSH

To use OpenSSH in PowerShell, download it from the Powershell GitHub release (https://github.com/PowerShell/Win32-OpenSSH/releases).

Powershell GitHub release

Choose OpenSSH-Win64 for a 64-bit operating system. After downloading the installer package, you can install it by double-clicking it. After installing

you can start using SSH in Windows PowerShell.

OpenSSH in PowerShell

Configuring OpenSSH

The server configuration file sshd_config is located in C:\ProgramData\ssh\sshd_config . You can also open this file in Notepad and Notepad++

Installing WSL in Powershell

Another way to use SSH in PowerShell is using SSH in WSL (Windows Subsystem for Linux). WSL is a compatibility layer for running Linux commands and applications on your Windows machine without using a virtual machine or Dual boot.

Before installing WSL on your Windows 10/11, ensure your system meets the minimum requirements.

  • You are running Windows 10 version 1903 or later
  • You have at least 4 GB of Ram
  • You are using a 64-bit operating system

Install WSL2 on Windows 10/11

This command will install the latest version of Ubuntu on your Windows System. After installing WSL restart your computer to use WSL.

Install WSL With Specific Distribution

If you prefer Linux distribution, you have several options to choose from. To check available Linux distributions to choose from, enter the command in PowerShell.

This command will show you available Linux distributions you can install on your Windows system. WSL supports distributions such as Ubuntu, OpenSUSE, Kali, Debian, and Arch Linux.

image 1

This command will install your preferred Linux Distribution. After installing the distribution restart the Windows to start using WSL.

Use SSH in WSL Powershell

To use SSH in WSL, open Powershell, type wsl , and hit enter to enter Linux Subsystem.

WSL SSH in Powershell

By default, OpenSSH is pre-installed in most distributions. But if OpenSSH is not installed in your distribution, you can install it easily.

 apt-get update &&  apt-get install openssh-server

Once you enter the subsystem, you can execute the Linux command in WSL. Use ssh the command to use OpenSSH within WSL.

Establishing a Connection with SSH

Once you have installed SSH in PowerShell with either one of these methods, you can establish SSH connection to your remote server. To connect to your remote server, you can use the command.

  • Replace user with your username and example.com with your public IP address.

Then enter your password to establish the connection. If you have a public/Private key, you can connect your remote server without a password. For this, enter the command.

  • Replace the key file with your key file location.
  • Use your username and public IP address.

Basic SSH Commands in PowerShell

Now that you have established SSH connection, you can use these commands in PowerShell to maximize your output.

SSH

SCP

SSH-Keygen

The ssh-keygen command generates SSH key pair to connect to a remote server without a password. SSH keys are more secure than passwords as it is impossible to guess SSH keys. To generate an SSH key, just type

This command will create SSH public and private key pair. You can submit this key to a remote server to authenticate your SSH connection without a password.

:/>  Как открыть 80 и 443 порт? — Хабр Q&A
SSH keygen in Powershell

These are a few basic SSH commands. You can use more advanced SSH commands in Windows PowerShell.

DevFunOps With Pradeep

SSH connections to Windows Server from Linux, macOS using PowerShell.

By reading this blog, you’ll learn how to easily set up SSH authentication for your Windows system. This improves security and makes managing your servers remotely much simpler and more efficient. Whether you’re new to IT or just looking for a straightforward way to secure your remote access, this blog has got you covered with practical steps and clear instructions.

To establish password less SSH connections from Linux or macOS to a Windows Server using PowerShell, you’ll need to first ensure that SSH is set up and properly configured on the Windows Server.

This involves installing and enabling the OpenSSH server feature on the Windows Server, which allows it to accept incoming SSH connections.

Once OpenSSH is installed, you’ll need to generate an SSH key pair (consisting of a public and private key) on your Linux or macOS machine.

The public key will then need to be added to the authorized_keys file on the Windows Server, allowing it to authenticate incoming SSH connections using the corresponding private key.

This process creates a secure and convenient method for accessing the Windows Server from Linux or macOS without the need for passwords.

Absolutely🤠🤠🤠, the steps provided are a one-time setup to install and configure OpenSSH on your Windows Server. Once you’ve completed these steps, the OpenSSH server will be installed and configured, ready to accept SSH connections securely.

  1. Log in to your Windows Server: Before beginning the process of setting up password less SSH connections from Linux or macOS to a Windows Server, you need to log in to your Windows Server using a Remote Desktop Protocol (RDP) client. This allows you to access the server’s desktop environment and execute PowerShell commands to configure SSH.
Log in to your Windows Server

Once you’re logged into the Windows Server via Remote Desktop, you can proceed to install OpenSSH and configure the SSH server using PowerShell.

2. Install OpenSSH Server on Windows:
This step involves installing the OpenSSH Server feature on the Windows Server using PowerShell.

 - Add-WindowsCapability -Online -Name OpenSSH.Client
- Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
- Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
- Start-Service sshd
- Set-Service -Name sshd -StartupType 'Automatic'
- Get-Service sshd

If the service is running, you’ll see its status listed as “Running” in the output. If it’s stopped, you’ll see its status listed as “Stopped” instead.

Install OpenSSH Server on Windows

Excellent!🚀🚀🚀🚀🚀 Congratulations on successfully completing the installation of OpenSSH on your Windows Server 🍻🍻🍻🍻🍻

3. Configure SSH Server:
By editing the SSH server configuration file, you can specify the desired authentication methods. In this case, the configuration is adjusted to enable public key authentication.

To open the SSH server configuration file on your Windows Server, you can use any text editor. Here’s how you can do it in PowerShell:

Executing this command in PowerShell will open the sshd_config file in Notepad.

Remove the # character at the beginning of the PubkeyAuthentication yes line to uncomment it. Save the changes and close Notepad.

Configure SSH Server
Create new User on windows

6. Generate SSH Key Pair:
On the client machine (Linux or macOS), you generate an SSH key pair consisting of a private key and a public key. The private key remains on the client machine, while the public key will be copied to the Windows Server.

To generate an SSH key pair on a Linux or macOS system, you can use the ssh-keygen command.

You’ll also be prompted to enter a passphrase for the key pair. It’s recommended to use a passphrase for added security. Enter a passphrase and confirm it when prompted, or press Enter to leave it blank for no passphrase.

Once the key pair is generated, you’ll see a message indicating the location of the public and private keys.

Generate SSH Key Pair
Copy Public Key to Windows Server
Copy Public Key to Windows Server
cp .\id_rsa.pub authorized_keys
Copy Public Key to Windows Server
Testing SSH Login
Testing SSH Login

If you ever need assistance, guidance, or just someone to bounce ideas off of, feel free to reach out. Here’s to endless discoveries and triumphs ahea 😊👍 Your feedback and questions are always welcome in the comments section below. We’re here to support you on your AWS journey!🚀🚀

:/>  Как скопировать экран и вставить в ворд

Subsystem is not meant to change your default shell, quite the opposite – it defines a command that clients have to explicitly request. (It’s literally a server-side alias.)

So the configuration snippet you found wasn’t meant to change what you get from regular ssh; it’s meant to allow a local PowerShell instance to invoke the SSH client and start a remote PowerShell on the server, without having to know its path (all it needs to specify is the subsystem name).

Specifically, the -sshs option indicates that it’s meant for PSRemoting over SSH using the Enter-PSSession or Invoke-Command cmdlets, which can run via SSH in PowerShell 7.

PS> Enter-PSSession -HostName otherpc
[otherpc] PS>

(-HostName option makes it use SSH, whereas -ComputerName makes it use WinRM.)

(This won’t work in PowerShell 5 – its PSRemoting support can only run via WinRM, though if you want PowerShell then WinRM is actually not a bad alternative to SSH.)


Actually changing the default shell in Windows OpenSSH (the official Microsoft builds) is done completely differently – it’s documented on this page:

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" ` -Name DefaultShell ` -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" ` -PropertyType String ` -Force

When I am trying to use ssh using powershell 7 it throws an error:

ssh: The term 'ssh' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Error in powershell

But it is working fine for cmd and windows powershell
Working on CMD
Working on Windows Powershell

I have also noticed that I cannot change directory to OpenSSH (C:\Windows\System32\OpenSSH)
Cannot cd to OpenSSH

I tried adding Openssh to the system Path, installing openssh client and server but did not work.

How can I fix it?

asked Aug 7, 2023 at 10:05

Adnan Bin Zahir's user avatar

I finally resolved the issue.

When installing PowerShell, I used to check the box that enables PowerShell remoting. However, this time I did not, and now SSH works.

  1. If OpenSSH is not installed (Doesn’t even work in cmd) then follow this first: Get started with OpenSSH for Windows
  2. Uninstall PowerShell.
  3. Download the installer: PowerShell Latest Release.
  4. During installation, make sure to leave the PowerShell remoting box unchecked.
    Keep PowerShell remoting unchecked

That was all I had to do.

And here is the screenshot of it working:
enter image description here

answered Dec 12, 2023 at 9:16

Adnan Bin Zahir's user avatar

Add ssh to the path.

$variable = Get-ItemPropertyValue -Path 'HKCU:\Environment\' -Name 'Path'
$new_path = $variable + ';C:\Windows\System32\OpenSSH\'
Set-ItemProperty -Path 'HKCU:\Environment\' -Name 'Path' -Value $new_path

Restart powershell and check this way

($env:path).split(";")

Alexandr's user avatar

2 gold badges4 silver badges17 bronze badges

3.2.0

Provide SSH and SCP functionality for executing commands against remote hosts.

Minimum PowerShell version




Install-Module -Name Posh-SSH


Install-PSResource -Name Posh-SSH

Owners

  • gravatar

    cperez

Copyright







VersionDownloadsLast updated


3.2.0
(current version)

2,882,609


3.1.3

2,549,216


3.1.3-beta1

54


3.1.2

1,032,148


3.1.1

2,276,069


3.0.8

9,523,209


3.0.7

95,285


3.0.6

1,645,947


3.0.5

48,576


3.0.4

460,059


3.0.3

354,892


3.0.2

6,631


3.0.1

1,420,246


3.0.0

3,809,541


3.0.0-beta5

1,340


3.0.0-beta4

103


3.0.0-beta3

109


3.0.0-beta2

218


3.0.0-beta1

52,318


3.0.0-alpha

682


2.3.0

3,167,450


2.3.0-Beta1

2,074


2.2

2,302,278


2.1

3,571,234


2.0.2

784,108


2.0.1

49,529


2.0

2,325


1.7.7

118,823


1.7.6

43,808


1.7.5

7,214


1.7.4

114


1.7.3

53,704


1.7.2

953


1.7

155


1.6

509

When working in a mixed environment, PowerShell and SSH make for a good remote management combination.

When Microsoft made its PowerShell automation tool open source in 2016, part of the reason was to develop cross-platform compatibility to include macOS and Linux systems. It came as no surprise when the PowerShell team implemented SSH connectivity to enable PowerShell remoting to Linux boxes. SSH is widely considered to be the standard for remoting. Learn how PowerShell remoting over SSH is different from traditional remote management methods in a Windows environment and the basics of how to use remoting over SSH to both Windows and Linux machines.

What is the difference between remoting with SSH and WinRM?

The conventional method for managing Windows remotely is with Windows Remote Management (WinRM), which is Microsoft’s version of the Web Services-Management protocol.

To start the WinRM service on Windows, enter the command from the Run menu. WinRM is a Windows native service that PowerShell has supported since version 2.0 on Windows 7 and Windows Server 2008.

SSH is the standard remoting tool for Linux. Microsoft added native support on clients running at least Windows 10 1809 and servers running at least Windows Server 2019 with the optional OpenSSH capability installed.

However, if you are introducing Windows into a Linux environment where your configuration management tool, such as Ansible, is already configured for SSH on port 22, then it is reasonable to stick with SSH. Ansible supports managing Windows over SSH, although it is considered experimental at this stage.

The prerequisites for PowerShell remoting over SSH are PowerShell 7+ and SSH.

The target computer requires an SSH server, and the source computer requires an SSH client. For Windows, OpenSSH is recommended for both server and client in its respective variant.

https://youtube.com/watch?v=Atbl7D_yPug%3Fautoplay%3D0%26modestbranding%3D1%26rel%3D0%26widget_referrer%3Dhttps%3A

How to set up OpenSSH on Windows

OpenSSH feature check
Figure 1. Use PowerShell to check the status of OpenSSH on the system.
Add-WindowsCapability -Online -Name "OpenSSH.Server~~~~0.0.1.0"

Lastly, start the SSH service:

SSH remoting configuration
Figure 2. The Enable-SSHRemoting cmdlet handles updates to the SSHD service and other tasks related to SSH configuration.

Run the help for the cmdlet to see all the work this PowerShell command performs related to SSH remoting:

The last step is to restart the service:

How to set up OpenSSH for PowerShell on Linux

sudo apt install powershell
sudo apt install openssh-client
sudo apt install openssh-server

Next, use the same PowerShell module and command used with Windows to install RemotingTools and run the cmdlet and restart sshd, the SSH service:

Install-Module Microsoft.PowerShell.RemotingTools
Enable-SSHRemoting
service sshd restart

How to perform PowerShell remoting over SSH

The difference is that using the PowerShell command uses the PowerShell endpoint.

You can finish the session with the command.

PowerShell remoting over SSH does not require named endpoints like WinRM. Instead, you can use an IP address.

works from Windows or Linux to a Windows machine or a Linux machine as long as you have met the prerequisites.

How to run scripts with a PowerShell remoting session

is great for interactive use, but running a script remotely requires a different approach. You want to use to create a session object and then run commands with the cmdlet. The command starts a persistent session to the remote machine, while enables running commands on local and remote machines.

First, generate and configure SSH keys needed to pass a password to the cmdlets. The process to set up SSH keys is outside the scope of this article, but producing them for PowerShell is the same as making them for typical SSH activities.

After creating the SSH keys, create a PowerShell session object:

remote machine processes
Figure 3. Use the Invoke-Command cmdlet to see the processes running on the remote machine.

How do I perform advanced PowerShell remoting procedures?

The script has no OS checks because works the same in Windows and Linux.

In this example script, you can find all computers with a certain file path:

How to troubleshoot PowerShell remoting over SSH issues

If you find yourself unable to connect to a remote device over SSH after checking through the prerequisites, then you can use PowerShell to troubleshoot.

First, check that you have restarted the sshd service with this command:

Next, test SSH connectivity:

Test-NetConnection -ComputerName 10.0.0.10 -Port 22

Lastly, test to see if SSH is working by using SSH without PowerShell:

Why you should consider SSH remoting with PowerShell

Even if WinRM-based PowerShell remoting is the recommended method for Windows device management, you can see the possibilities that open up with SSH remoting. As long as you don’t need remote endpoint configuration or JEA, then SSH works in most scenarios. If you want to have a consistent remoting experience across Windows and Linux, then it’s time to get SSH up and running.