Key-based authentication in OpenSSH for Windows (2024)

  • Article
  • 7 minutes to read

Applies to Windows Server 2022, Windows Server 2019, Windows 10 (build 1809 and later)

Most authentication in Windows environments is done with a username-password pair, which works well for systems that share a common domain. When working across domains, such as between on-premises and cloud-hosted systems, it becomes vulnerable to brute force intrusions.

By comparison, Linux environments commonly use public-key/private-key pairs to drive authentication that doesn't require the use of guessable passwords. OpenSSH includes tools to help support key based authentication, specifically:

  • ssh-keygen for generating secure keys
  • ssh-agent and ssh-add for securely storing private keys
  • scp and sftp to securely copy public key files during initial use of a server

This document provides an overview of how to use these tools on Windows to begin using key-based authentication with SSH.If you're unfamiliar with SSH key management, we strongly recommend you review NIST document IR 7966 titled "Security of Interactive and Automated Access Management Using Secure Shell (SSH)".

About key pairs

Key pairs refer to the public and private key files that are used by certain authentication protocols.

SSH public key authentication uses asymmetric cryptographic algorithms to generate two key files – one "private" and the other "public". The private key files are the equivalent of a password, and should stay protected under all circ*mstances. If someone acquires your private key, they can sign in as you to any SSH server you have access to. The public key is what is placed on the SSH server, and may be shared without compromising the private key.

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.

Multi-factor authentication may be implemented with key pairs by entering a passphrase when the key pair is generated (see user key generation below). The user will be prompted for the passphrase during authentication. The passphrase is used along with the presence of the private key on the SSH client to authenticate the user.

Important

A remote session opened via key based authentication does not have associated user credentials andhence is not capable of outbound authentication as the user, this is by design.

Host key generation

Public keys have specific ACL requirements that, on Windows, equate to only allowing access to administrators and System. On first use of sshd, the key pair for the host will be automatically generated.

Important

You need to have OpenSSH Server installed first. Please see Getting started with OpenSSH.

By default the sshd service is set to start manually. To start it each time the server is rebooted, run the following commands from an elevated PowerShell prompt on your server:

# Set the sshd service to be started automaticallyGet-Service -Name sshd | Set-Service -StartupType Automatic# Now start the sshd serviceStart-Service sshd

Since there's no user associated with the sshd service, the host keys are stored under C:\ProgramData\ssh.

User key generation

To use key-based authentication, you first need to generate public/private key pairs for your client. ssh-keygen.exe is used to generate key files and the algorithms DSA, RSA, ECDSA, or Ed25519 can be specified. If no algorithm is specified, RSA is used. A strong algorithm and key length should be used, such as Ed25519 in this example.

To generate key files using the Ed25519 algorithm, run the following command from a PowerShell or cmd prompt on your client:

ssh-keygen -t ed25519

The output from the command should display the following output (where "username" is replaced by your username):

Generating public/private ed25519 key pair.Enter file in which to save the key (C:\Users\username/.ssh/id_ed25519):

You can press Enter to accept the default, or specify a path and/or filename where you would like your keys to be generated.At this point, you'll be prompted to use a passphrase to encrypt your private key files. The passphrase can be empty but it's not recommended.The passphrase works with the key file to provide two-factor authentication. For this example, we're leaving the passphrase empty.

Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in C:\Users\username/.ssh/id_ed25519.Your public key has been saved in C:\Users\username/.ssh/id_ed25519.pub.The key fingerprint is:SHA256:OIzc1yE7joL2Bzy8!gS0j8eGK7bYaH1FmF3sDuMeSj8 username@LOCAL-HOSTNAMEThe key's randomart image is:+--[ED25519 256]--+| . || o || . + + . || o B * = . || o= B S . || .=B O o || + =+% o || *oo.O.E ||+.o+=o. . |+----[SHA256]-----+

Now you have a public/private ed25519 key pair in the location specified. The .pub files are public keys, and files without an extension are private keys:

Mode LastWriteTime Length Name---- ------------- ------ -----a---- 6/3/2021 2:55 PM 464 ed25519-a---- 6/3/2021 2:55 PM 103 ed25519.pub

Remember that private key files are the equivalent of a password should be protected the same way you protect your password.Use ssh-agent to securely store the private keys within a Windows security context, associated with your Windows account. To start the ssh-agent service each time your computer is rebooted, and use ssh-add to store the private key run the following commands from an elevated PowerShell prompt on your server:

# By default the ssh-agent service is disabled. Configure it to start automatically.# Make sure you're running as an Administrator.Get-Service ssh-agent | Set-Service -StartupType Automatic# Start the serviceStart-Service ssh-agent# This should return a status of RunningGet-Service ssh-agent# Now load your key files into ssh-agentssh-add $env:USERPROFILE\.ssh\id_ed25519

Once you've added the key to the ssh-agent on your client, the ssh-agent will automatically retrieve the local private key and pass it to your SSH client.

Important

It is strongly recommended that you back up your private key to a secure location, then delete itfrom the local system, after adding it to ssh-agent. The private key cannot be retrieved fromthe agent providing a strong algorithm has been used, such as Ed25519 in this example. If you loseaccess to the private key, you will have to create a new key pair and update the public key on allsystems you interact with.

Deploying the public key

To use the user key that was created above, the contents of your public key (\.ssh\id_ed25519.pub) needs to be placed on the server into a text file. The name and location of the file depends on whether the user account is a member of the local administrators group or a standard user account. The following sections cover both standard and administrative users.

Standard user

The contents of your public key (\.ssh\id_ed25519.pub) needs to be placed on the server into a text file called authorized_keys in C:\Users\username\.ssh\. You can copy your public key using the OpenSSH scp secure file-transfer utility, or using a PowerShell to write the key to the file.

The example below copies the public key to the server (where "username" is replaced by your username). You'll need to use the password for the user account for the server initially.

# Get the public key file generated previously on your client$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ed25519.pub# Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server$remotePowershell = "powershell New-Item -Force -ItemType Directory -Path $env:USERPROFILE\.ssh; Add-Content -Force -Path $env:USERPROFILE\.ssh\authorized_keys -Value '$authorizedKey'"# Connect to your server and run the PowerShell using the $remotePowerShell variablessh username@domain1@contoso.com $remotePowershell

Administrative user

The contents of your public key (\.ssh\id_ed25519.pub) needs to be placed on the server into a text file called administrators_authorized_keys in C:\ProgramData\ssh\. You can copy your public key using the OpenSSH scp secure file-transfer utility, or using a PowerShell to write the key to the file. The ACL on this file needs to be configured to only allow access to administrators and System.

The example below copies the public key to the server and configures the ACL (where "username" isreplaced by your user name). You'll need to use the password for the user account for the serverinitially.

Note

This example shows the steps for creating the administrators_authorized_keys file. This onlyapplies to administrator accounts and must be user instead of the per user file within the user'sprofile location.

# Get the public key file generated previously on your client$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ed25519.pub# Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server$remotePowershell = "powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '$authorizedKey';icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""Administrators:F"" /grant ""SYSTEM:F"""# Connect to your server and run the PowerShell using the $remotePowerShell variablessh username@domain1@contoso.com $remotePowershell

These steps complete the configuration required to use key-based authentication with OpenSSH on Windows.Once the example PowerShell commands have been run, the user can connect to the sshd host from any client that has the private key.

Key-based authentication in OpenSSH for Windows (2024)

FAQs

How is key-based authentication implemented in OpenSSH? ›

User key generation

To use key-based authentication, you first need to generate public/private key pairs for your client. ssh-keygen.exe is used to generate key files and the algorithms DSA, RSA, ECDSA, or Ed25519 can be specified. If no algorithm is specified, RSA is used.

How do I authorize a SSH key in Windows? ›

For Windows 10 & 11
  1. Press the Windows key or open up the Start Menu. Type “cmd”.
  2. Under “Best Match”, click “Command Prompt”.
  3. In the command prompt, use the ssh-keygen command: ...
  4. The system will now generate the key pair and display the key fingerprint and a randomart image. ...
  5. Open your file explorer.

How do I add an authentication key to 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.

How to add private key to SSH agent Windows? ›

Adding your SSH key to the ssh-agent
  1. Start the ssh-agent in the background. $ eval "$(ssh-agent -s)" > Agent pid 59566. ...
  2. If you're using macOS Sierra 10.12. ...
  3. Add your SSH private key to the ssh-agent and store your passphrase in the keychain. ...
  4. Add the SSH key to your account on GitHub.

How to configure SSH key based authentication on a Windows server? ›

Here are the complete steps:
  1. Open the public key file in Notepad. ...
  2. Copy the contents of the file to clipboard. ...
  3. Connect to the server with Remote 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.
May 23, 2019

How to install SSH keygen on Windows? ›

  1. Generate an SSH key in Windows 10 with OpenSSH Client. Step 1: Verify if OpenSSH Client is Installed. Step 2: Open Command Prompt. Step 3: Use OpenSSH to Generate an SSH Key Pair.
  2. Generate SSH Keys Using PuTTY. Step 1: Install PuTTY. Step 2: Run the PuTTY SSH Key Generator. ...
  3. Using Your SSH Keys.
May 5, 2020

What is SSH key based authentication? ›

Remote connections to a server via Secure Shell (SSH) can be authenticated in two ways. The traditional and default method is to use password authentication. The second approach is key-based authentication, which is based on a private-public key pair.

How do I enable OpenSSH on Windows? ›

Install OpenSSH for Windows
  1. Open Settings, select Apps, then select Optional Features.
  2. Scan the list to see if the OpenSSH is already installed. ...
  3. Once setup completes, return to Apps and Optional Features and confirm OpenSSH is listed.
  4. Open the Services desktop app. ...
  5. In the details pane, double-click OpenSSH SSH Server.
Jan 11, 2023

What is OpenSSH authentication agent? ›

The SSH agent ( ssh-agent ) is an SSH key manager that stores the SSH key in a process memory so that users can log into SSH servers without having to type the key's passphrase every time they authenticate with the server.

What are the three authentication methods supported by SSH? ›

Client Side consists of three authentication methods.
  • Password Authentication. By default, the SSH components will attempt to use Password Authentication when authenticating to the server. ...
  • Public Key Authentication. ...
  • Keyboard Interactive Authentication. ...
  • Multi-step Authentication.

How do I set up authentication key? ›

Set up Authenticator
  1. On your Android device, go to your Google Account.
  2. At the top, tap the Security tab. If at first you don't get the Security tab, swipe through all tabs until you find it.
  3. Under "Signing in to Google," tap 2-Step Verification. ...
  4. Under "Authenticator app," tap Set up. ...
  5. Follow the on-screen steps.

What are the 4 SSH server authentication methods? ›

There are essentially four ways you can implement passwordless SSH access. SSH certificate-based authentication, SSH key-based authentication, SSH host-based authentication, or using a custom PAM module that supports out-of-band authentication.

How do I get a private key for OpenSSH? ›

Generating a Secure Shell (SSH) Public/Private Key Pair
  1. Navigate to your home directory: ...
  2. Run the ssh-keygen utility, providing as filename your choice of file name for the private key: ...
  3. Enter a passphrase for the private key, or press Enter to create a private key without a passphrase:

How do I log into my OpenSSH private key? ›

Step 1 Create and Copy Private Key to remote VM
  1. Copy private key to new file called centos7template01.txt.
  2. Type ls to verify file is there.
  3. Copy file to remote VM.
  4. Type yes to connect and transfer file.
  5. SSH into remote VM (Cent7-07)
  6. Type ls to confirm file copied successfully.

How do I change my RSA private key to OpenSSH? ›

Answer
  1. Open PuTTYgen, choose Key > SSH-2 RSA key, and select RSA in the lower left corner.
  2. Import the private key in OpenSSH format to PuTTYgen. Choose Conversions > Import key, select the private key in OpenSSH format, and open it.
  3. Choose Conversions > Export OpenSSH key, name and save the file.

How do I create a RSA public key in Windows? ›

Whether you use Command Prompt or Windows Terminal, type ssh-keygen and hit Enter. This will automatically generate the SSH keys. In our tests on Windows 11, it created a 2048-bit RSA key. If you'd like to use a different algorithm—GitHub recommends Ed25519, for example—then you'd type ssh-keygen -t ed25519 .

How to setup passwordless SSH login Windows? ›

These commands should work on the majority of Linux distributions, and instructions are included for modern client machines of the macOS, Windows, and Linux varieties.
  1. Step 1: Generate a key pair. ...
  2. Step 2: Create SSH directory on server. ...
  3. Step 3: Upload public key to remote server.
Dec 9, 2022

Does Windows have SSH agent? ›

SSH-Agent and OpenSSH are tools in Windows that can be used to authenticate to remote Git repositories, such as GitLab, GitHub, Azure DevOps, etc.

How to generate SSH key in Windows using PuTTYgen? ›

Generating an SSH key pair using PuTTY
  1. Start PuTTYgen by clicking Start > Programs > PuTTY > PuTTYgen. ...
  2. Click SSH-2 RSA as the type of key to generate. ...
  3. Click Generate and then move the cursor around the blank area of the Key section to generate the random characters that create a unique key.

Which authentication method is used by SSH? ›

The two widely used methods of SSH authentication for secure remote access are:
  1. Password authentication (using user name and passwords)
  2. Public key-based authentication (using public and private key pairs)

Why use SSH key instead of 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.

What is difference between SSH and OpenSSH? ›

SSH is based on a client-server architecture where the system the user is working on is the client and the remote system being managed is the server. OpenSSH includes a range of components and tools designed to provide a secure and straightforward approach to remote system administration.

Is OpenSSH for Windows secure? ›

It encrypts all traffic to eliminate eavesdropping, connection hijacking, and other attacks. In addition, OpenSSH provides a large suite of secure tunneling capabilities, several authentication methods, and sophisticated configuration options.

What is the difference between OpenSSH server and OpenSSH client? ›

The SSH client always initiates the setup of the secure connection, and the SSH server listens for incoming connection requests (usually on TCP port 22 on the host system) and responds to them.

What is OpenSSH authentication agent Windows 10? ›

Agent to hold private keys used for public key authentication. This service exists in Windows 10 only.

Can OpenSSH be hacked? ›

High volume SSH Key scanning attacks going undetected

Activity reported by web servers has proven attackers are exploiting SSH Keys to gain access to company data. Attackers can breach the perimeter in a number of ways, as they have been doing, but once they get in, they steal SSH Keys to advance the attack.

Does OpenSSH use rsa? ›

OpenSSH is a 100% complete SSH protocol 2.0 implementation and includes sftp client and server support. OpenSSH will disable the ssh-rsa signature scheme by default in the next release. In the SSH protocol, the "ssh-rsa" signature scheme uses the SHA-1 hash algorithm in conjunction with the RSA public key algorithm.

What is YubiKey for SSH authentication? ›

A YubiKey with OpenPGP can be used for logging in to remote SSH servers. In this setup, the Authentication subkey of an OpenPGP key is used as an SSH key to authenticate against a server. To ensure that the only way to log in is by using your YubiKey we recommend disabling password login on your SSH server.

What are the three 3 main types of authentication? ›

Authentication factors can be classified into three groups: something you know: a password or personal identification number (PIN); something you have: a token, such as bank card; something you are: biometrics, such as fingerprints and voice recognition.

How do you use key authentication? ›

The SSH public key authentication has four steps:
  1. Generate a private and public key, known as the key pair. ...
  2. Add the corresponding public key to the server.
  3. The server stores and marks the public key as approved.
  4. The server allows access to anyone who proves the ownership of the corresponding private key.
Aug 10, 2021

How does key authentication work? ›

And SSH key authentication is more convenient than password authentication. The keys connect users and processes to a server by initiating authentication and granting access automatically, so users don't have to remember or enter their password for each and every system.

How do I find my auth key? ›

Finding your auth key in Chrome
  1. The first step is simple, navigate to the Dlive website in Chrome and log in with your account.
  2. Once you're logged in, you now need to open the 'developer console', to do this, hit the 'F12' key on your keyboard.
  3. Great!

Which are the 3 ways of authenticating user identity? ›

There are three basic types of authentication. The first is knowledge-based — something like a password or PIN code that only the identified user would know. The second is property-based, meaning the user possesses an access card, key, key fob or authorized device unique to them. The third is biologically based.

Where is OpenSSH key? ›

By default, the keys are stored in the ~/. ssh directory with the filenames id_rsa for the private key and id_rsa. pub for the public key. Using the default locations allows your SSH client to automatically find your SSH keys when authenticating, so we recommend accepting them by pressing ENTER .

Where does OpenSSH look for keys? ›

How to set up public key authentication for OpenSSH. SSH keys are typically configured in an authorized_keys file in . ssh subdirectory in the user's home directory. Typically a system administrator would first create a key using ssh-keygen and then install it as an authorized key on a server using the ssh-copy-id tool ...

How to convert SSH2 private key to OpenSSH? ›

STEPS :
  1. Open the PuTTY Key Generator.
  2. On the menu bar, click "File" -> "Load private key"
  3. Select your .ppk file.
  4. On the menu bar, click "Conversions" -> "Export OpenSSH key"
  5. Save the file as (without an extension) e.g. mercurial_rsa `
Sep 6, 2020

What is OpenSSH key format? ›

An SSH2 public key in OpenSSH format will start with "ssh-rsa". The idea behind all of this is that once you have keys on the remote server and your local host, access will be simpler since the server will only grant access to someone who has the matching private key.

Do you use public or private key for SSH? ›

The SSH employs a public key cryptography. A public-key cryptography, also known as asymmetric cryptography, is a class of cryptographic algorithms which requires two separate keys, one of which is secret (or private) and one of which is public.

What is key based authentication in SSH? ›

SSH Key Based Authentication provides cryptographic security to the SSH server, where the encryption algorithm works with a Public Key and Private Key pair. The user is granted access to data on the SSH server by decrypting the user's Public Key on the SSH Server with a Private Key for successful Authentication.

How does SSH key verification work? ›

The most common means of authentication is via SSH asymmetric key pairs. The server uses the public key to encrypt a message and send it to the client. If the client has the correct private key, they can decrypt the message and send it back to the server for verification.

How does YubiKey work with SSH? ›

A YubiKey with OpenPGP can be used for logging in to remote SSH servers. In this setup, the Authentication subkey of an OpenPGP key is used as an SSH key to authenticate against a server. To ensure that the only way to log in is by using your YubiKey we recommend disabling password login on your SSH server.

Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 5708

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.