<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kiran Reddy</title>
    <description>The latest articles on DEV Community by Kiran Reddy (@v2kiran).</description>
    <link>https://dev.to/v2kiran</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F365671%2Fe54fbd41-c661-4ccd-a4c0-a84cbd8a76b3.jpeg</url>
      <title>DEV Community: Kiran Reddy</title>
      <link>https://dev.to/v2kiran</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/v2kiran"/>
    <language>en</language>
    <item>
      <title>PowerShell 7 RSA keys SSH Remoting</title>
      <dc:creator>Kiran Reddy</dc:creator>
      <pubDate>Thu, 16 Apr 2020 08:52:32 +0000</pubDate>
      <link>https://dev.to/v2kiran/powershell-7-rsa-keys-ssh-remoting-3m7e</link>
      <guid>https://dev.to/v2kiran/powershell-7-rsa-keys-ssh-remoting-3m7e</guid>
      <description>&lt;h1&gt;
  
  
  RSA key-based PowerShell 7 SSH remoting
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;Use PowerShell SSH remoting from Windows 10 to Windows 2012 Server&lt;/p&gt;

&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Remotely login and administer computers without providing credentials.&lt;/li&gt;
&lt;li&gt;Works with machines that are in a workgroup(Non-AD) as well as on machines that are in different domains.&lt;/li&gt;
&lt;li&gt;Works across various operating systems

&lt;ol&gt;
&lt;li&gt;Windows → Mac OS or Linux&lt;/li&gt;
&lt;li&gt;Linux or Mac OS → Windows&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;placeholder&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Assumptions
&lt;/h2&gt;

&lt;p&gt;PowerShell 7 has been installed on both the client as well as the server and the install path is :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;C:\Program Files\PowerShell\7&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  On Windows 10 - Client
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install OpenSSH
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;OpenSSH feature is built into Windows 10 &lt;code&gt;build version&lt;/code&gt; 1809 and above .The feature just needs to be enabled.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To check the Windows 10 build version type &lt;code&gt;Winver&lt;/code&gt; in PowerShell .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--upMBPmX3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/v2kiran/powershell7/raw/master/images/winver.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--upMBPmX3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/v2kiran/powershell7/raw/master/images/winver.png" alt="Winver"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open PowerShell as Administrator and type&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get-WindowsCapability -Online | where Name -like 'OpenSSH*'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0fHmo2eb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/v2kiran/powershell7/raw/master/images/sshstate.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0fHmo2eb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/v2kiran/powershell7/raw/master/images/sshstate.png" alt="SSH State"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If the &lt;code&gt;state&lt;/code&gt; of the &lt;code&gt;openssh.client&lt;/code&gt; is &lt;code&gt;NotPresent&lt;/code&gt; then we need to install it.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In order to SSH from Windows 10 to a remote machine we dont need to enable the OpenSSH.Server role on Windows 10. We just need the client enabled or installed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configure the SSH Client
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Set the service to start Automatically&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set-Service ssh-agent -StartupType Automatic
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start the service&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start-Service ssh-agent -PassThru
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Generate RSA Key-Pair
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Change to the the user profile ssh directory&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd $home\.ssh
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Generate the Public-Private key pair.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen.exe -t rsa
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You will be prompted to provide a password to secure the private key. Hit enter to continue without providing a password.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Enter passphrase (empty for no passphrase):&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Enter same passphrase again:&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;sample output&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS C:\Users\kiran\.ssh&amp;gt; ssh-keygen.exe -t rsa                                                                                                                                                                  Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\kiran/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\kiran/.ssh/id_rsa.
Your public key has been saved in C:\Users\kiran/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:x5H0LQ29b4favJIxmSKlmYe42JzHJxgEZXaPuX69asQ lab\kiran@KIRAN-Laptop
The key's randomart image is:
+---[RSA 2048]----+
|     .+ . . ..   |
|    .o . = o +.  |
|     .  o + o o. |
|      .  o.. ..  |
|     . .SBo  o o |
|      o.*.E.= . +|
|     + *.+...B ..|
|    . * +.o +.o  |
|       . +...... |
+----[SHA256]-----+
PS C:\Users\kiran\.ssh&amp;gt; dir

    Directory: C:\Users\kiran\.ssh

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        3/18/2020   2:50 PM           1675 id_rsa
-a----        3/18/2020   2:50 PM            410 id_rsa.pub
-a----        3/18/2020  10:58 AM            204 known_hosts
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You should see the following 2 files in the .ssh directory&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;id_rsa        ⇒ Private Key&lt;/li&gt;
&lt;li&gt;id_rsa.pub ⇒ Public Key&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  On Windows 2012 - Server
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Install OpenSSH
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Donwload the latest version of OpenSSH from &lt;a href="https://github.com/PowerShell/Win32-OpenSSH/releases/tag/v8.1.0.0p1-Beta"&gt;Github&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Extract contents of the latest build to C:\Program Files\OpenSSH (Make sure binary location has the Write permissions to just to SYSTEM, Administrator groups. Authenticated users should and only have Read and Execute.)&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;From an elevated PowerShell Install SSH:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PS C:\Program Files\OpenSSH&amp;gt; .\install-sshd.ps1
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open the firewall for sshd.exe to allow inbound SSH connections&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;New-NetFirewallRule -Name sshd -DisplayName 'SSH Inbound' -Profile @('Domain', 'Private') -Enabled True -Direction Inbound -Action Allow -Protocol TCP ‑LocalPort 22
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configure the SSH server service
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Set the service to start Automatically&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set-Service sshd -StartupType Automatic
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start the service&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start-Service sshd -PassThru
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configure the SSH server Shell
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The default shell used by SSH is the Windows command shell. We change to PowerShell:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\PowerShell\7\pwsh.exe" -PropertyType String -Force
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;There's a bug in OpenSSH on Windows. It doesn't work with paths with a space.  For more information, see this &lt;strong&gt;&lt;a href="https://github.com/PowerShell/Win32-OpenSSH/issues/784"&gt;GitHub issue&lt;/a&gt;&lt;/strong&gt;. The workaround is to create a symbolic link that creates a path that OpenSSH can use:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;New-Item -ItemType SymbolicLink -Path C:\pwsh -Target 'C:\Program Files\PowerShell\7'
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;OR&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You can use the 8.3 short name for any file paths that contain spaces. The 8.3 short name for the &lt;code&gt;Program Files&lt;/code&gt; folder in Windows is usually &lt;code&gt;Progra~1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will Use the path below in the sshd_config file.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c:/progra~1/powershell/7/pwsh.exe
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configure the SSH server sshd_config file
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The SSH keys and configuration file reside in C:\ProgramData\ssh, which is a hidden folder. edit the config file sshd_config file as follows:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
StrictModes no
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile  .ssh/authorized_keys

#AuthorizedPrincipalsFile none

# For this to work you will also need host keys in %programData%/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no

# GSSAPI options
#GSSAPIAuthentication no

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# override default of no subsystems
#Subsystem  sftp    sftp-server.exe
Subsystem    powershell c:/progra~1/powershell/7/pwsh.exe -sshs -NoLogo -NoProfile

# Example of overriding settings on a per-user basis
#Match User anoncvs
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server

Match Group administrators
#AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;After saving the changes to the sshd_config file, restart the sshd server service:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Restart-Service sshd -PassThru
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Public Key configuration
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Run this from the windows 10 client
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Copy the public key from the Windows 10 client to Windows 2012 server:&lt;/p&gt;

&lt;p&gt;Make sure that the .ssh directory exists in your server's user home folder.&lt;/p&gt;

&lt;p&gt;User can either be a local or a domain account. In my case i am using a domain account&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh kiran@windows2012Server  new-item c:\users\kiran\.ssh -ea 0 -item directory
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use scp to copy the public key file generated previously on the window10 client to authorized_keys on your server&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scp C:\Users\kiran\.ssh\id_rsa.pub kiran@windows2012Server:C:\Users\kiran\.ssh\authorized_keys
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Testing SSH with SSH.exe
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Run this from the windows 10 client
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Test ssh with a domain user named kiran&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh -v -i  C:\Users\kiran\.ssh\id_rsa kiran@windows2012Server
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This should get you a powershell 7 console on the remote server named: windows2012Server&lt;/p&gt;

&lt;p&gt;You can verify using the "hostname" command.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If this works that means our ssh configuration is a success. we can proceed to the next step.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Testing SSH with PowerShell : Interactive
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Run this from the windows 10 client
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Test powershell ssh-based remoting with a domain user named kiran&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enter-PSSession -HostName windows2012Server -UserName kiran
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This should get you a powershell 7 console on the remote server named: windows2012Server&lt;/p&gt;

&lt;p&gt;You can verify using the "hostname" command.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Testing with SSH with PowerShell : Non-Interactive
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Run this from the windows 10 client
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a ps-session&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$session = New-PSSession -HostName windows2012Server -UserName kiran
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Username parameter can be omitted if you are remoting from the client that generated the rsa key.&lt;/p&gt;

&lt;p&gt;However if you copied the rsa key from one client to another and use it under a different login then username needs to be specified.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Test with invoke-command&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Invoke-Command -Session $session -ScriptBlock { hostname }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This should return the name of the remote computer : &lt;strong&gt;windows2012Server&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;blockquote&gt;
&lt;p&gt;PowerShell Remoting over SSH does not currently support remote endpoint configuration and JEA (Just Enough Administration)&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>powershell</category>
      <category>rsa</category>
      <category>security</category>
      <category>ssh</category>
    </item>
  </channel>
</rss>
