[Hadoop]SSH免密码登录以及失败解决方案

1. 创建ssh-key

这里我们采用rsa方式,使用如下命令:

   
   
   
   
  1. xiaosi@xiaosi:~$ ssh-keygen -t rsa -f ~/.ssh/id_rsa
  2. Generating public/private rsa key pair.
  3. Created directory '/home/xiaosi/.ssh'.
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /home/xiaosi/.ssh/id_rsa.
  7. Your public key has been saved in /home/xiaosi/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:n/sFaAT94A/xxxxxxxxxxxxxxxxxxxxxxx xiaosi@xiaosi
  10. The key's randomart image is:
  11. +---[xxxxx]----+
  12. |        o= .. .. |
  13. |        o.= ..  .|
  14. |         *.* o  .|
  15. |        +.4.=E+..|
  16. |       .SBo=. h+ |
  17. |        ogo..oo. |
  18. |          or +j..|
  19. |          ...+o=.|
  20. |          ... o=+|
  21. +----[xxxxx]-----+

备注:

这里会提示输入pass phrase,一定不要输入任何字符,回车即可。

2. 生成authorized_keys文件
   
   
   
   
  1. xiaosi@xiaosi:~$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

备注:

记得要把authorized_keys文件放到.ssh目录下,与rsa等文件放在一起,否则免登录失败,debug如下(ssh -vvv localhost进行调试,查找错误原因):

    
    
    
    
  1. xiaosi@xiaosi:~$ ssh -vvv localhost
  2. OpenSSH_7.2p2 Ubuntu-4ubuntu1, OpenSSL 1.0.2g-fips 1 Mar 2016
  3. debug1: Reading configuration data /etc/ssh/ssh_config
  4. debug1: /etc/ssh/ssh_config line 19: Applying options for *
  5. debug2: resolving "localhost" port 22
  6. debug2: ssh_connect_direct: needpriv 0
  7. debug1: Connecting to localhost [127.0.0.1] port 22.
  8. debug1: Connection established.
  9. debug1: identity file /home/xiaosi/.ssh/id_rsa type 1
  10. debug1: key_load_public: No such file or directory
  11. debug1: identity file /home/xiaosi/.ssh/id_rsa-cert type -1
  12. debug1: key_load_public: No such file or directory
  13. debug1: identity file /home/xiaosi/.ssh/id_dsa type -1
  14. debug1: key_load_public: No such file or directory
  15. debug1: identity file /home/xiaosi/.ssh/id_dsa-cert type -1
  16. debug1: key_load_public: No such file or directory
  17. debug1: identity file /home/xiaosi/.ssh/id_ecdsa type -1
  18. debug1: key_load_public: No such file or directory
  19. debug1: identity file /home/xiaosi/.ssh/id_ecdsa-cert type -1
  20. debug1: key_load_public: No such file or directory
  21. debug1: identity file /home/xiaosi/.ssh/id_ed25519 type -1
  22. debug1: key_load_public: No such file or directory
  23. debug1: identity file /home/xiaosi/.ssh/id_ed25519-cert type -1
  24. debug1: Enabling compatibility mode for protocol 2.0
  25. debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu1
  26. debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu1
  27. debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu1 pat OpenSSH* compat 0x04000000
  28. debug2: fd 3 setting O_NONBLOCK
  29. debug1: Authenticating to localhost:22 as 'xiaosi'
  30. debug3: hostkeys_foreach: reading file "/home/xiaosi/.ssh/known_hosts"
  31. debug3: record_hostkey: found key type ECDSA in file /home/xiaosi/.ssh/known_hosts:1
  32. debug3: load_hostkeys: loaded 1 keys from localhost
  33. debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
  34. debug3: send packet: type 20
  35. debug1: SSH2_MSG_KEXINIT sent
  36. debug3: receive packet: type 20
  37. debug1: SSH2_MSG_KEXINIT received
  38. debug2: local client KEXINIT proposal
  39. debug2: KEX algorithms: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,ext-info-c
  40. debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
  41. debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,3des-cbc
  42. debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,aes128-cbc,aes192-cbc,aes256-cbc,3des-cbc
  43. debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
  44. debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
  45. debug2: compression ctos: none,zlib@openssh.com,zlib
  46. debug2: compression stoc: none,zlib@openssh.com,zlib
  47. debug2: languages ctos:
  48. debug2: languages stoc:
  49. debug2: first_kex_follows 0
  50. debug2: reserved 0
  51. debug2: peer server KEXINIT proposal
  52. debug2: KEX algorithms: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
  53. debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
  54. debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
  55. debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
  56. debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
  57. debug2: MACs stoc: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
  58. debug2: compression ctos: none,zlib@openssh.com
  59. debug2: compression stoc: none,zlib@openssh.com
  60. debug2: languages ctos:
  61. debug2: languages stoc:
  62. debug2: first_kex_follows 0
  63. debug2: reserved 0
  64. debug1: kex: algorithm: curve25519-sha256@libssh.org
  65. debug1: kex: host key algorithm: ecdsa-sha2-nistp256
  66. debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
  67. debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
  68. debug3: send packet: type 30
  69. debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
  70. debug3: receive packet: type 31
  71. debug1: Server host key: ecdsa-sha2-nistp256 SHA256:378enl3ckhdpObP8fnsHr1EXz4d1q2Jde+jUplkub/Y
  72. debug3: hostkeys_foreach: reading file "/home/xiaosi/.ssh/known_hosts"
  73. debug3: record_hostkey: found key type ECDSA in file /home/xiaosi/.ssh/known_hosts:1
  74. debug3: load_hostkeys: loaded 1 keys from localhost
  75. debug1: Host 'localhost' is known and matches the ECDSA host key.
  76. debug1: Found key in /home/xiaosi/.ssh/known_hosts:1
  77. debug3: send packet: type 21
  78. debug2: set_newkeys: mode 1
  79. debug1: rekey after 134217728 blocks
  80. debug1: SSH2_MSG_NEWKEYS sent
  81. debug1: expecting SSH2_MSG_NEWKEYS
  82. debug3: receive packet: type 21
  83. debug2: set_newkeys: mode 0
  84. debug1: rekey after 134217728 blocks
  85. debug1: SSH2_MSG_NEWKEYS received
  86. debug2: key: /home/xiaosi/.ssh/id_rsa (0x5602df5e80c0)
  87. debug2: key: /home/xiaosi/.ssh/id_dsa ((nil))
  88. debug2: key: /home/xiaosi/.ssh/id_ecdsa ((nil))
  89. debug2: key: /home/xiaosi/.ssh/id_ed25519 ((nil))
  90. debug3: send packet: type 5
  91. debug3: receive packet: type 7
  92. debug1: SSH2_MSG_EXT_INFO received
  93. debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
  94. debug3: receive packet: type 6
  95. debug2: service_accept: ssh-userauth
  96. debug1: SSH2_MSG_SERVICE_ACCEPT received
  97. debug3: send packet: type 50
  98. debug3: receive packet: type 51
  99. debug1: Authentications that can continue: publickey,password
  100. debug3: start over, passed a different list publickey,password
  101. debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
  102. debug3: authmethod_lookup publickey
  103. debug3: remaining preferred: keyboard-interactive,password
  104. debug3: authmethod_is_enabled publickey
  105. debug1: Next authentication method: publickey
  106. debug1: Offering RSA public key: /home/xiaosi/.ssh/id_rsa
  107. debug3: send_pubkey_test
  108. debug3: send packet: type 50
  109. debug2: we sent a publickey packet, wait for reply
  110. debug3: receive packet: type 51
  111. debug1: Authentications that can continue: publickey,password
  112. debug1: Trying private key: /home/xiaosi/.ssh/id_dsa
  113. debug3: no such identity: /home/xiaosi/.ssh/id_dsa: No such file or directory
  114. debug1: Trying private key: /home/xiaosi/.ssh/id_ecdsa
  115. debug3: no such identity: /home/xiaosi/.ssh/id_ecdsa: No such file or directory
  116. debug1: Trying private key: /home/xiaosi/.ssh/id_ed25519
  117. debug3: no such identity: /home/xiaosi/.ssh/id_ed25519: No such file or directory
  118. debug2: we did not send a packet, disable method
  119. debug3: authmethod_lookup password
  120. debug3: remaining preferred: ,password
  121. debug3: authmethod_is_enabled password
  122. debug1: Next authentication method: password
  123. xiaosi@localhost's password:

3. 验证
   
   
   
   
  1. xiaosi@xiaosi:~$ ssh localhost
  2. The authenticity of host 'localhost (127.0.0.1)' can't be established.
  3. ECDSA key fingerprint is SHA256:378enl3ckhdpObP8fnsHr1EXz4d1q2Jde+jUplkub/Y.
  4. Are you sure you want to continue connecting (yes/no)? yes
  5. Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
  6. sign_and_send_pubkey: signing failed: agent refused operation
  7. xiaosi@localhost's password:
4. authorized_keys权限

我们可以看到还是让我输入密码,很大可能是authorized_keys文件权限的问题,我们给该文件赋予一定权限:

   
   
   
   
  1. xiaosi@xiaosi:~$ chmod 600 ~/.ssh/authorized_keys
再次验证:
   
   
   
   
  1. xiaosi@xiaosi:~$ ssh localhost
  2. Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-24-generic x86_64)
  3. * Documentation:  https://help.ubuntu.com/
  4. 0 个可升级软件包。
  5. 0 个安全更新。
  6. Last login: Thu Jun 16 08:05:50 2016 from 127.0.0.1

到此表示OK了。


备注:

或者第一次需要输入密码,以后再次登陆就不需要输入密码了。


有更明白的小伙伴可以指导一下。。。。。。




你可能感兴趣的:(SSH,免密码登陆,失败解决,rsa,Hadoop)