Powershell ssh connect to linux

How to use SSH in Powershell

Who can imagine 10 years ago that today SSH would work from Windows native program?

Most of us when we want to connect to a Linux Server use the SSH with an application like Putty to establish the connection.

Today we can use SSH in PowerShell to create an SSH connection between Windows and Linux ,Linux and Windows or Windows to Windows.

I will explain step by step how can use OpenSSH or SSH in PowerShell Remoting to create an SSH connection from Windows to Linux or Linux to Windows.

Prerequisites to use SSH in Powershell

Make sure that meet the prerequisites before trying to use ssh in PowerShell

How to use SSH with PowerShell in Windows

Th faster way to use SSH in PowerShell is to install the OpenSSH Client or Server in Windows/Linux.

Then you can establish an SSH connection from Windows to Linux and vice versa.

If you have Windows 10 v1809 and prior then you can read my article that explains step by step how can install the SSH Client in Windows 10.

So let’s take it from the beginning and follow the steps

#Install the OpenSSH Server and OpenSSH Client in Centos

install openssh in Centos

  • Run the following command to install the open SSH server and the open SSH client.
    yum –y install OpenSSH-server OpenSSH-clients

start sshd service in Centos

  • Now run the following commands to allow the sshd service to run automatically after a restart, and start the sshd service.
    systemctl enable sshd
    systemctl start sshd

By default after the installation finish successfully you can establish the connection with the password. But we can change it to use a public/private key which is more secure.

Читайте также:  Linux клон mac os

use ssh in windows

  • For now let’s use the SSH with the password to test that all works fine.
  • From the Windows machine open the PowerShell and write the command as ssh @
    ssh root@192.168.37.8
  • Type the password of the user and the connection will be established

If we don’t use the then the local account of the machine trying to do the connection will be used by default.

#Create SSH Connection from Linux to Windows

If you would like to connect with SSH from a Linux machine to Windows you can read my article which describes step by step all the settings that need to be done in Windows.

How to use SSH over PowerShell Remoting

From PowerShell 6 and later SSH can be supported over PowerShell remoting which can use it on Windows or Linux as well.

SSH remoting will do basic PowerShell session between Windows and Linux.

In order to create an SSH connection over a PowerShell remoting you must use the following commands including the new parameter -Hostname instead of Computername.

The prerequisites to establish an SSH Connection over a Powershell Remoting is to install the Powershell 6 or higher and OpenSSH Client and Server to allow remote connection to and from the computer.

Note that if you want only to establish SSH connections from a specific machine you can install only the OpenSSH Client.

But if you want remote connection to and from the machines then you should install both(Server/client).

Let’s do a little practice

#How to Install PowerShell in Windows

You can use this single command in Windows Powershell to install the Powershell 7.

or you can download the msi package to install Manual.

You can find the links from Microsoft Docs Installing PowerShell on Windows

#How to Install PowerShell in Linux

You can use the following command to install the PowerShell 7 in Linux.

Are you finish the PowerShell installation in your machines ?

Then continue with the following steps to establish the connection

  • First of all you must configure the sshd_config in all machines that you would like to use SSH over Powershell remoting.
  • So in linux type vi /etc/ssh/sshd_config
  • Go at the end of the file and find the override default of no subsystems
  • Add the following line Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile

  • Restart the sshd service with the command
    sysemctl restart sshd
  • Now login on the Windows machines that you have installed the powershell.
  • Open the sshd_config. You will find it in C:\Programmdata\ssh
  • Go at the end of the file and find the line #override default of no subsystems
  • Add the line Subsystem powershell pwsh.exe -sshs -NoLogo -NoProfile
  • Restart the sshd service with the following command in Powershell
    Restart-Service -Name sshd
  • Now that you have finished with the configuration it’s time to test the connectivity.
  • From the Powershell 6 or 7 depends what you have install type the following command
    New-PSsession -hostname
  • As you can see with the parameter -Hostname the Transport is SSH

PowerShell is a multiplatform tool that you can use it with Windows and Linux.

You don’t need to use different tools to connect with SSH in Linux.

I hope my article helps you or explore something new or resolve a problem.

I invite you to follow me on Twitter or Facebook. If you have any questions, send me an email at info@askme4tech.com

Источник

SSH подключение к Linux из Windows PowerShell

Функция удаленного взаимодействия PowerShell обычно использует WinRM для передачи данных. Теперь протокол SSH доступен на платформах Linux и Windows, что позволяет осуществлять многоплатформенное удаленное взаимодействие с PowerShell. Начиная с PowerShell 6 и выше можно работать по WinRM между Windows и Linux машинами, так-же можно работать непосредственно по SSH, при условии, что на Windows машине установлен SSH клиент.

На сегодня PS доступен в 7-ой версии для Windows, Linux, macOS платформ, последние версии доступны на ОФИЦИАЛЬНОЙ СТРАНИЦЕ POWERSHELL в GitHub. Сегодня рассмотрим несколько сценариев работы / подключения к Linux из PowerShell Windows, участники эксперимента:

  • Windows Server 2016
  • CentOS 7 + PSRP (WinRM)
  • Ubuntu 18 (SSH)
  • PowerShell (последний релиз, на сегодня 7.0.0)
    • 0.1 Сценарии подключения
    • 1.1 Установка последней версии PowerShell в Windows
    • 1.2 Установка PowerShell в Linux
    • 1.3 Установка PSRP в CentOS
    • 1.4 Подключение PowerShell SSH (Windows > Linux)
    • 1.5 Пример удаленного вызова команды
    • 1.6 Удаленный запуск скриптов
    • 1.7 Проблемы OMID SSL
    • 2.1 Установка Windows SSH клиента
    • 2.2 На стороне Linux
    • 2.3 Подключение по SSH
    • 2.4 Устранение ошибки A remote session might have ended.
    • 2.5 Использование SSH клиента из командной строки

    Сценарии подключения

    1. Использование PSRP (WinRM)
    2. Использование SSH клиента из Powershell (более лаконочиный и предпочитаемый сценарий)

    Небольшое предисловие — это ознакомительная статья, использование PS на Linux проде, это хороший вопрос, который требуется хорошо обдумать, прежде, чем использовать. По ходу статьи будут приведены ссылки на те или иные материалы / аббревиатуры, так что будем считать, что вводная на используемые технологии дана в той или иной мере.

    Сценарий 1

    Использование OMI сервера, работа по WINRM (PSRP) (детали ниже).

    Установка последней версии PowerShell в Windows

    Устанавливается одной командой:

    Источник

Оцените статью
Adblock
detector