生成密钥对

ssh-keygen -t rsa


  1. [root@hadoop101 ~]# ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:tODbWKXEly3aqKt15dwR6cyLoozpS3Qv1V6NNWorqzY root@hadoop101
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. | |
  13. | . o . |
  14. | . + = + o |
  15. | . + X = * . |
  16. | . o S + X . |
  17. | . . O = = + |
  18. | . * = * + |
  19. | . = =E. o |
  20. | .*o+..o. |
  21. +----[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 主机名
......输入密码后即完成......