SSH免密钥登录其实很简单,只需几步就能部署完成。
测试环境:CentOS Linux release 7.1.1503 (Core)
步骤如下:
1. 在主机上生成公钥和密钥!
[ Thegod:~ chengde$ ssh-keygen #一直回车即可!
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/chengde/.ssh/id_rsa):
/Users/chengde/.ssh/id_rsa already exists.
...

2. 第一步执行完成后,会在你当前登录的用户的宿主目录下生成一个.ssh的目录,在.ssh目录下会生成两个文件(id_rsa和id_rsa.pub)私钥和公钥

Thegod:~ chengde$ cd .ssh/
Thegod:.ssh chengde$ ls
id_rsa id_rsa.pub known_hosts

3. 将公钥(id_rsa.pub)移动到要免密码登录的服务器的用户家目录下的.ssh目录下并改名为authorized_keys
Thegod:.ssh chengde$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@103.40.101.59
Remarks: 如果需要免密钥登录的服务器的用户家目录下的.ssh目录下已经有authorized_keys这个文件了,那么就将公钥追加到这个文件中,如果覆盖了里面的内容就更改了!(此内容只适合多台服务器免密钥登录这台服务器)

4.测试,完成

Thegod:.ssh chengde$ ssh root@103.40.101.59
Last failed login: Sun Jan 28 21:13:04 CST 2018 from 218.65.30.30 on ssh:notty
There were 883 failed login attempts since the last successful login.
Last login: Sun Jan 28 20:30:20 2018 from 139.215.216.12
[root@server59 ~]#