Sometimes working on projects, you will have the need to login from this Linux machine to another Linux machine using SSH without having to enter a password to do some automated tasks as your needs, for example: copy a file … This tutorial, I would like to guide you to do this!
The condition is that the two machines have to install SSH 😀
Let’s say we have two virtual machines with the following IP addresses:
and
Now, I want from 192.168.59.157 login to 192.168.59.150 without having to enter a password, I will do the following:
First, on 192.168.59.157 I will generate a public key and a private key using the ssh-keygen tool by entering the following terminal:
1 |
ssh-keygen |
Result:
Then, I will copy the public key file id.rsa.pub which created in the ~/.ssh/ folder of 192.168.59.157 machine to the 192.168.59.150 machine using the ssh-copy-id tool by entering the command line:
1 |
ssh-copy-id -i ~/.ssh/id_rsa.pub <username>@<IP_remote_server> |
In this example, I would enter the following:
1 |
khanh@ubuntu:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub khanh@192.168.59.150 |
That’s it, now to log into 192.168.59.150 machine, I just enter:
1 |
ssh khanh@192.168.59.150 |