伺服器怎麼使用ssh實現免密碼登陸?

Tags: 密碼, 伺服器,

每次使用SSH就等複製貼上一次密碼,像小專案在操作的時候,有時候一天要登

陸N次有木有,下面分享一下怎麼實現SSH免密碼登陸

方法/步驟

[[email protected] .ssh]# /usr/bin/ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

30:f6:d7:2a:ac:56:eb:3f:fa:40:25:8d:90:96:68:cb

[email protected]

在主機/.ssh目錄下生成密匙

使用ssh的rsa密匙

其中

id_rsa 私匙

id_rsa.pub 公匙

下述命令產生不同型別的金鑰

ssh-keygen -t dsa

ssh-keygen -t rsa

ssh-keygen -t rsa1

[[email protected] .ssh]# scp /root/.ssh/[email protected]:/root/.ssh/authorized_keys

The authenticity of host '172.16.142.5 (172.16.142.5)' can't be established.

RSA key fingerprint is 4b:a5:74:fb:2e:08:60:af:fa:76:d4:b0:26:4c:13:75.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.16.142.5' (RSA) to the list of known hosts.

[email protected]'s password:

id_rsa.pub 100% 236 0.2KB/s 00:00

說明:

將公鑰拷貝到遠端主機,並寫入授權列表檔案

你也可以把公鑰檔案拷貝過去後,在遠端主機下直接執行

touch /root/.ssh/authorized_keys

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

操作完畢,登陸檢查。

[[email protected] .ssh]# ssh 172.16.142.5

[email protected]'s password:

請注意此時如果仍提示輸入密碼,請檢查如下資料夾和檔案的操作許可權,這是非常重要的, 否則ssh公鑰認證體制不能正常工作:

172.16.142.4(客戶端)

/home/root資料夾的許可權是600

/home/root/.ssh資料夾的許可權是600 (好像這個許可權關係不是很大)

/home/root/.ssh/id_dsa私鑰的許可權600

172.16.142.5(遠端主機)

/home/root資料夾的許可權是644

/home/root/.ssh資料夾的許可權是644 (好像這個許可權關係不是很大)

/root/.ssh/authorized_keys公鑰的許可權644

[[email protected] ~]# ssh 172.16.142.5

Last login: Sat Dec 15 21:10:17 2007 from 172.16.142.4

[[email protected] ~]#

無密碼SSH登陸成功!

相關問題答案