Virtual Machines

Products

Virtual Machines

Documentation > Products > Virtual Machines > Set ssh Automatic Login Between Virtual Machines

Set ssh Automatic Login Between Virtual Machines

The ssh automatic login needs to use public key and private key. Under Linux, use ssh-keygen to generate public/private key pairs, taking CentOS as an example.

There are Virtual Machines A(192.168.1.155) and Virtual Machines B(192.168.1.181). And set the user of Virtual Machines A to the user of Virtual Machines B through ssh automatic login.

If the root user sets the automatic login, replace the path /home/user in the following method with /root. When using scp to replicate, replace the target machine user with root.

  1. Generate public/private key pairs under Virtual Machines A.

[user@A ~]$ ssh-keygen -t rsa

When an interactive interface appears and it is required to enter password and key pair save path, etc., press Enter to generate by default. After the execution is successful, the .ssh directory is generated under /home/user. There are id_rsa and id_rsa.pub under .ssh.

  1. Copy id_rsa.pub under Virtual Machines A machine to the same path under Virtual Machines B. When using the scp command, you will be asked to enter the user's login password of Virtual Machines B. Enter the password and press Enter to complete the copy

[user@A ~]$ scp .ssh/id_rsa.pub user@192.168.1.181:/home/user/id_rsa.pub

user@192.168.1.181's password:'s password:

id_rsa.pub 100% 223 0.2KB/s 00:00

  1. Virtual Machines B adds id_rsa.pub copied from Virtual Machines A to /home/user/.ssh/authorized_keys file.

[user@B ~]$ cat /home/user/id_rsa.pub >> /home/user/.ssh/authorized_keys

[user@B ~]$ chmod 600 /home/user/.ssh/authorized_keys

Authority of authorized_keys is to be set to 600.

  1. Virtual Machines A logs in to Virtual Machines B.

[user@A ~]$ ssh user@192.168.1.181

The authenticity of host 'B (192.168.1.181)' can't be established.

RSA key fingerprint is 00:a6:a8:87:eb:c7:40:10:39:cc:a0:eb:50:d9:6a:5b.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.1.181' (RSA) to the list of known hosts.

Last login: Thu Mar 3 09:53:18 2018 from user

[user@B ~]$

The first time you log in, you need to enter yes to add Virtual Machines B to the known machine list of Virtual Machines A

Now, the user of Virtual Machines A can automatically log in to the user of Virtual Machines B.

To realize mutual automatic login of Virtual Machines A and B, configure on Virtual Machines B by the above method and add id_rsa.pub content generated by Virtual Machines B to authorized_keys file of Virtual Machines A.

Update Time:2019-08-28 21:38:32