How to generate keys with SSH-KEYGEN


SSH-KEYGEN Tutorial to deploy and configure SSH key authentication

Public key authentication, also known as asymmetric cryptosystem, is an authentication method used by OpenSSH and OpenSSL to encrypt and authenticate users to a server using a key pair consisting of digital signatures, with the private and public keys. Such a key pair is much harder to compromise than a password.

Generate OpenSSH RSA key

The easiest way to generate a key pair in OpenSSH format is to run ssh-keygen without arguments in the shell. In this case, it asks for the file in which the keys are to be stored. Like here in this example:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/
The key fingerprint is:
SHA256:TZ+isKhohYE6wOcKuqWxElclsdZw4UxJwFtnB9txEC8 user@debian.localdomain
The key's randomart image is:
+---[RSA 3072]----+
|   .+.o...+o.    |
|    oOo.oo.+     |
|o   o=*o..E .    |
|oo oo. . o o .   |
|o =.  . S o o    |
|=..o   . . .     |
|+++   . .        |
|oO   . .         |
|B.... .          |

SSH keys for user authentication are typically stored in the user base under the .ssh directory.

Generate a key pair with the passing of parameters.

$ ssh-keygen -f ~/.ssh/key_rsa -t rsa -b 4096

The parameters have the following meaning:
-f the key file
-t the algorithm
-b the key length
The longest key is 4096. If no algorithm is passed, the default is an RSA key.

When asking for the passphrase for the private key, a passphrase can be entered, in the next section after that it is shown how it can be generated, by pressing the Enter key, without a passphrase the key is created without password protection.

The purpose of the passphrase is usually to protect the key. It is a second authentication factor. This makes the key file without a public key unusable for attackers. It is not uncommon for hackers to filter for files from compromised systems, including backups and decommissioned hardware.

A secure passphrase can be generated automatically, with the following input a 32-character passphrase is generated.

$ date +%s | sha256sum | base64 | head -c 32 ; echo

OpenSSL can also be used to generate a passphrase with the built-in random number generator (rand), which is output as follows.

$ openssl rand -base64 32

Unauthorized persons must not have access to the private key and the associated passphrase, if the private key is stolen, a new one must be generated, then the public key is also exchanged.

If only the public key is lost, the following input shows how the public key can be recovered from the openSSH private key with the -y option, the passphrase is required for this.

$ ssh-keygen -y -f ~/.ssh/key_rsa > ~/.ssh/

It should be noted that at the end of the file the comment (key comment) must be added again, after a space, as in this example user@host.localdomain:

$ ssh-rsa AAAA...HPCQ== user@host.localdomain

  in do not change anything else.

If you don’t want a passphrase, to remove it with the following command.

$ ssh-keygen -p -P old_passphrase -N "" -f ~/.ssh/key_rsa

The empty input between the quotation marks does not write a new passphrase, so it is deleted.

Generate ECDSA key

Next, an ECDSA key pair with a key length of 521 bits is generated and stored under the user base ~/.ssh. Valid lengths are 256, 384 or 521 bits.

$ ssh-keygen -f ~/.ssh/key-ecdsa -t ecdsa -b 521

ECDSA is a new algorithm for digital signatures standardized by the U.S. government using elliptic curves. It’s probably the best algorithm right now. Most SSH clients today support ECDSA.

Two files have now been created in the path ~/.ssh directory. The file with the extension .pub is the public key. The key-ecdsa file in this example is the private key and is readable only by the owner.

-rw-------.  1 user user 801 May 20 20:33 key-ecdsa
-rw-r--r--.  1 user user 279 May 20 20:33

The public key key as SHA2 hash is now stored in the file authorized_keys on the server in the user’s home directory under .ssh.

$ cat ~/.ssh/authorized_keys
ecdsa-sha2-nistp521 AAAAE2vjZHNhLXNoYTItbmlzdHA1MjEFASBIbml
dhSmeVwnLHUQNoktbVPoVDHw== user@host.localdomain

Public-Key Transfer with ssh-copy-id

To use public key authentication, the public key must be copied to the server and stored in the authorized_keys file. This can be done conveniently with the ssh-copy-id tool.

$ ssh-copy-id -i ~/.ssh/key-ecdsa

Now you can log on to the server without having to enter a password, this method is also suitable for automated tasks and for logins from scripts.

$ ssh
Last login: Mon May 20 21:53:46 2019 from

Verbose mode can be useful for troubleshooting, with numerous debugging messages about progress, such as connection, authentication, and configuration issues.

$ ssh -v

  Multiple -v options increases verbosity, max. -vvv.

OpenSSH config

If you have different hosts and key pairs, this can be defined in the config file under ~/.ssh.

$ vi ~/.ssh/config
Host *
     User user
     Port 22
     HostKeyAlgorithms +ssh-rsa
     PubkeyAcceptedKeyTypes +ssh-rsa
     IdentityFile key-ecdsa

     User user
     Port 22
     HostKeyAlgorithms +ssh-dss
     PubkeyAcceptedKeyTypes +ssh-dss
     IdentityFile id_dss

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published.