How to Generate SSH Key in Windows 10?


Introduction

SSH, short for Secure Shell, is an encryption protocol that provides a secure connection between two computers over an insecure network, such as the internet. It allows users to remotely and securely access and control another computer or server. SSH works by encrypting all data sent between the two computers, including any usernames and passwords used to log in.

In order to establish a secure SSH connection between two computers or servers, SSH keys are necessary. An SSH key is a pair of cryptographic keys - one public and one private.

The private key is kept on the user's local computer, while the public key is uploaded to remote servers or computers being accessed. The public key acts as identification for the user's local device when attempting to connect securely with another device over SSH.

Understanding SSH Key Generation Process

What is an SSH key?

An SSH key, also called a public-key cryptography key, is a cryptographic code used to secure communication between two machines. It consists of two keys - a private key that is stored on your local machine and a public key that you share with the remote server. These keys work in tandem to establish secure communication by encrypting and decrypting data.

How does an SSH key work?

When you attempt to connect to a remote server using SSH, your local machine sends the server its public key for authentication. If the server recognizes and trusts your public key, it will generate a unique encrypted message that can only be decrypted using your private key stored on your local machine.

Why do you need to generate an SSH key?

Generating an SSH key is essential when working with remote servers as it enables secure communication between machines without the need for passwords. Passwords can be compromised or stolen, but since SSH keys are unique codes specific to each user/machine combination, they offer more robust security than traditional password-based authentication methods.

Preparing to Generate an SSH Key in Windows 10

Checking if OpenSSH client is installed on your system.

Before generating an SSH key, you need to make sure that the OpenSSH client is installed on your Windows 10 system. To check if it is already installed, you can use the following steps −

  • Open PowerShell as an administrator by pressing the "Windows + X" keys and selecting "Windows PowerShell (Admin)" from the options.

  • Once PowerShell opens, type the command `ssh` and press Enter.

  • If OpenSSH client is already installed, you will see a list of its commands and their usage.

If it is not yet installed, you will receive an error message stating that `ssh` command is not recognized.

Installing OpenSSH client if it's not already installed.

To install OpenSSH client on Windows 10 −

  • Go to "Settings" by clicking the gear icon in the Start Menu or pressing "Windows + I".

  • Click on "Apps".

  • Scroll down and click on "Optional features".

  • Click "+ Add a feature".

  • Find and select "OpenSSH Client" from the list.

  • Click Install.

The installation process may take a few minutes depending on your internet speed and system specifications. Once completed, you can verify that OpenSSH Client has been successfully installed by following step 2 above in checking if it's already installed - this time there should be no errors returned; instead, a list of its available commands would be displayed indicating that it has been properly set up in your Windows 10 machine.

Generating the SSH Key in Windows 10

After ensuring that the OpenSSH client is installed in your Windows 10 system, you can proceed to generate the SSH key. This process involves using PowerShell, a command-line interface tool for Windows, to generate the key pair. The following steps will guide you through the process −

Launching PowerShell as Administrator

To launch PowerShell as an administrator −

  • Press the Start button and search for "PowerShell".

  • Right-click on "Windows PowerShell" and select "Run as Administrator".

Once you're in PowerShell as an administrator, you'll be able to execute commands that require elevated privileges.

Typing the Command to Generate the Key Pair

In PowerShell, type the following command to generate your SSH key pair −

ssh-keygen -t rsa -b 4096 

This command will create a new RSA (Rivest-Shamir-Adleman) key pair with a bit length of 4096, which is more secure than shorter keys. You can adjust this value up or down based on your needs.

Choosing a Location for Saving the Key Pair

After typing in the command above, PowerShell will prompt you to enter a file location where your key pair will be stored. You can accept the default location by pressing Enter or specify your own directory path.

For example −

Enter file in which to save the key (/c/Users/username/.ssh/id_rsa): 

By default, SSH keys are saved under `C:\Users\.ssh`. You can accept this path by pressing Enter or enter a different file location if desired.

Setting a Passphrase for Added Security

You'll then be prompted to enter an optional passphrase for added security. A passphrase is like a password that protects your private key.

If someone gets a hold of your private key, they won't be able to use it without knowing the passphrase.

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

You can choose to set a complex and unique passphrase or leave it blank for no passphrase.

Confirming That the Key Has Been Generated Successfully

After entering your preferred file location and optional passphrase, PowerShell will generate the key pair and display a confirmation message −

Your identification has been saved in /[filepath]/id_rsa. 
Your public key has been saved in /[filepath]/id_rsa.pub. The key fingerprint is: 
[unique code] The key's randomart image is: 
+--[ RSA 4096]----+ |[ascii art] | +-----------------+ 

Adding Your Public Key to Remote Server(s)

Copying Your Public Key from Your Local Machine

There are two ways to copy your public key from your local machine: using a PowerShell command or manually copying it using Notepad or any other text editor.

To copy your public key using PowerShell, first open PowerShell as an Administrator. Then run the following command −

powershell Get-Content ~/.ssh/id_rsa.pub | ssh user@host 'cat >> .ssh/authorized_keys' 

Replace "user" and "host" with the username and IP address of the remote server that you want to access. This command will copy your public key from your local machine and append it to the authorized_keys file on the remote server.

Pasting Your Public Key into Remote Server(s)

Once you have copied your public key either through PowerShell or manually, the final step is pasting it into each remote server's authorized_keys file so that they recognize and accept connections from this specific SSH key pair. Most Linux servers will have this file located in `~/.ssh/authorized_keys`.

If this file does not exist already, create one at that location. Now, use an SFTP client or a tool like PuTTY's PSCP utility (which allows secure copying of files between two computers) to copy the public key to the remote server.

Conclusion

Generating an SSH key pair and adding your public key to remote servers is a crucial step for secure and efficient communication between your computer and remote servers.

The process detailed above outlines how you can easily generate an SSH key pair on Windows 10 using OpenSSH client, add your public key to remote servers either by copying it through PowerShell or manually copying it using text editors like Notepad, and finally pasting your public keys into respective authorized_keys files in each of these remote servers.

Updated on: 06-Jun-2023

426 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements