How to setup SSH key in CentOS 8 /RHEL 8

SSH, or secure shell, is an encrypted protocol used to administer and communicate with servers. SSH keys provide a straightforward, secure method of logging into your server and are recommended for all users.


1. A client machine where we will generate our key

2. A server machine where we will SSH into the client machine

Step 1: Create a RSA keypair

Generate a key in the client machine


By default ssh-keygen create a 2048-bit RSA key pair(For creating a 4096-bit RSA pass a -b 4096 flag )

After entering the command you will see the following output

Press enter to save the key in /root/.ssh/id_rsa) file

If you had previously entered RSA the output will be like:

Enter y for overwriting the previously generated key

Then you will be asked to passphrase. Which adds an additional security layer for your key

Then we will have RSA key. The output will be as shown in the below figure

Step 2: Copy the key to a server machine

Copy the generated key to the server machine by using the command:

ssh-copy-id [email protected]_host

Specify the user and IP address/hostname of the server machine to that you want to copy

You will be asked to enter the server machines password. After entering the password you will have the following output:

Step 3: Testing the key generation

Now after successfully generating the key and coping to the server machine, try to ssh into the server machine

ssh [email protected]_host

For the first time you will be asked to enter password of the server machine after that you will be able to enter into the server machine with the help of passphrase. Thus we will have the connection with remote server with a security key.

Related Articles