Linux ssh 不需要输入密码的方法

采用证书的方法可以解决ssh不需要输入密码的问题。

本文采用CentOS的操作系统,创建SSH的key,并在两台或多台机器间实现信任。从而实现SSH登录不需要输入密码的功能。

首先,在一台机器上创建SSH Key:

[root@mysql-ha2 ~]# ssh-keygen 

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:

e1:9b:9a:b8:4d:ea:2c:e7:0c:49:bd:a7:47:fa:b4:4b root@mysql-ha2

The key's randomart image is:

+--[ RSA 2048]----+

|                 |

|                 |

|        .        |

|   .   . .       |

|  . .   S        |

| . . ..  o       |

|  o .oE o        |

|  .+oX.+         |

|   =X+B.         |

+-----------------+

此时可以在/root/.ssh目录下看到两个文件:

-rw-------. 1 root root 1675 Dec  5 05:02 id_rsa

-rw-r--r--. 1 root root  396 Dec  5 05:02 id_rsa.pub

复制Key到另外一台机器上,使登录这台机器不输入密码:

[root@mysql-ha2 .ssh]# ssh-copy-id -i id_rsa.pub root@10.1.1.4

测试,发现登录ssh 10.1.1.4不需要密码。

 

把id_rsa和id_rsa.pub复制到10.1.1.4上,在10.1.1.4上做同样的ssh-copy-id的动作,可以使双向ssh登录不需要密码。

 

且采用的是相同的ssh key。

原文地址:https://www.cnblogs.com/hengwei/p/5021411.html