如果我使用非預設金鑰文件,SSH 會不斷請求我的密碼

如果我使用非預設金鑰文件,SSH 會不斷請求我的密碼

我有一個名為 的密鑰文件KEY,其中包含KEYKEY.pub。我將 pub 上傳到authorized_keys,並添加 private

ssh-add /home/user/.ssh/KEY

但是當我嘗試連接時,它一直要求我輸入密碼。ssh [email protected]

如果我產生一個金鑰ssh-keygen並保留預設金鑰名稱,上傳 pub 並載入 private,它才不是請求密碼。

問題可能是什麼?

答案1

您可以獲得偵錯輸出,它可能會告訴您它無法使用(ssh 的預設金鑰檔案)進行身份驗證。答案是告訴 ssh 使用哪個金鑰:ssh -vvv [email protected]~/.ssh/id_rsa

ssh -i /home/user/.ssh/KEY [email protected]

您也可以將每個主機的密鑰檔案新增至您的 中.ssh/config,然後您只需鍵入即可ssh host.com自動選擇使用者/密鑰。

.ssh/config 的範例條目(更多資訊請參閱man ssh_config):

Host mysshserver ssh.host.com
HostName ssh.host.com
User myusername
IdentityFile ~/.ssh/mykeyfile


密鑰檔的解釋來自man ssh

 -i identity_file
             Selects a file from which the identity (private key) for RSA or
             DSA authentication is read.  The default is ~/.ssh/identity for
             protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for pro‐
             tocol version 2.  Identity files may also be specified on a per-
             host basis in the configuration file.  It is possible to have
             multiple -i options (and multiple identities specified in config‐
             uration files).

答案2

正確設定密鑰檔(操作方法這裡),請注意以下事項:

如果您的主機.ssh目錄和檔案沒有正確的權限和/或您的遠端使用者主目錄沒有正確的權限,ssh 將繼續要求輸入密碼,儘管它找到了金鑰檔案。

您可以查看您的金鑰檔案是否正在使用ssh -vvv user@host

輸出範例:

debug1: Offering DSA public key: /Users/<user>/.ssh/id_dsa
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password

檢查遠端使用者主目錄和遠端.ssh目錄權限

例如,權限應該是:

$ls -ld .ssh
drwx------ 2 <owner> <group> 4096 2011-12-29 20:39 .ssh

$ls -ld ~/
drwxr-xr-x 28 <owner> <group> 4096 2011-12-29 20:15 /home/<user>/ 

相關內容