生成密钥对
ssh-keygen -t rsa
[root@hadoop101 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
SHA256:tODbWKXEly3aqKt15dwR6cyLoozpS3Qv1V6NNWorqzY root@hadoop101
The key's randomart image is:
+---[RSA 2048]----+
| |
| . o . |
| . + = + o |
| . + X = * . |
| . o S + X . |
| . . O = = + |
| . * = * + |
| . = =E. o |
| .*o+..o. |
+----[SHA256]-----+
查看生成的密钥对
在home目录下的.ssh隐藏文件夹中
[root@hadoop101 ~]# cd ~/.ssh/
[root@hadoop101 .ssh]# ll
总用量 12
-rw-------. 1 root root 1679 1月 11 22:09 id_rsa
-rw-r--r--. 1 root root 396 1月 11 22:09 id_rsa.pub
-rw-r--r--. 1 root root 355 1月 11 22:10 known_hosts
本机免密登录
测试ssh登录
[root@hadoop101 .ssh]# ssh hadoop101
root@hadoop101's password:
提示需要输入密码
生成免密文件
authorized_keys
[root@hadoop101 .ssh]# cp id_rsa.pub authorized_keys
再次测试ssh登录
[root@hadoop101 .ssh]# ssh hadoop101
Last login: Sat Jan 11 22:10:28 2020 from hadoop101
[root@hadoop101 ~]# exit
登出
Connection to hadoop101 closed.
[root@hadoop101 .ssh]#
可以直接登录,不需要输入密码,输入exit退出登录
远程免密登录
ssh-copy-id -i hadoop102
[root@hadoop101 sbin]# ssh hadoop102
Last login: Sun Jan 12 01:17:09 2020 from hadoop101
[root@hadoop102 ~]# exit
登出
Connection to hadoop102 closed.
[root@hadoop101 sbin]#
拷贝成功后,可以直接免密登录102机器
总结
[root@hadoop101 ~]# ssh-keygen -t rsa
......一路回车......
[root@hadoop101 sbin]# ssh 主机名
......输入密码后即完成......