
我一直將 ssh 身份檔案放在我的~/.ssh/
資料夾中。我那裡大概有大約 30 個文件。
當我連接到伺服器時,我將指定要使用的身份文件,例如
ssh -i ~/.ssh/client1-identity[電子郵件受保護]
但是,如果我不指定身分文件,而只使用以下內容:
SSH[電子郵件受保護]
我收到錯誤
user123 的身份驗證失敗次數太多
我理解這是因為如果沒有指定身份文件,並且 ssh 可以找到身份文件,那麼它將嘗試所有這些文件。
我還了解我可以編輯該~/.ssh/config
文件並指定如下內容:
託管 example.com PreferredAuthentications 鍵盤交互,密碼
以防止該連線嘗試已知的身份檔案。
所以,我想我可以將我的身份文件移到目錄之外~/.ssh/
,或者我可以在配置文件中指定我想要禁用身份文件身份驗證的每個主機,但是有沒有辦法告訴 SSH 默認情況下不要搜索身份文件?或指定它將搜尋的內容?
答案1
您可以使用該IdentitiesOnly=yes
選項IdentityFile
(請參閱ssh_config 手冊頁)。這樣,您就可以指定它應該尋找哪些檔案。
在此範例中,ssh 將僅有的查看 ssh_config 檔案中給出的身份 + 命令列上列出的 4 個身份(代理提供的身份將被忽略):
ssh -o IdentitiesOnly=yes \
-o IdentityFile=id1.key \
-o IdentityFile=id2.key \
-i id3.key \
-i id4.key \
[email protected]
形式-i
和-o IdentityFile=
是可以互換的。
在 中.ssh/config
,您可以包含以下配置:
Host example
User user123
Hostname example.com
IdentityFile ~/.ssh/id_rsa_example
IdentityFile ~/.ssh/id_rsa_example2
IdentitiesOnly yes
答案2
user76528的簡短回答是正確的,但我剛剛遇到這個問題,並認為一些詳細說明會很有用。如果您想知道“為什麼 ssh 忽略我的身份文件配置選項”,您可能也會關心這個解決方案?
首先,與 ssh_config 中的所有其他選項不同,ssh 不使用IdentityFile
它找到的第一個選項。相反,該IdentityFile
選項將該檔案新增至所使用的身分清單。您可以堆疊多個IdentityFile
選項,ssh 用戶端將嘗試所有選項,直到伺服器接受其中一個或拒絕連線。
其次,如果您使用 ssh-agent,ssh 將自動嘗試使用代理程式中的金鑰,即使您沒有使用 in 的ssh_config
(IdentityFile
或-i
)選項來指定它們。這是您可能會收到錯誤的常見原因Too many authentication failures for user
。使用該IdentitiesOnly yes
選項將停用此行為。
如果您作為多個使用者透過 ssh 連接到多個系統,我建議您將IdentitiesOnly yes
其放入 的全域部分ssh_config
,並將每個部分放入IdentityFile
適當的主機子部分中。
答案3
我通常是這樣做的:
$ ssh -o IdentitiesOnly=yes -F /dev/null -i ~/path/to/some_id_rsa [email protected]
選項如下:
-o IdentitiesOnly=yes
- 告訴 SSH 僅使用透過 CLI 提供的金鑰,而不使用來自$HOME/.ssh
ssh-agent 或透過 ssh-agent提供的金鑰-F /dev/null
- 禁止使用$HOME/.ssh/config
-i ~/path/to/some_id_rsa
- 您明確想要用於連線的金鑰
例子
$ ssh -v -o IdentitiesOnly=yes -F /dev/null -i ~/my_id_rsa [email protected]
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /dev/null
debug1: Connecting to someserver.mydom.com [10.128.12.124] port 22.
debug1: Connection established.
debug1: identity file /Users/sammingolelli/my_id_rsa type 1
debug1: identity file /Users/sammingolelli/my_id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5*
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA f5:60:30:71:8c:a3:da:a3:fe:b1:6d:0b:20:87:23:e1
debug1: Host 'someserver' is known and matches the RSA host key.
debug1: Found key in /Users/sammingolelli/.ssh/known_hosts:103
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/sammingolelli/my_id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to someserver.mydom.com ([10.128.12.124]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
Last login: Tue Dec 8 19:03:24 2015 from 153.65.219.15
someserver$
請注意,在上面的輸出中,ssh
僅透過 CLI 識別了my_id_rsa
私鑰,並使用它連接到某個伺服器。
具體來說這些部分:
debug1: identity file /Users/sammingolelli/my_id_rsa type 1
debug1: identity file /Users/sammingolelli/my_id_rsa-cert type -1
和:
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/sammingolelli/my_id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
答案4
使用 IdentityFile 但繼續使用 ssh-agent 以避免再次提示密碼
公認的使用解決方案IdentitiesOnly yes
意味著您將永遠無法利用 ssh-agent,導致在載入金鑰時重複提示您輸入密碼。
若要繼續使用ssh-agent
並避免「身份驗證失敗次數過多」錯誤,請嘗試以下操作:
刪除所有自動將密鑰載入到
ssh-agent
.新增
AddKeysToAgent yes
到客戶端的 ssh 配置。這將在第一次連線時提示您輸入密碼,然後將金鑰新增至您的代理程式。ssh-add -D
當您收到“太多身份驗證”錯誤時使用。這只是“重置”(刪除)您的 ssh 代理快取。然後在同一會話中再次嘗試連線。系統將提示您輸入密碼,一旦接受,它將被添加到您的代理中。由於您的代理中只有一把密鑰,因此您將被允許連接。 ssh-agent 仍然存在,以便在同一會話期間進行將來的連接,以避免重新提示。Host ex example.com User joe HostName example.com PreferredAuthentications publickey,password IdentityFile /path/to/id_rsa AddKeysToAgent yes