How to create SSH key on Linux

SSH keys allow you to keep your remote server accounts secure while providing password-free access. It's easy to generate an SSH key on Linux.

SSH keys allow you to keep your remote server accounts secure while providing password-free access. It's easy to generate an SSH key on Linux.

What is SSH key?

SSH key is a cryptographically generated key that allows you to log in to remote machines. SSH keys are "key pairs" containing a public key and a private key. You can copy your public key to remote machines and they will use it to authenticate you. The private key, as its name suggests, is private and must be on your local machine.

Warning: Do not share your SSH private key with anyone!

At login time, the remote server will use SSH to compare your public key with your private key. If they match, you are allowed to access the system remotely. This key pair system allows you to set up your account easily while still ensuring security.

How to create remote keys

To generate an SSH key pair, simply use the "ssh-keygen" command on the Linux command line. It will prompt you to select a file location, then enter and confirm a passphrase if you choose to use one. Passphrase allows you to add an extra layer of security to your key.

Warning: If you use a passphrase, don't forget it! You will not be able to log in with that key if you forget it.

If you want to log in without a password, just leave the passphrase blank.

Picture 1 of How to create SSH key on Linux

 By default, ssh-keygen uses the Ed25519 algorithm. This is enough for most cases because it is very safe. You can specify the type of algorithm you want with the -t flag. You usually don't need to do this unless the server doesn't support whatever algorithm you used. You can read the ssh-keygen manual page to learn how to generate another key pair.

Enter your new SSH key into the remote machine

Now, you have created your key pair. You need to copy your key to the remote server. There are two ways to do this.

The first and easiest is to use the ssh-copy-id utility. To do so, simply call it with your remote server account username and address:

ssh-copy-id user@example.com

Enter your password and it will copy your private key.

Picture 2 of How to create SSH key on Linux

Now, you can log in to the remote server:

ssh user@example.com

If that doesn't work, you'll have to copy and paste your public key manually. Your public key will be a file with the extension ".pub" in the .ssh directory of your home directory. Open it and copy and paste its contents into the .ssh/authorized_keys file on the remote machine.

Picture 3 of How to create SSH key on Linux

 

Save the file and now you can log in to the server using SSH. Make sure that only you can write the file, otherwise you may get an error.

Update 02 February 2024
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile