Windows/SSH: Difference between revisions

From Omnia
Jump to navigation Jump to search
Line 11: Line 11:
  # Windows Server 2022:
  # Windows Server 2022:
<pre>
<pre>
Add-WindowsCapability -Online -Name OpenSSH.Server~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Client~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
</pre>
</pre>



Revision as of 17:45, 12 November 2026

Enable SSH

Check installed (powershell):

Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Server*'
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH.Client*'

Install service (powershell):

Add-WindowsCapability -Online -Name OpenSSH.Server*
Add-WindowsCapability -Online -Name OpenSSH.Client*
# Windows Server 2022:
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

Verify Service:

Get-Service sshd

Check if is set to auto start: (does not by default)

Get-Service sshd | Select-Object Name, Status, StartType

Set SSH Service to Start Automatically: (Optional)

Set-Service -Name sshd -StartupType 'Automatic'

Start service (powershell): (as needed)

Start-Service sshd
Stop-Service sshd
Restart-Service sshd

Enable Firewall Rule (powershell): (as needed)

# check firewall rule - should have been auto created
Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP"
# Check if our firewall rule below has been added
Get-NetFirewallRule -Name "OpenSSH Server (sshd)"
# search for any (not case sensitive)
Get-NetFirewallRule -Name "*ssh*"
Get-NetFirewallRule -DisplayName "*ssh*"
# if need to create a new one:
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

References:

Chocolatey

Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))


$env:chocolateyUseWindowsCompression = 'true'
choco install -y openssh -params '"/SSHServerFeature"'

Config

C:\ProgramData\ssh
C:\ProgramData\ssh\sshd_config ::
 AuthorizedKeysFile	.ssh/authorized_keys

This line causes the user's .ssh/authorized_keys file to be ignored, if they are in the Administrators group:

Match Group administrators
       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys


To fix this, comment out these "Matched" lines, then run:

Restart-Service sshd

SSH Keys

For standard users:

%USERPROFILE%/.ssh/authorized_keys

For users who are administrators, they must put their keys in

C:\ProgramData\ssh\administrators_authorized_keys
%PROGRAMDATA%\ssh\administrators_authorized_keys
%ALLUSERSPROFILE%\ssh\administrators_authorized_keys


Fix the permissions with Powershell:

$acl = Get-Acl C:\ProgramData\ssh\administrators_authorized_keys
$acl.SetAccessRuleProtection($true, $false)
$administratorsRule = New-Object system.security.accesscontrol.filesystemaccessrule("Administrators","FullControl","Allow")
$systemRule = New-Object system.security.accesscontrol.filesystemaccessrule("SYSTEM","FullControl","Allow")
$acl.SetAccessRule($administratorsRule)
$acl.SetAccessRule($systemRule)
$acl | Set-Acl

ref: https://www.concurrency.com/blog/may-2019/key-based-authentication-for-openssh-on-windows

Ansible

- name: Install openssh
  win_chocolatey:
    name: openssh
    package_params: /SSHServerFeature
    state: present
  tags: openssh

ref: https://curiousdba.netlify.app/post/windowsopenssh/