歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux遠程登錄ssh免密碼

Linux遠程登錄ssh免密碼

日期:2017/2/28 16:01:23   编辑:Linux教程

(一)問題:

假如我們現在有兩台機器:ServerA和ServerB,現在想要讓ServerA不用輸入密碼就能夠進行訪問。

(二)方法和原理:

我們使用ssh-keygen在ServerA上生成private和public密鑰,將生成的public密鑰拷貝到遠程機器ServerB上後,就可以使用ssh命令無需密碼登錄到另外一台機器ServerB上。

在linux系統中,ssh是遠程登錄的默認工具,因為該工具的協議使用了RSA/DSA的加密算法【默認是DSR算法】,該工具做linux系統的遠程管理是非常安全的。

(三)實驗步驟:

1.登錄ServerA

2.ssh-keygen -t rsa,將會生成公鑰和私鑰文件id_rsa和id_rsa.pub【如果一直回車下去,最後這兩個文件應該在/home/$USER/.ssh下面】

3.將 .pub 文件復制到ServerB機器的 .ssh 目錄下,並保存為authorized_keys

可以使用:

ssh-cop-id命令會將指定的公鑰文件復制到遠程計算機

[Oracle@ www.linuxidc.com ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
28
[email protected]'s password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:


.ssh/authorized_keys


to make sure we haven't added extra keys that you weren't expecting.


[oracle@ www.linuxidc.com ~]$ ssh [email protected]
[email protected]'s password:
Last login: Thu Nov 24 16:05:32 2011 from 192.168.55.229
[oracle@ www.linuxidc.com ~]$

4.大功告成,從A機器登錄B機器的目標賬戶,不再需要密碼了

5.設置文件和目錄權限【這一步可以省略,但是為了安全起見,加上也是有必要的~】

設置authorized_keys權限

chmod 600 authorized_keys

設置.ssh目錄權限

chmod 700 -R .ssh

6.要保證.ssh和authorized_keys都只有用戶自己有寫權限。否則驗證無效。(今天就是遇到這個問題,找了好久問題所在),其實仔細想想,這樣做是為了不會出現系統漏洞。

報錯:

The authenticity of host '192.168.20.59 (192.168.20.59)' can't be established.
RSA key fingerprint is 6a:37:c0:e1:09:a4:29:8d:68:d0:ca:21:20:94:be:18.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.20.59' (RSA) to the list of known hosts.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied (publickey,gssapi-with-mic,password).

ps:

如果我們添加公鑰後,還是無法訪問,也可能是ServerA主機上面的一個文件known_hosts中已經存在ServerB的ip信息,刪除重新訪問即可。

另外,如果以上方法嘗試過後還是不行,可能是因為登錄失敗次數過多被鎖定,這個時候我們就需要去看看我們的系統日志了/var/log/messages,將之前的鎖定信息日志刪除就可以了。

Copyright © Linux教程網 All Rights Reserved