How do I set the host name and port in a config file for Windows, using OpenSSH through PowerShell?

As on Unix/Linux:

Edit or create the file now by typing:

nano ~/.ssh/config

In here, you can set host-specific configuration options. To specify your new port, use a format like this:

Host remote_alias HostName remote_host Port port_num

This will allow you to log in without specifying the specific port number on the command line.



The OpenSSH configuration and key files (including the config, known_hosts, authorized_keys, id_rsa, etc.), which on *nix go to ~/.ssh, on Win32-OpenSSH they go to %USERPROFILE%\.ssh.

That typically is:

  • All I have in this folder is known_hosts – Mark Deven Jan 20 at 14:30
  • @MarkDeven I never wrote that the files appear their on their own. You have to create them. – Martin Prikryl Jan 20 at 15:43

After OpenSSH installs, perform some additional configuration steps.

Ensure that the OpenSSH folder is included on the system path environment variable:

C:\Windows\System32\OpenSSH\ if installed as the Windows optional feature C:\Program Files\OpenSSH\ if installed via the OpenSSH download Set the two services to start automatically:

Set-Service sshd -StartupType Automatic

Set-Service ssh-agent -StartupType Automatic If you installed OpenSSH with the optional feature, then Windows creates a new firewall rule to allow inbound access of SSH over port 22. If you installed OpenSSH from the download, then create the firewall rule with this command:

New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' `

-Enabled True -Direction Inbound -Protocol TCP `

-Action Allow -LocalPort 22 Start the sshd service to generate the SSH keys:

Start-Service sshd The SSH keys and configuration file reside in C:\ProgramData\ssh, which is a hidden folder. The default shell used by SSH is the Windows command shell. This needs to change to PowerShell:

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell `

-Value "C:\Program Files\PowerShell\6\pwsh.exe" -PropertyType String -Force Now, when you connect to the system over SSH, PowerShell Core will start and will be the default shell. You can also make the default shell Windows PowerShell if desired.

There's a bug in OpenSSH on Windows. It doesn't work with paths with a space, such as the path to the PowerShell Core executable! The workaround is to create a symbolic link that creates a path that OpenSSH can use:

New-Item -ItemType SymbolicLink -Path C:\pwsh -Target 'C:\Program Files\PowerShell\6' In the sshd_config file, un-comment the following lines:

PubkeyAuthentication yes

PasswordAuthentication yes Add this line before other subsystem lines:

Subsystem powershell C:\pwsh\pwsh.exe -sshs -NoLogo -NoProfile This tells OpenSSH to run PowerShell Core.

Comment out the line:

AuthorizedKeysFile PROGRAMDATA/ssh/administrators_authorized_keys After saving the changes to the sshd_config file, restart the services:

Restart-Service sshd

Start-Service ssh-agent You need to restart the sshd service after any change to the config file.

  • 1
    How does this answer the question? – Martin Prikryl Apr 1 '20 at 11:25
  • I think he's giving directions on configuring a server which accepts ssh connections. – Thufir Aug 24 '20 at 9:54
  • 2
    -1: If the answer to the question is there, it's too hard to find. – quant Nov 1 '20 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.