ssh免密碼登入失敗解決

Tlimited發表於2018-06-19

基於centOS7環境,最近在搞ssh免密碼登入,但是發現執行完下面程式碼,還是沒效果,程式碼如下:

$ ssh-keygen -t rsa
$ cd .ssh
$ cp id_rsa.pub authorized_keys
$ chmod 600 authorized_keys 

執行效果:
輸入ssh-keygen -t rsa後一直按Enter鍵,好像有3次

[hadoop@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa): 
Created directory '/home/hadoop/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
9c:71:ea:40:13:5b:92:26:ed:df:bb:19:c3:ef:25:16 hadoop@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|     .o..        |
|    . +=         |
|     ++ . .      |
|     ..o =       |
|      ..S.  E    |
|       o...  .   |
|        . +.o .  |
|          .* o   |
|          ooo    |
+-----------------+
[hadoop@localhost ~]$ cd .ssh
[hadoop@localhost .ssh]$ ls
id_rsa  id_rsa.pub
[hadoop@localhost .ssh]$ cp id_rsa.pub authorized_keys
[hadoop@localhost .ssh]$ chmod 600 authorized_keys 

登入效果,登入兩次發現失敗,還是需要密碼

[hadoop@localhost ~]$ ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is 4f:e2:e4:ca:9d:db:46:e7:72:a1:83:3e:09:27:6a:ef.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
hadoop@localhost's password: 
Last failed login: Tue Jun 19 14:40:00 CST 2018 from :0 on :0
There was 1 failed login attempt since the last successful login.
Last login: Tue Jun 19 14:24:32 2018 from master
[hadoop@localhost ~]$ exit
登出
Connection to localhost closed.
[hadoop@localhost ~]$ ssh localhost
hadoop@localhost's password: 
Last login: Tue Jun 19 18:39:11 2018 from localhost
[hadoop@localhost ~]$ exit
登出
Connection to localhost closed.

解決思路

情況一:

我之前用root使用者做過ssh免密碼登入,所以導致有可能存在衝突,解決需要切換到root使用者,刪除root下的.ssh資料夾,由於.ssh是隱藏目錄,得用ls -a才能看到

[hadoop@localhost ~]$ su
密碼:
[root@localhost hadoop]# cd ~
[root@localhost ~]# ls
anaconda-ks.cfg                jdk-7u80-linux-x64.tar.gz  模板  文件  桌面
apache-maven-3.5.3-bin.tar.gz  v2.6.1.zip                 視訊  下載
initial-setup-ks.cfg           公共                       圖片  音樂
[root@localhost ~]# cd ~/.ssh
[root@localhost .ssh]# ls
authorized_keys  known_hosts
[root@localhost .ssh]# cd ~
[root@localhost ~]# ls -a
.                              .cache                     .local        公共
..                             .config                    .m2           模板
anaconda-ks.cfg                .cshrc                     .mozilla      視訊
apache-maven-3.5.3-bin.tar.gz  .dbus                      .ssh          圖片
.bash_history                  .esd_auth                  .tcshrc       文件
.bash_logout                   .ICEauthority              v2.6.1.zip    下載
.bash_profile                  initial-setup-ks.cfg       .viminfo      音樂
.bashrc                        jdk-7u80-linux-x64.tar.gz  .xauth3XmQP7  桌面
[root@localhost ~]# rm -r .ssh
rm:是否進入目錄".ssh"? yes
rm:是否刪除普通檔案 ".ssh/known_hosts"?yes
rm:是否刪除普通檔案 ".ssh/authorized_keys"?yes 
rm:是否刪除目錄 ".ssh"?yes
[root@localhost ~]# ls -a
.                              .cache                     .local        模板
..                             .config                    .m2           視訊
anaconda-ks.cfg                .cshrc                     .mozilla      圖片
apache-maven-3.5.3-bin.tar.gz  .dbus                      .tcshrc       文件
.bash_history                  .esd_auth                  v2.6.1.zip    下載
.bash_logout                   .ICEauthority              .viminfo      音樂
.bash_profile                  initial-setup-ks.cfg       .xauth3XmQP7  桌面
.bashrc                        jdk-7u80-linux-x64.tar.gz  公共
[root@localhost ~]# su -hadoop

刪除了.ssh隱藏資料夾後退出root使用者再執行ssh生成金鑰操作一次即可,也就是文章最開頭的那四行程式碼

情況二:

執行下面程式碼

$ ssh-keygen -t rsa
$ cd .ssh
$ cp id_rsa.pub authorized_keys

忘記給許可權了,也就是忘記執行下面這一行程式碼了

$ chmod 600 authorized_keys 

如果還是不行,重新做一次,去到當前使用者的home目錄下,刪除.ssh資料夾,再來一次,ls -a 可以顯示隱藏資料夾

[hadoop@localhost ~]$ ls -a
.              .bash_profile  .esd_auth      .ssh      視訊  音樂
..             .bashrc        .ICEauthority  .viminfo  圖片  桌面
.bash_history  .cache         .local         公共      文件
.bash_logout   .config        .mozilla       模板      下載
[hadoop@localhost ~]$ rm -r .ssh
[hadoop@localhost ~]$ ls -a
.              .bash_logout   .cache     .ICEauthority  .viminfo  視訊  下載
..             .bash_profile  .config    .local         公共      圖片  音樂
.bash_history  .bashrc        .esd_auth  .mozilla       模板      文件  桌面
[hadoop@localhost ~]$ 

如果想要無密碼登入其他機器,請看我這篇部落格:https://blog.csdn.net/u014204541/article/details/80762794

相關文章