1.6 SSH免密登录

第1章 环境安装配置

1.6 SSH免密登录

1、XShell的一个技巧

当多台虚拟机需要执行相同命令时,可以单击XShell右下角的“三条横杠”按钮,选择“全部会话”,这时XShell下方的输入框将显示提示“发送文本到当前XShell窗口的全部会话”

这里写图片描述

比如在XShell下方输入框输入data命令

这里写图片描述

回车后,将该date命令同时发送到node1、node2和node3。这时可以看到其他两个会话标签多了个叹号提示符。

这里写图片描述 
查看node2执行情况 
这里写图片描述 
查询node3执行情况 
这里写图片描述

2、生成RSA秘钥对

在XShell下方输入框中输入命令ssh-keygen -t rsa,然后回车执行。再回车3次,即可生成RSA秘钥对

这里写图片描述

这里写图片描述

这里写图片描述

查看密钥对文件


     
     
  1. [root @node1 ~] # ls .ssh
  2. id_rsa id_rsa.pub
  • 1
  • 2
  • id_rsa:私钥
  • id_rsa.pub:公钥-

3、将每个节点的公钥拷贝到authorized_keys

可以通过命令ssh-copy-id,将本机公钥复制到其他节点的authorized_keys目录,这样本机即可免秘钥登录其他节点。 
为了操作方便,我们通过XShell执行下方输入框执行命令即可:

ssh-copy-id node1
     
     
  • 1

按照提示输入“yes”和密码,即可将node1的公钥复制到node1、node2和node3节点。(复制本机,可以实现免密登录本机)

然后再通过XShell分别执行:


     
     
  1. ssh- copy- id node2
  2. ssh- copy- id node3
  • 1
  • 2

通过XShell执行三条ssh-copy-id命令,实际上就是分别在node1、node2和node3节点执行。下面是命令执行信息: 
(1)node1 
ssh-copy-id node1 
ssh-copy-id node2 
ssh-copy-id node3


     
     
  1. [root@node1 ~] # ssh-copy-id node1
  2. The authenticity of host 'node1 (192.168.80.131)' can't be established.
  3. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  6. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  7. root@node1 's password:
  8. Number of key(s) added: 1
  9. Now try logging into the machine, with: "ssh 'node1'"
  10. and check to make sure that only the key(s) you wanted were added.
  11. [root@node1 ~] # ssh-copy-id node2
  12. The authenticity of host 'node2 (192.168.80.132)' can't be established.
  13. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  14. Are you sure you want to continue connecting (yes/no)? yes
  15. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  16. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  17. root@node2 's password:
  18. Number of key(s) added: 1
  19. Now try logging into the machine, with: "ssh 'node2'"
  20. and check to make sure that only the key(s) you wanted were added.
  21. [root@node1 ~] # ssh-copy-id node3
  22. The authenticity of host 'node3 (192.168.80.133)' can't be established.
  23. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  24. Are you sure you want to continue connecting (yes/no)? yes
  25. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  26. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  27. root@node3 's password:
  28. Number of key(s) added: 1
  29. Now try logging into the machine, with: "ssh 'node3'"
  30. and check to make sure that only the key(s) you wanted were added.
  31. [root@node1 ~] #
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

(2)node2 
ssh-copy-id node1 
ssh-copy-id node2 
ssh-copy-id node3


     
     
  1. [root@node2 ~] # ssh-copy-id node1
  2. The authenticity of host 'node1 (192.168.80.131)' can't be established.
  3. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  6. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  7. root@node1 's password:
  8. Number of key(s) added: 1
  9. Now try logging into the machine, with: "ssh 'node1'"
  10. and check to make sure that only the key(s) you wanted were added.
  11. [root@node2 ~] # ssh-copy-id node2
  12. The authenticity of host 'node2 (192.168.80.132)' can't be established.
  13. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  14. Are you sure you want to continue connecting (yes/no)? yes
  15. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  16. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  17. root@node2 's password:
  18. Number of key(s) added: 1
  19. Now try logging into the machine, with: "ssh 'node2'"
  20. and check to make sure that only the key(s) you wanted were added.
  21. [root@node2 ~] # ssh-copy-id node3
  22. The authenticity of host 'node3 (192.168.80.133)' can't be established.
  23. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  24. Are you sure you want to continue connecting (yes/no)? yes
  25. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  26. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  27. root@node3 's password:
  28. Number of key(s) added: 1
  29. Now try logging into the machine, with: "ssh 'node3'"
  30. and check to make sure that only the key(s) you wanted were added.
  31. [root@node2 ~] #
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

(3)node3 
ssh-copy-id node1 
ssh-copy-id node2 
ssh-copy-id node3


     
     
  1. [root@node3 ~] # ssh-copy-id node1
  2. The authenticity of host 'node1 (192.168.80.131)' can't be established.
  3. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  6. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  7. root@node1 's password:
  8. Number of key(s) added: 1
  9. Now try logging into the machine, with: "ssh 'node1'"
  10. and check to make sure that only the key(s) you wanted were added.
  11. [root@node3 ~] # ssh-copy-id node2
  12. The authenticity of host 'node2 (192.168.80.132)' can't be established.
  13. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  14. Are you sure you want to continue connecting (yes/no)? yes
  15. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  16. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  17. root@node2 's password:
  18. Number of key(s) added: 1
  19. Now try logging into the machine, with: "ssh 'node2'"
  20. and check to make sure that only the key(s) you wanted were added.
  21. [root@node3 ~] # ssh-copy-id node3
  22. The authenticity of host 'node3 (192.168.80.133)' can't be established.
  23. ECDSA key fingerprint is e2: 9a: 7d: 70: 25: 24: 45: 11: 97: 12: 35:e0: 45: 4c: 64: 31.
  24. Are you sure you want to continue connecting (yes/no)? yes
  25. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  26. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  27. root@node3 's password:
  28. Number of key(s) added: 1
  29. Now try logging into the machine, with: "ssh 'node3'"
  30. and check to make sure that only the key(s) you wanted were added.
  31. [root@node3 ~] #
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

4、测试免密登录


     
     
  1. [root@node1 ~]# ssh node1
  2. Last login: Fri Jul 21 23 :13 :10 2017 from node1
  3. [root@node1 ~]# ls .ssh
  4. authorized_keys id_rsa id_rsa .pub known_hosts
  5. [root@node1 ~]# ssh node2
  6. Last login: Fri Jul 21 23 :13 :18 2017 from node2
  7. [root@node2 ~]# ssh node3
  8. Last login: Fri Jul 21 23 :16 :03 2017 from node3
  9. [root@node3 ~]# ssh node2
  10. Last login: Fri Jul 21 23 :31 :43 2017 from node1
  11. [root@node2 ~]# ssh node1
  12. Last login: Fri Jul 21 23 :16 :06 2017 from node3
  13. [root@node1 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

     
     
  1. [root@node1 ~]# ll .ssh
  2. total 16
  3. - rw------- 1 root root 1176 Jul 21 23:14 authorized_keys
  4. - rw------- 1 root root 1679 Jul 21 23:14 id_rsa
  5. - rw-r--r-- 1 root root 392 Jul 21 23:14 id_rsa.pub
  6. - rw-r--r-- 1 root root 546 Jul 21 23:15 known_hosts
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

cat .ssh/authorized_keys


     
     
  1. [root @node1 ~] # cat .ssh/authorized_keys
  2. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABA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ncqUese3L7dVwxBUNWFA/wmwuWRAQhY/ GwSAp/ 67ZnwpNs1sBSK6D+ 1d2XNgzmpfWK18Ilk6fb root @node2
  • 1
  • 2
  • 3
  • 4

cat .ssh/known_hosts


     
     
  1. [root @node1 ~] # cat .ssh/known_hosts
  2. node1, 192.168. 80.131 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBA+ 9ht7NXrQyz1cD2YY4w17BEMnM0yZjG2VeVFIztrmusVnFFqJNmvHsxVHWJNnhQeFQPJ0GvPL7I4WB34pmz5E=
  3. node2, 192.168. 80.132 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBA+ 9ht7NXrQyz1cD2YY4w17BEMnM0yZjG2VeVFIztrmusVnFFqJNmvHsxVHWJNnhQeFQPJ0GvPL7I4WB34pmz5E=
  4. node3, 192.168. 80.133 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBA+ 9ht7NXrQyz1cD2YY4w17BEMnM0yZjG2VeVFIztrmusVnFFqJNmvHsxVHWJNnhQeFQPJ0GvPL7I4WB34pmz5E=
  5. [root @node1 ~] #
  • 1
  • 2
  • 3
  • 4
  • 5

5、退出ssh登录

exit命令退出


     
     
  1. [root @node1 ~] # exit
  2. logout
  3. Connection to node1 closed.
  4. [root @node2 ~] #
  • 1
  • 2
  • 3
  • 4

快捷键ctl+d退出


     
     
  1. [root @node2 ~] # logout
  2. Connection to node2 closed.
  3. [root @node3 ~] # logout
  4. Connection to node3 closed.
  5. [root @node2 ~] # logout
  6. Connection to node2 closed.
  7. [root @node1 ~] #
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
灰常灰常感谢原博主的辛苦工作,为防止删博,所以转载,只供学习使用,不做其他任何商业用途。
参考:https://blog.csdn.net/chengyuqiang/article/details/71512677
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值