Key-based Authentication for OpenSSH on Windows (2024)

This is a follow up to a previous post:Getting Started with SSH on Windows Server 2019. If you haven’t yet installed and configuredSSH, start by reading that first.

Key-Based Authentication Overview

The previous post leaves off with SSH enabled and working with username and password authentication. If you want to enable key-based auth instead, you have to go through some additional steps to generate the keys and place them in the correct locations. If you are familiar with key-based auth for SSH to Linux servers, this process is very similar.

In case you aren’t already familiar with key-based authentication, it is a way of authenticating to remote servers without using a password. Instead, a private key stored on the client is paired with a public key stored on the server. When the client attempts to connect, the client and server communicate to ensure the private key that the client has matches an allowed public key on the server side. Since the private key is considerably more complex than a password, this offers greater security over basic username/password authentication.

Configuring the Client and Generating a Key Pair

The first step to using key-based authentication is to generate a key pair – a set of matching public and private keys. The OpenSSH client capability in Windows includes the tools required to do so. Follow this process using PowerShell from the client computer you will be connecting from

First, if not already installed, install the OpenSSH client. This will need to be done with a PowerShell window run as AdministratorAdd-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

Key-based Authentication for OpenSSH on Windows (1)

Next, switch to a regular (non-admin) PowerShell session and use the ssh-keygen command to generate a new key pair. When prompted, you can press enter to accept the defaults, or enter a custom path and name for the key files. You can also protect the private key with a passphrase. If you choose to use a passphrase, you’ll be prompted to enter it to decode the private key every time you connect to the server with SSH. This provides an additional layer of security.ssh-keygen

Key-based Authentication for OpenSSH on Windows (2)

By default, the ssh-keygen command will create two files in the user’s .ssh folder: id_rsa and id_rsa.pub. Id_rsa (without an extension) is the private key file, while id_rsa.pub contains the public key.

With the key created, next you must start the SSH Agent service which manages private keys locally and coordinates their usage in authentication. Once the service is started, use the ssh-add component to add the new key into the local key store.Start-Service ssh-agent ssh-add <path to new private key file>

Key-based Authentication for OpenSSH on Windows (3)

Configuring the Server

Finally, the public key of the key pair must manually be placed onto the server you will SSH to. This is easiest to do via copy/paste into a Remote Desktop session. The public key should be named authorized_keys and copied into the .ssh folder inside the profile folder of the user you are setting up. For example, c:\users\myuser\.ssh\authorized_keys. Note, if the user is in the local Administrators group on the server, the key must be placed in a different path. See the next section for more details.

  1. Open the public key file in Notepad. If using default path, it is C:\Users\myuser\.ssh\id_rsa.pub
  2. Copy the contents of the file to clipboard. Ensure you get the entire file.
  3. Connect to the server with Remote Desktop.
  4. Open Explorer and navigate to your profile folder on the server, such as C:\Users\myuser\
  5. Look for a .ssh folder. If one doesn’t exist, create it.
    1. Note, Windows Explorer won’t let you create the folder with the name “.ssh”. Instead, use “.ssh.” with an extra dot at the end. The extra dot will be removed, and you’ll have a folder correctly named .ssh
  6. In the .ssh folder, create a new text document named “authorized_keys” and open it with Notepad. If the file already exists, just open it.
    1. Note, this file has no extension. You may need to make file extensions visible to ensure you remove the .txt extension
  7. In Notepad, paste the key you copied earlier and save the file. If there was already a key in this file, paste your key onto a new line below the existing one.
Key-based Authentication for OpenSSH on Windows (4)
Key-based Authentication for OpenSSH on Windows (5)
Key-based Authentication for OpenSSH on Windows (6)

With all the pieces in place, you should be able to SSH to the server with no password required. If you configured a passphrase on your private key, you will have to enter that before you can connect. The SSH Agent service will automatically attempt to use the private key you added to the key stored during the connection attempt.ssh username@domain@servername #For example, to connect to a server named cntsapp01 as adminuser on the contoso.com domain, use: ssh adminuser@contoso.com@cntsapp01

Key-based Authentication for OpenSSH on Windows (7)
Key-based Authentication for OpenSSH on Windows (8)

Here, I got connected to my remote server without being prompted for a password. The hostname command verifies that my PowerShell window is connected to it via SSH.

Admin Users

If the user account on the server you are connecting to is in the local Administrators group, the public key must be placed in the C:\ProgramData\ssh\administrators_authorized_keys instead of the user’s .ssh folder. Additionally, only the Administrators group and SYSTEM account can have access to that file, for security purposes. After copying your key into the file and saving it, you can use this script to set the correct permissions on it. Here are the complete steps:

  1. Open the public key file in Notepad. If using default path, it is C:\Users\myuser\.ssh\id_rsa.pub
  2. Copy the contents of the file to clipboard. Ensure you get the entire file.
  3. Connect to the server withRemote Desktop.
  4. Open Notepad as administrator
  5. In Notepad, paste in the key you copied earlier
  6. Save the file as C:\ProgramData\ssh\administrators_authorized_keys with no extension. You may need to make file extensions visible to ensure you can remove the .txt extension.
  7. Use the below PowerShell script to set the correct permissions on the file

$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

If you don’t do this, and instead only place the file in the .ssh folder for the user, you’ll either get prompted for your password (instead of using the key file), or your connection will fail with “Too many authentication attempts”.

Key-based Authentication for OpenSSH on Windows (2024)

FAQs

What is key based authentication in OpenSSH? ›

Key-based authentication provides two primary benefits: Helps mitigate brute-force password attacks against SSH. Prevents administrators from being required to manually type passwords in automated processes such as scripts or Ansible.

What are the disadvantages of key based authentication SSH? ›

3 Drawbacks of keys

First, keys are more difficult to set up and maintain than passwords. You need to generate, distribute, and store your keys securely, and update them regularly. Second, keys can pose a security risk if they are lost, stolen, or compromised.

How do I install OpenSSH authentication agent on Windows? ›

Open Settings, select System, then select Optional Features. Scan the list to see if the OpenSSH is already installed. If not, at the top of the page, select View Features, then: Search for OpenSSH Client, select Next, then select Install.

What is key based authentication in Windows? ›

Key based authentication enables the SSH server and client to compare the public key for a user name provided against the private key. If the server-side public key can't be validated against the client-side private key, authentication fails.

How does key-based authentication work? ›

A Key Based Authentication is a more secure and encrypted method of authorization that allows a user to gain access to target resources with the help of secret keys that are stored and guarded in a secure location by the end user.

How to configure key based authentication for SSH? ›

Procedure
  1. Use the ssh-keygen tool to create a key pair. ...
  2. Validate that the keys were generated. ...
  3. Enable key-based authentication in the /etc/ssh directory on the SSH server. ...
  4. Copy the rsa. ...
  5. If you have an existing authorized_keys file, edit it to remove any no-pty restrictions.

Is SSH key based authentication better than password? ›

From a security standpoint, using SSH-keys to authenticate a user's identity leads to greater protection of your data. Username/password authentication can often lead to security compromises, in particular, brute force attacks by hackers.

Is key based authentication better than password authentication? ›

Why authenticate using SSH key instead of password? Undeniably, the main advantage of authentication using SSH public key over authentication using password would be security. No matter how long or complex a password is, it can never equate with the cryptographic strength that SSH public key offers.

Is SSH key based authentication Secure? ›

Public key authentication provides cryptographic strength that even extremely long passwords can not offer. With SSH, public key authentication improves security considerably as it frees the users from remembering complicated passwords (or worse yet, writing them down).

What is the OpenSSH authentication agent in Windows 10? ›

In this article

Applies to Windows Server 2022, Windows Server 2019, Windows 10 (build 1809 and later) OpenSSH is the open-source version of the Secure Shell (SSH) tools used by administrators of Linux and other non-Windows for cross-platform management of remote systems.

How to enable OpenSSH authentication agent in Windows 10? ›

Set up a Windows 10 SSH server:
  1. Open 'Settings' > 'Apps' > 'Apps & Features' > 'Optional Features'.
  2. Select 'Add Features' and 'OpenSSH Server' and 'Install' (admin rights required).
  3. Set the start-up type for 'OpenSSH Authentication Agent' and 'OpenSSH Server' to 'Automatic' in the Windows 'Services' app.
Oct 4, 2023

Where is the SSH key stored in Windows? ›

For Windows 10 & 11

In the command prompt, use the ssh-keygen command: By default, the system will save the keys to [your home directory]/.ssh/id_rsa.

What is key based authentication vs certificate authentication? ›

While SSH Key-based authentication uses public key cryptography to operate, SSH Certificate-based authentication simply attaches a signed certificate to each key to verify their identities.

What is the difference between basic authentication and Windows authentication? ›

Windows authentication authenticates the user by validating the credentials against the user account in a Windows domain. Basic authentication verifies the credentials that are provided in a form against the user account that is stored in a database.

What is form based authentication vs Windows based authentication? ›

Forms authentication is where the user is required to login with credentials just for the web site. Windows authentication is for when the web site will accept the user's Windows credentials for login purposes.

What is the difference between SSH host key and authentication key? ›

SSH client and host keys are used when an SFTP client attempts to connect and authenticate with an SFTP server. Client keys authenticate the user connecting to an SFTP server. Host keys ensure that the SFTP client is connected to the correct SFTP server.

What are the three authentication methods supported by SSH? ›

The SSH server supports three types of user authentication methods and sends these authentication methods to the SSH client in the following predefined order:
  • Public-key authentication method.
  • Keyboard-interactive authentication method.
  • Password authentication method.

Is SSH key-based authentication Secure? ›

Public key authentication provides cryptographic strength that even extremely long passwords can not offer. With SSH, public key authentication improves security considerably as it frees the users from remembering complicated passwords (or worse yet, writing them down).

What is key-based authentication vs certificate authentication? ›

While SSH Key-based authentication uses public key cryptography to operate, SSH Certificate-based authentication simply attaches a signed certificate to each key to verify their identities.

Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6305

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.