溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

遠(yuǎn)程登錄ssh免密碼

發(fā)布時間:2020-07-25 07:26:17 來源:網(wǎng)絡(luò) 閱讀:677 作者:清風(fēng)一明月 欄目:關(guān)系型數(shù)據(jù)庫

問題:

假如我們現(xiàn)在有兩臺機器:ServerA和ServerB,現(xiàn)在想要讓ServerA不用輸入密碼就能夠進(jìn)行訪問。

方法和原理:

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

在linux系統(tǒng)中,ssh是遠(yuǎn)程登錄的默認(rèn)工具,因為該工具的協(xié)議使用了RSA/DSA的加密算法【默認(rèn)是DSR算法】,該工具做linux系統(tǒng)的遠(yuǎn)程管理是非常安全的。

實驗步驟:

1.登錄ServerA

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

3.將 .pub 文件復(fù)制到ServerB機器的 .ssh 目錄下,并保存為authorized_keys

可以使用:

ssh-cop-id命令會將指定的公鑰文件復(fù)制到遠(yuǎn)程計算機

[oracle@Test232 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub oracle@192.168.55.232

28

oracle@192.168.55.232's password: 

Now try logging into the machine, with "ssh 'oracle@192.168.55.232'", and check in:

.ssh/authorized_keys

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

[oracle@Test232 ~]$ ssh oracle@192.168.55.232

oracle@192.168.55.232's password: 

Last login: Thu Nov 24 16:05:32 2011 from 192.168.55.229

[oracle@Test232 ~]$ 

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

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

設(shè)置authorized_keys權(quán)限

chmod 644 authorized_keys 

設(shè)置.ssh目錄權(quán)限

chmod 700 -R .ssh

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

報錯:

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.  

root@192.168.20.59's password:   

Permission denied, please try again.  

root@192.168.20.59's password:   

Permission denied, please try again.  

root@192.168.20.59's password:   

Permission denied (publickey,gssapi-with-mic,password).  

如果說以上權(quán)限沒有問題的話,那就趕緊去看看你的配置文件吧,因為有人會喜歡改這個東東,導(dǎo)致你無論如何都不會成功:

打開/etc/ssh/ssh_config文件,找到以下部分:

#   IdentityFile ~/.ssh/identity

   IdentityFile ~/.ssh/id_rsa

看看你的私鑰名字是不是這個 ~/.ssh/id_rsa

哈哈,還是得好好看配置文件嘍~

假如有臺中控機已經(jīng)打通了到各臺應(yīng)用主機間的無密碼遠(yuǎn)程登陸,現(xiàn)在需要打通另一臺中控,下面是我寫的腳本來實習(xí)此功能:

############################################ 實現(xiàn)中控?zé)o密碼登陸 ############################################

#!/bin/bash

#Author:zhuying

scriptdir=/home/oracle/zy/changecps

for ip in `grep -v ^# "$scriptdir"/cps.ip`

do

        scp /home/oracle/zy/changecps/169keys oracle@$ip:~/ </dev/null

        ssh $ip "cat ~/169keys >> /home/oracle/.ssh/authorized_keys;rm ~/169keys" </dev/null

        ssh $ip "cat /home/oracle/.ssh/authorized_keys|sort|uniq > /home/oracle/.ssh/tmp.keys" </dev/null

        ssh $ip "mv /home/oracle/.ssh/tmp.keys /home/oracle/.ssh/authorized_keys" </dev/null

        ssh $ip "chmod 644 /home/oracle/.ssh/authorized_keys" </dev/null

done

###########################################  實現(xiàn)中控?zé)o密碼登陸 ##############################################

ps:

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

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

問題:


(1)Connection closed by $IP


可能是超過系統(tǒng)默認(rèn)失敗次數(shù)了,然后清理就ok了~【faillog -a;faillog -r】


(2)在前面步驟都正確無誤的情況下,每次遠(yuǎn)程登錄還是提示讓輸入密碼,這時候有可能是權(quán)限問題:


    chmod 644 authorized_keys再次登錄就ok了~【如果是Red Hat 5.6的話,最好改成600,否則認(rèn)為是不安全的~】


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI