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.

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.

How to create SSH key on Linux Picture 1

 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.

How to create SSH key on Linux Picture 2

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.

How to create SSH key on Linux Picture 3

 

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.

4 ★ | 2 Vote

May be interested

  • How to install and configure Apache on Rocky LinuxPhoto of How to install and configure Apache on Rocky Linux
    apache http server is one of the most widely used web servers in the world. it is a free, open source, cross-platform web server that offers powerful features such as loadable modules, strong media support, and extensive integration with other software.
  • How to use SSH Pipe on LinuxPhoto of How to use SSH Pipe on Linux
    the unix pipe is a major step forward in the development of unix and unix-like operating systems. it allows users to perform complex computational tasks by linking together the input and output of basic programs.
  • Fix log file taking up all Linux drive spacePhoto of Fix log file taking up all Linux drive space
    although linux systems are notoriously compact, you may find that your disk space is suddenly running low. why does this happen? the clue and possibly the biggest culprit will be found in your linux system logs.
  • How to fix 'apt-get: command not found' error in Linux TerminalPhoto of How to fix 'apt-get: command not found' error in Linux Terminal
    if you want to install software on a debian-based linux distribution, apt-get is a reliable terminal command. however, sometimes you may get stuck with 'command not found' error as response.
  • 8 best Linux debugger tools for software engineersPhoto of 8 best Linux debugger tools for software engineers
    debugger is an essential tool for finding errors in software. there are many good linux debuggers that make finding weaknesses in software easier for programmers.
  • 5 example bash scripts to help you learn Linux programmingPhoto of 5 example bash scripts to help you learn Linux programming
    writing bash scripts is one of the most popular and accessible ways of programming linux computers. these simple script examples will help you understand the process and introduce you to the basics of bash programming.