Enable PowerShell SSH Remoting in PowerShell 7

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

内容简介: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.


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

查看所有标签

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

搜索模式

搜索模式

Peter Morville、Jeffery Callender / 蒋彬 / 电子工业出版社 / 2010-10 / 35.00元

本书是信息架构领域创始人彼得•莫维里的又一力作,全书详尽剖析了10种搜索模式,告诉读者如何为不同情境设计搜索功能,涉及互联网、电子商务、企业、手机、社交和实时搜索等不同平台和领域。每种搜索模式均配以大量案例,并结合了作者自身的经验,因此更富实用性和实战性。书中遍布作者对于搜索模式的探索和思考,既适合对未来的搜索进行前瞻性的探讨,也能够指导当前进行中的项目。一起来看看 《搜索模式》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具