DEV Community

Erick Quinteros
Erick Quinteros

Posted on

What Is ssh-keygen

What Is ssh-keygen?

ssh-keygen is a tool for creating new SSH public-key key pairs.

SSH Keys and Public Key Authentication

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

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 the keys

ssh-keygen
Generating public/private RSA key pair.
Enter fullscreen mode Exit fullscreen mode

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.
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.

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. The RSA algorithm may 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 standardized 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 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 ecdsa -b 521
Enter fullscreen mode Exit fullscreen mode

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 ~/name-key-ecdsa -t ecdsa -b 521
Enter fullscreen mode Exit fullscreen mode

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.
It only takes one leaked, stolen, or misconfigured key to gain access.

Bibliografia

Further Reading

Check out the other articles in this series:

  • ssh-agent:
  • ssh-keygen:
  • known_hosts:

Top comments (0)