What is ssh-keygen & How to Use It to Generate a New SSH Key? (2024)

This page is about the OpenSSH version of ssh-keygen. For Tectia SSH, see here. If you wish to generate keys for PuTTY, see PuTTYgen on Windows or PuTTYgen on Linux.

Contents

What Is ssh-keygen? SSH Keys and Public Key Authentication Creating an SSH Key Pair for User Authentication Choosing an Algorithm and Key Size Specifying the File Name Copying the Public Key to the Server Adding the Key to SSH Agent Creating Host Keys Using X.509 Certificates for Host Authentication Using OpenSSH's Proprietary Certificates Key Management Requires Attention Make Sure There Is Enough Randomness General Purpose Systems Embedded Devices and Internet of Things Command and Option Summary

What Is ssh-keygen?

Ssh-keygen is a tool for creating new authentication key pairs for SSH. Such key pairs are used for automating logins, single sign-on, and for authenticating hosts.

SSH Keys and Public Key Authentication

The SSH protocol uses public key cryptography for authenticating hosts and users. The authentication keys, called SSH keys, are created using the keygen program.

SSH introduced public key authentication as a more secure alternative to the older .rhosts authentication. It improved security by avoiding the need to have password stored in files, and eliminated the possibility of a compromised server stealing the user's password.

However, SSH keys are authentication credentials just like passwords. Thus, they must be managed somewhat analogously to user names and passwords. They should have a proper termination process so that keys are removed when no longer needed.

What is ssh-keygen & How to Use It to Generate a New SSH Key? (1)

Creating an SSH Key Pair for User Authentication

The simplest way to generate a key pair is to run ssh-keygen without arguments. In this case, it will prompt for the file in which to store keys. Here's an example:

klar (11:39) ~>ssh-keygen Generating public/private rsa key pair. 
Enter file in which to save the key (/home/ylo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): Enter same passphrase again:
Your identification has been saved in /home/ylo/.ssh/id_rsa.
Your public key has been saved in /home/ylo/.ssh/id_rsa.pub.
The key fingerprint is: SHA256:Up6KjbnEV4Hgfo75YM393QdQsK3Z0aTNBz0DoirrW+c ylo@klar
The key's randomart image is:
+---[RSA 2048]----+ | . ..oo..| | . . . . .o.X.| | . . o. ..+ B| 
| . o.o .+ ..| | ..o.S o.. | | . %o= . | | @.B... . |
| o.=. o. . . .| | .oo E. . .. | +----[SHA256]-----+ klar (11:40) ~>

First, the tool asked where to save the file. SSH keys for user authentication are usually stored in the user's .ssh directory under the home directory. However, in enterprise environments, the location is often different. The default key file name depends on the algorithm, in this case id_rsa when using the default RSA algorithm. It could also be, for example, id_dsa or id_ecdsa.

Then it asks to enter a passphrase. The passphrase is used for encrypting the key, so that it cannot be used even if someone obtains the private key file. The passphrase should be cryptographically strong. Our online random password generator is one possible tool for generating strong passphrases.

Choosing an Algorithm and Key Size

SSH supports several public key algorithms for authentication keys. These include:

  • rsa - an old algorithm based on the difficulty of factoring large numbers. A key size of at least 2048 bits is recommended for RSA; 4096 bits is better. RSA is getting old and significant advances are being made in factoring. Choosing a different algorithm may be advisable. It is quite possible the RSA algorithm will become practically breakable in the foreseeable future. All SSH clients support this algorithm.

  • dsa - an old US government Digital Signature Algorithm. It is based on the difficulty of computing discrete logarithms. A key size of 1024 would normally be used with it. DSA in its original form is no longer recommended.

  • ecdsa - a new Digital Signature Algorithm standarized by the US government, using elliptic curves. This is probably a good algorithm for current applications. Only three key sizes are supported: 256, 384, and 521 (sic!) bits. We would recommend always using it with 521 bits, since the keys are still small and probably more secure than the smaller keys (even though they should be safe as well). Most SSH clients now support this algorithm.

  • ed25519 - this is a new algorithm added in OpenSSH. Support for it in clients is not yet universal. Thus its use in general purpose applications may not yet be advisable.

The algorithm is selected using the -t option and key size using the -b option. The following commands illustrate:

ssh-keygen -t rsa -b 4096
ssh-keygen -t dsa
ssh-keygen -t ecdsa -b 521
ssh-keygen -t ed25519

Specifying the File Name

Normally, the tool prompts for the file in which to store the key. However, it can also be specified on the command line using the -f <filename> option.

ssh-keygen -f ~/tatu-key-ecdsa -t ecdsa -b 521

Copying the Public Key to the Server

To use public key authentication, the public key must be copied to a server and installed in an authorized_keys file. This can be conveniently done using the ssh-copy-id tool. Like this:

ssh-copy-id -i ~/.ssh/tatu-key-ecdsa user@host

Once the public key has been configured on the server, the server will allow any connecting user that has the private key to log in. During the login process, the client proves possession of the private key by digitally signing the key exchange.

Adding the Key to SSH Agent

ssh-agent is a program that can hold a user's private key, so that the private key passphrase only needs to be supplied once. A connection to the agent can also be forwarded when logging into a server, allowing SSH commands on the server to use the agent running on the user's desktop.

For more information on using and configuring the SSH agent, see the ssh-agent page.

Creating Host Keys

The tool is also used for creating host authentication keys. Host keys are stored in the /etc/ssh/ directory.

Host keys are just ordinary SSH key pairs. Each host can have one host key for each algorithm. The host keys are almost always stored in the following files:

/etc/ssh/ssh_host_dsa_key 
/etc/ssh/ssh_host_ecdsa_key 
/etc/ssh/ssh_host_ed25519_key 
/etc/ssh/ssh_host_rsa_key

The host keys are usually automatically generated when an SSH server is installed. They can be regenerated at any time. However, if host keys are changed, clients may warn about changed keys. Changed keys are also reported when someone tries to perform a man-in-the-middle attack. Thus it is not advisable to train your users to blindly accept them. Changing the keys is thus either best done using an SSH key management tool that also changes them on clients, or using certificates.

What is ssh-keygen & How to Use It to Generate a New SSH Key? (2)Using X.509 Certificates for Host Authentication

OpenSSH does not support X.509 certificates. Tectia SSH does support them. X.509 certificates are widely used in larger organizations for making it easy to change host keys on a period basis while avoiding unnecessary warnings from clients. They also allow using strict host key checking, which means that the clients will outright refuse a connection if the host key has changed.

Using OpenSSH's Proprietary Certificates

OpenSSH has its own proprietary certificate format, which can be used for signing host certificates or user certificates. For user authentication, the lack of highly secure certificate authorities combined with the inability to audit who can access a server by inspecting the server makes us recommend against using OpenSSH certificates for user authentication.

However, OpenSSH certificates can be very useful for server authentication and can achieve similar benefits as the standard X.509 certificates. However, they need their own infrastructure for certificate issuance.

Key Management Requires Attention

It is easy to create and configure new SSH keys. In the default configuration, OpenSSH allows any user to configure new keys. The keys are permanent access credentials that remain valid even after the user's account has been deleted.

In organizations with more than a few dozen users, SSH keys easily accumulate on servers and service accounts over the years. We have seen enterprises with several million keys granting access to their production servers. It only takes one leaked, stolen, or misconfigured key to gain access.

In any larger organization, use of SSH key management solutions is almost necessary. SSH keys should also be moved to root-owned locations with proper provisioning and termination processes. For more information, see how to manage SSH keys. A widely used SSH key management tool for OpenSSH is Universal SSH Key Manager.

Practically all cybersecurity regulatory frameworks require managing who can access what. SSH keys grant access, and fall under this requirement. This, organizations under compliance mandates are required to implement proper management processes for the keys. NIST IR 7966 is a good starting point.

Make Sure There Is Enough Randomness

It is important to ensure there is enough unpredictable entropy in the system when SSH keys are generated. There have been incidents when thousands of devices on the Internet have shared the same host key when they were improperly configured to generate the key without proper randomness.

General Purpose Systems

On general purpose computers, randomness for SSH key generation is usually not a problem. It may be something of an issue when initially installing the SSH server and generating host keys, and only people building new Linux distributions or SSH installation packages generally need to worry about it.

Our recommendation is to collect randomness during the whole installation of the operating system, save that randomness in a random seed file. Then boot the system, collect some more randomness during the boot, mix in the saved randomness from the seed file, and only then generate the host keys. This maximizes the use of the available randomness. And make sure the random seed file is periodically updated, in particular make sure that it is updated after generating the SSH host keys.

Many modern general-purpose CPUs also have hardware random number generators. This helps a lot with this problem. The best practice is to collect some entropy in other ways, still keep it in a random seed file, and mix in some entropy from the hardware random number generator. This way, even if one of them is compromised somehow, the other source of randomness should keep the keys secure.

Embedded Devices and Internet of Things

Available entropy can be a real problem on small IoT devices that don't have much other activity on the system. They may just not have the mechanical randomness from disk drive mechanical movement timings, user-caused interrupts, or network traffic. Furthermore, embedded devices often run on low-end processors that may not have a hardware random number generator.

The availability of entropy is also critically important when such devices generate keys for HTTPS.

Our recommendation is that such devices should have a hardware random number generator. If the CPU does not have one, it should be built onto the motherboard. The cost is rather small.

Command and Option Summary

Here's a summary of commonly used options to the keygen tool:

-b “Bits” This option specifies the number of bits in the key. The regulations that govern the use case for SSH may require a specific key length to be used. In general, 2048 bits is considered to be sufficient for RSA keys.

-e “Export” This option allows reformatting of existing keys between the OpenSSH key file format and the format documented in RFC 4716, “SSH Public Key File Format”.

-p “Change the passphrase” This option allows changing the passphrase of a private key file with [-P old_passphrase] and [-N new_passphrase], [-f keyfile].

-t “Type” This option specifies the type of key to be created. Commonly used values are: - rsa for RSA keys - dsa for DSA keys - ecdsa for elliptic curve DSA keys

-i "Input" When ssh-keygen is required to access an existing key, this option designates the file.

-f "File" Specifies name of the file in which to store the created key.

-N "New" Provides a new passphrase for the key.

-P "Passphrase" Provides the (old) passphrase when reading a key.

-c "Comment" Changes the comment for a keyfile.

-p Change the passphrase of a private key file.

-q Silence ssh-keygen.

-v Verbose mode.

-l "Fingerprint" Print the fingerprint of the specified public key.

-B "Bubble babble" Shows a "bubble babble" (Tectia format) fingerprint of a keyfile.

-F Search for a specified hostname in a known_hosts file.

-R Remove all keys belonging to a hostname from a known_hosts file.

-y Read a private OpenSSH format file and print an OpenSSH public key to stdout.

This only listed the most commonly used options. For full usage, including the more exotic and special-purpose options, use the man ssh-keygen command.

What is ssh-keygen & How to Use It to Generate a New SSH Key? (3)

What is ssh-keygen & How to Use It to Generate a New SSH Key? (2024)

FAQs

How to generate SSH key using keygen? ›

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 generate a new SSH key for my host? ›

To generate an SSH key on your Linux server, run the command ssh-keygen . The command can take flags if you would like to customize the type of key that is generated and the signing algorithms that are used to generate the key. This example generates a standard 2048-bit RSA key without a passphrase.

Do I need to generate a new SSH key every time? ›

If your key has a passphrase and you don't want to enter the passphrase every time you use the key, you can add your key to the SSH agent. The SSH agent manages your SSH keys and remembers your passphrase. If you don't already have an SSH key, you must generate a new SSH key to use for authentication.

What is the purpose of the ssh-keygen command? ›

ssh-keygen is able to generate a key using one of three different digital signature algorithms. With the help of the ssh-keygen tool, a user can create passphrase keys for any of these key types.

Where is key in ssh-keygen? ›

ssh-keygen without a password

By default, your private and public keys are saved in your ~/. ssh/id_rsa and ~/. ssh/id_rsa. pub files, respectively.

What does generating an SSH key do? ›

The Secure Shell keys are encrypted files that authenticate and establish secure communication between clients and servers. It provides faster access than traditional password-based authentication. These keys are generated in pairs: A public key that is shared with the server.

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

SSH host keys serve as the default SSH server identification for connecting SSH clients. They are the default machine identity generated when an SSH server is installed. Analogous to user SSH keys, host keys represent the server's identity and are used for authentication towards the connecting client.

How do I generate a modern SSH key? ›

You can generate keys with the 'ssh-keygen' command: $ ssh-keygen -t ed25519 Generating public/private ed25519 key pair. Enter file in which to save the key ($HOME/. ssh/id_ed25519): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in $HOME/.

How is SSH host key generated? ›

Host keys are normally generated automatically when OpenSSH is first installed or when the computer is first booted. The ssh-keygen program can be used for generating additional host keys or for replacing existing keys.

How do I know if my SSH key is generated? ›

Checking for existing SSH keys
  1. Open Terminal .
  2. Enter ls -al ~/.ssh to see if existing SSH keys are present. $ ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist.
  3. Check the directory listing to see if you already have a public SSH key. ...
  4. Either generate a new SSH key or upload an existing key.

What is the best option for ssh-keygen? ›

To generate a robust SSH key, you have two main options: ED25519 and RSA. Both have their advantages, but ED25519 is generally recommended for its security and performance benefits.

What is the best SSH key type to use? ›

We strongly recommend using only the ed25519 algorithm (an ECDSA variant). It is the most secure SSH key type widely available, and is very well supported in the majority of systems. If you are using an client or server without ed25519 support, you should consider upgrading where possible.

Why keygen is used? ›

A key generator (key-gen) is a computer program that generates a product licensing key, such as a serial number, necessary to activate for use of a software application.

Do I need a passphrase for ssh-keygen? ›

SSH passphrases protect your private key from being used by someone who doesn't know the passphrase. Without a passphrase, anyone who gains access to your computer has the potential to copy your private key. For example, family members, coworkers, system administrators, and hostile actors could gain access.

How do I generate a SSH key for a remote server? ›

Copy the ssh key into remote servers
  1. Open a terminal on your local computer.
  2. Generate an SSH key pair if you haven't already done so by running the command: ssh-keygen . ...
  3. Once the key pair is generated, run the command: ssh-copy-id user@remote_server . ...
  4. You'll be prompted to enter the password for the remote user account.
Apr 8, 2023

How to generate ssh-keygen id_ed25519? ›

You can generate keys with the 'ssh-keygen' command: $ ssh-keygen -t ed25519 Generating public/private ed25519 key pair. Enter file in which to save the key ($HOME/. ssh/id_ed25519): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in $HOME/.

How to generate ECDSA key using ssh-keygen? ›

On your local computer, using a terminal application, call the following command to create a pair of keys: ssh-keygen -t ecdsa -b 521 . This creates a 521 bit long key using the ecdsa Digital Signature Algorithm. . Hit when asked which file to create and make sure that you provide a sufficiently long passphrase.

Is it possible to use ssh-keygen to create an SSH key without a password? ›

Use ssh-keygen to generate a key pair consisting of a public key and a private key on the client computer. This command can be run on any modern Linux client distribution, the Terminal in macOS, or in the Command Prompt in Windows 10/11. The -t rsa option specifies that the type of the key should be RSA.

How to generate PEM file using ssh-keygen? ›

SSH | Keys generation
  1. 1) Install Git for Windows or any tool containing ssh keygen.
  2. 2) Generate your private key using ssh-keygen. Type in Git Bash. ssh-keygen -t rsa.
  3. 3) Generate your public key using ssh-keygen with .pem format (-m pem). Type in Git Bash. ...
  4. You are good to go.

Top Articles
Frame Rate: a Beginner’s Guide | The TechSmith Blog
The Rule of Three in Multi-Timeframe Analysis | Real Trading
Mcgeorge Academic Calendar
Danielle Moodie-Mills Net Worth
Farepay Login
Nm Remote Access
Acbl Homeport
William Spencer Funeral Home Portland Indiana
Oppenheimer Showtimes Near Cinemark Denton
What is Cyber Big Game Hunting? - CrowdStrike
Nwi Arrests Lake County
Becu Turbotax Discount Code
Gino Jennings Live Stream Today
Craftology East Peoria Il
Second Chance Maryland Lottery
Google Flights Missoula
Traveling Merchants Tack Diablo 4
Our History
Where Is George The Pet Collector
Daytonaskipthegames
Shopmonsterus Reviews
Cbssports Rankings
Shiftselect Carolinas
Nz Herald Obituary Notices
Johnnie Walker Double Black Costco
Inkwell, pen rests and nib boxes made of pewter, glass and porcelain.
Masterbuilt Gravity Fan Not Working
Expression&nbsp;Home&nbsp;XP-452 | Grand public | Imprimantes jet d'encre | Imprimantes | Produits | Epson France
John Deere 44 Snowblower Parts Manual
The Menu Showtimes Near Amc Classic Pekin 14
Beth Moore 2023
CVS Near Me | Somersworth, NH
Studio 22 Nashville Review
Poe Flameblast
Koninklijk Theater Tuschinski
Ticket To Paradise Showtimes Near Regal Citrus Park
Wo ein Pfand ist, ist auch Einweg
Aurora Il Back Pages
Lovely Nails Prices (2024) – Salon Rates
Craigslist Com Panama City Fl
All Obituaries | Sneath Strilchuk Funeral Services | Funeral Home Roblin Dauphin Ste Rose McCreary MB
Random Animal Hybrid Generator Wheel
Blow Dry Bar Boynton Beach
Tommy Bahama Restaurant Bar & Store The Woodlands Menu
Suntory Yamazaki 18 Jahre | Whisky.de » Zum Online-Shop
Arch Aplin Iii Felony
Greg Steube Height
Ratchet And Clank Tools Of Destruction Rpcs3 Freeze
Joy Taylor Nip Slip
San Diego Padres Box Scores
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5550

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.