Enable PowerShell SSH Remoting in PowerShell 7

栏目: IT技术 · 发布时间: 4年前

内容简介:In this blog post, we will have a look at how you can enable and set up PowerShell SSH Remoting or PowerShell Remoting over SSh withPowerShell 7. WithPowerShell Core 6, Microsoft introduced PowerShell 7 Remoting over SSH, which allows true multiplatform Po

In this blog post, we will have a look at how you can enable and set up PowerShell SSH Remoting or PowerShell Remoting over SSh withPowerShell 7. WithPowerShell Core 6, Microsoft introduced PowerShell 7 Remoting over SSH, which allows true multiplatform PowerShell remoting between Linux, macOS, and Windows. PowerShell SSH Remoting creates a PowerShell host process on the target machine as an SSH subsystem. Normally, Windows PowerShell remoting uses WinRM for connection negotiation and data transport. However, WinRM is only available on Windows-based machines.

There are also some downsides to it. SSH-based remoting doesn’t currently support remote endpoint configuration and JEA ( Just Enough Administration ). It is also important to understand that this is not just another PowerShell SSH client .

Use SSH Transport with PowerShell Remoting

To use PowerShell 7 remoting with SSH on Windows, Linux, and macOS machines, you can use the same cmdlets you are already familiar from Windows PowerShell remoting with WinRM.

  • New-PSSession
  • Enter-PSSession
  • Invoke-Command

There are three new parameters for these cmdlets if you are using PowerShell SSH remoting.

  • -HostName (Instead of -Computername, you define the SSH target)
  • -UserName (Instead of -Credentials you use the -UserName parameter)
  • -KeyFilePath (If you are using SSH key authentication you can use the -KeyFilePath parameter to point to the key file)
New-PSSession -HostName tomsssh.server.com -UserName thomas

Enable PowerShell SSH Remoting

To work with PowerShell SSH, you will need to complete the following steps on all systems and machines.

  • Install OpenSSH Server and Client
    • OpenSSH for Windows is available directly inWindows 10 (1809 or higher) andWindows Server 2019 as an optional feature.
    • On Linux, you install OpenSSH depending on your platform
  • Install PowerShell 7 on all systems
  • Configure the SSH subsystem to host a PowerShell process on the remote machine
  • Configure password or key-based authentication

Step by step set up SSH remoting on Windows

Step 1: First, you will need to install PowerShell 7 on Windows. You can follow my blog post toinstall PowerShell 7.

Enable PowerShell SSH Remoting in PowerShell 7

Install PowerShell 7

Step 2: Install OpenSSH Client and Open SSH Server. To install the ssh client and server and initially configure the OpenSSH server, you can use the following command.

# Install the OpenSSH Client and Server
 
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
 
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
 
# Initial Configuration of SSH Server
 
Start-Service sshd
 
Set-Service -Name sshd -StartupType 'Automatic'
 
# Confirm the Firewall rule is configured. It should be created automatically by setup.
 
Get-NetFirewallRule -Name *ssh*
 
# There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled

Step 3: Configure and edit the sshd_config file located at $env:ProgramData\ssh on the target machine.

Enable PowerShell SSH Remoting in PowerShell 7

Edit sshd_config for PowerShell Remoting

Check that password authentication is enabled by removing the “#” sign.

PasswordAuthentication yes

Add the Subsystem for PowerShell. You can see that we are using the 8.3 short names for the file paths that contain spaces.

Subsystem powershell c:/progra~1/powershell/7/pwsh.exe -sshs -NoLogo -NoProfile

The 8.3 short name for the Program Files folder in Windows is usually Progra~1. However, you can use the following command to make sure.

Get-CimInstance Win32_Directory -Filter 'Name="C:\\Program Files"' | Select-Object EightDotThreeFileName

Optional enable key authentication

PubkeyAuthentication yes

If you are interested in setting up key-based authentication with OpenSSH on Windows Server , check out the blog post on ITOpstalk.com by Orin Thomas (Microsoft Cloud Advocate).

Step 4: Restart the sshd service

Restart-Service sshd

Step by step set up SSH remoting on Linux (Ubuntu 18.04)

Step 1: First, you will need to install PowerShell 7 on Linux. You can follow my blog post toinstall PowerShell 7.

Enable PowerShell SSH Remoting in PowerShell 7

Step 2: Install OpenSSH Client and Open SSH Server. To install the client and server and initially configure the OpenSSH server, you can use the following commands.

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

Step 3: Configure and edit the sshd_config file at location /etc/ssh on the target machine.

Check that password authentication is enabled

PasswordAuthentication yes

Add the Subsystem for PowerShell

Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile

Optional enable key authentication

PubkeyAuthentication yes

Step 4: Restart the sshd service

sudo service sshd restart

SSH Remoting with PowerShell 7

Now you can start using PowerShell SSH remoting to connect even from different operating systems like Windows to Linux or Linux to Windows.

Enable PowerShell SSH Remoting in PowerShell 7

PowerShell 7 SSH Remoting

As mentioned before, you can use the same commands as you are familiar with, like New-PSSession, Enter-PSSession, or Invoke-Command.

As a client, you can also use theAzure Cloud Shell, which runs PowerShell 7. If you want to know more about what’s new in PowerShell 7 , check out my blog post. If you want to know how to set it up with macOS, you can read the following Microsoft Docs article .

Of course, you can also use the PowerShell SSH remoting to connect from Linux to Linux and Windows to Windows. You can find more information about PowerShell SSH remoting on the Microsoft Docs pages. If you have any questions, please let me know in the comments.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Git

Pro Git

Scott Chacon / Apress / 2009-8-27 / USD 34.99

Git is the version control system developed by Linus Torvalds for Linux kernel development. It took the open source world by storm since its inception in 2005, and is used by small development shops a......一起来看看 《Pro Git》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具