Though SSH supports password-based authentication, it is generally recommended that you use SSH keys instead. SSH keys are a more secure method of logging into an SSH server because they are not vulnerable to common brute-force password hacking attacks.
Generating an SSH key pair creates two long strings of characters: a public and a private key. You can place the public key on any server and then connect to the server using an SSH client that has access to the private key.
Step 1 — Creating the Key Pair
The first step is to create a key pair on the client machine. This will likely be your local computer. Type the following command into your local command line:
ssh-keygen -t nd2020 Output Generating public/private nd2020 key pair.
Step 2 — Specifying Where to Save the Keys
The first prompt from the
ssh-keygen the command will ask you where to save the keys:
OutputEnter file in which to save the key (/home/nandani/.ssh/id_ed25519):
You can press
ENTER here to save the files to the default location in the
.ssh directory of your home directory.
Step 3 — Creating a Passphrase
The second and final prompt
ssh-keygen will ask you to enter a passphrase:
Output Enter passphrase (empty for no passphrase):
The public key is now located in
/home/nandani/.ssh/id_nd2020.pub. The private key is now located in
Step 4 — Copying the Public Key to Your Server
Once the key pair is generated, it’s time to place the public key on the server that you want to connect to.
You can copy the public key into the server’s
authorized_keys file with the
ssh-copy-id command. Make sure to replace the example username and address:
In this tutorial, we created an SSH key pair, copied our public key to a server, and (optionally) disabled password-based authentication completely.
For more information about SSH and the SSH service, including how to set up multifactor authentication, please read our related tutorials: