To setup SSH keys between two servers we need to follow these steps:
Step 1 : Create a Key Pair in the Source Server
Create a key pair on the source server. When we give ssh-keygen command, it will by default create a 2048 -bit RSA Key pair and if you need more stronger encryption you can use 4096 bit as well. For that you need to use “-b 4096” in the ssh-keygen command end. I am using default one here.
Couple of things to be taken care in the below output:
In line “Enter file in which to save the key
It is asking the path to save the key and default one is normally fine. If default is fine, you can simply press enter. If you want to try alternate path, then you need to specify the same there. Some times this will says like:
You should take a copy of the .ssh folder before making any changes or should know what you are doing. Sending a Yes will make old key (if in use already ) not to work.
In the line “Enter passphrase (empty for no passphrase):” This is an additional security procedure which will ask passphrase each time when you try to login to SSH and that will work as a 2 step verification. But if you need ssh access for any scripting or any other direct works and speedy works, then better to not to have this. Other than scripting or automating the works, we will suggest you to have this for sure.
Full result of the command for reference:
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
The key's randomart image is:
| . .|
| . oo.o|
| E S o .*oBo|
| . . * o+.+.=|
| . . . .o=. =ooo|
| . .. + o*.B|
| .. o. o+oB+|
Step 2 : Copy this created Key Pair to your Destination Server
There are 2 different ways to get this copied to your destination server
- Using the ssh-copy-id command
- Copying the ssh key using normal ssh user/pass as one liner from our local machine or after logging in the server.
2.1 Using the ssh-copy-id command
ssh-copy-id will handle the copy and setup of the key to a remote server in the proper way for you. Once the command is completed you will not need a password for each login. Now you can write all your automated scripts for system admin work without having to enter a password manually and save time on day to day access of systems you use all the time.
First you need to check if there is a command like this and if the command is working and user as which you are trying is having access to this command, then you can use this command to copy the public key to the remote server. This Utility will scan your local account for any rsa public key and will prompt you for the password of the remote user’s account.
Here we are going to copy the root ssh key to the servers root level access. So to get this copied, you need to login / switch to the user for which you have created the key. In this case we are trying root-root connection.
Full output is below and I am adding needed details in between them
The authenticity of host '[188.8.131.52]:1986 ([184.108.40.206]:1986)' can't be established.
ECDSA key fingerprint is SHA256:YYOj54aEJvIle4D2osDiEhuS0NEDImPTiMhHGgDqQFk.
Are you sure you want to continue connecting (yes/no)? yes
If you are using this for the first time, you will get such a response and you need to type yes and then press enter
to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted
now it is to install the new keys
Enter the password and then press enter.
Now try logging into the machine, with: “ssh -p ‘1986’ ‘email@example.com′”
and check to make sure that it is working as expected.
After this you will be able to login to server without any passwords. Once password less auth is working fine, you can disable the password authentication so that you can lock ssh access to just using ssh keys
2.2 Copying the ssh key using normal ssh user/pass manually
If some how you are not able to get that above command working, I will add the steps so that you can copy the ssh key and setup password less auth from your machine to your server.
To do this we have to manually append the content of your id_rsa.pub file to the /root/.ssh/authorized_keys file on your Destination machine. If you are going to copy the key to root user the location will be
From the Step 1 : you may have seen the below line
Your public key has been saved in
This says the public key which you need to copy to the remote server is located in the above file. So you need to copy the content of this file and then copy or paste them in the authorized_keys of the remote server
So do the below steps
Below command will give you the key to be copied:
Login to Remote server to which you need to copy this above key and make sure you use the same user to which you need to copy the ssh key. If you need direct root access, copy the key directly to /root/.ssh/ section
Create a folder .ssh if it does not exist
To check if that exists and if not create it using the below commands:
If the folder is not there , then create it with the below command:
root@destination$ touch /root/.ssh/authorized_keys
root@destination:$ echo “ssh-rsa
Make sure the permission of the folder is correct one
After this please try login to server from a new terminal and make sure keyless auth is working as expected. Only then disable password authentication in the ssh config.
NOTE: Make double sure you are able to login to server as needed by you (either directly from your machine, or you can login to another user in the remote server and switch to root from that account manually using su or sudo ) and then only disable password auth else there is chances to get root users locked.
If you have any needs you can always contact me for any help and share your comments.