Installing OpenSSH server in Windows 10/11
Microsoft finally got around to allowing SSH access into Windows 10.
In a typical Microsoft way, they provide a way to do so using the GUI, and tried to hid the way to do so via the command line.
I wanted a command line way of doing it, so I could easily script it for any Windows 10 device I had to deploy, especially in my lab.
It needs to be done as an Administrator, so the script had to check to see if it was run using Administrator privileges, then install OpenSSH server.
I had to divide it into two different scripts:
-
The first script, a simple .bat file, to check if it was run with Administrator privileges
-
The second script run in Powershell to actually install OpenSSH server, start it, and add it to run on boot up.
I usually copy the first script (.bat) to the Desktop, and the second script (.ps1) to ~/Downloads.
To run/install OpenSSH, right click on the .bat file, and select 'Run as Administrator'
Assuming you ran the script with Administrator privileges, the script will install OpenSSH server.
End result is that the OpenSSH server is now installed.
The first script, a simple .bat file, which I keep on the Desktop, as Install-OpenSSH-server.bat:
@echo off
:: Running 'net session' will result in an error code other than 0 if not run as an Administrator
net session >nul 2>&1
if %errorLevel% == 0 (
echo Script run as administrator...installing OpenSSH-Server
powershell -ExecutionPolicy Bypass -File c:\users\tom\downloads\Install-OpenSSH-server.ps1
) else (
echo Script must be run as Administrator!
)
@pause
The second script, a Powershell script, actually installs the OpenSSH server.
This I keep in \Users\tom\Downloads\Install-OpenSSH-server.ps1
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
Update: This procedure also works for Windows 11
If installing in Windows 11, be sure to either disable the 'Public Network' firewall, or allow SSH (port 22) through the firewall.
References
Get started with OpenSSH for Windows