An SSH key consists of a pair of files: the private key and the public key. The private key is confidential and should never be shared, while the public key enables you to log into the servers, containers, and VMs you set up. When creating these keys using the ssh-keygen
command, the keys are stored securely, allowing you to bypass the login prompt when connecting to your instances.
To generate SSH keys, follow these steps:
- Enter the following command in the shell.
1ssh-keygen -t rsa
The-t
option specifies the type of key to generate. Here,rsa
is chosen due to its widespread use, though other types such asdsa
,ecdsa
,ed25519
, orrsa
for different security needs can be selected, depending on your requirements. From theman
documentation:
1234$ man ssh-keygen-t dsa | ecdsa | ed25519 | rsa | rsa1Specifies the type of key to create. The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa”, “ed25519”, or “rsa” for protocol version 2.
This command initiates the key generation process.ssh-keygen
will then prompt you to specify a storage location for the key. - Enter a passphrase for added security or press ENTER to proceed without one (not recommended for security reasons).
- Confirm the passphrase by entering it again. Upon confirming the passphrase, the system generates the key pair and provides a summary of the operation, including the location of the saved key pair and a key fingerprint for verification purposes.
The generated output will resemble the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/vinicius.grippa/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/vinicius.grippa/.ssh/id_rsa. Your public key has been saved in /home/vinicius.grippa/.ssh/id_rsa.pub. The key fingerprint is: SHA256:in5ES8pWGp43XMpjI76NX/fX7E6fhksM70h+8gljhWI vinicius.grippa@testing-server.com The key's randomart image is: +---[RSA 2048]----+ | | | | | | | . + . . | | o @ +SE o . | | O.%.. . = | | o.=.+. .= +.o.| | ..o.. .+o*ooo=| | ++o o=*o++| +----[SHA256]-----+ |
Your private key (id_rsa
) is stored in the .ssh
directory and is critical for verifying your public key’s association with your user account.