我一直把我的ssh身份文件放在我的〜/ .ssh /文件夾中。我可能有大約30個文件。
當我連接到服務器時,我將指定要使用的身份文件,例如
ssh -i~ / .ssh / client1-identity client1@10.1.1.10
但是,如果我沒有指定身份文件,只需使用以下內容:
ssh user123@example.com
我收到了錯誤
user123的身份驗證失敗太多
我理解這是因為如果沒有指定身份文件,並且ssh可以找到身份文件,那麼它將嘗試所有這些文件。
我也明白我可以編輯了 ~/.ssh/config
文件並指定如下內容:
主持example.com
PreferredAuthentications鍵盤交互式密碼
為了防止該連接嘗試已知的身份文件。
所以,我想我可以移動我的身份文件 ~/.ssh/
目錄,或者我可以在配置文件中指定我想要禁用身份文件身份驗證的每個主機,但有沒有辦法告訴SSH購買默認值而不搜索身份文件?或者指定它將搜索的那些?
你可以使用 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 \
user123@example.com
表格 -i
和 -o IdentityFile=
是可以互換的。
user76528的簡短回答是正確的,但我只是遇到了這個問題,並認為一些詳細說明會很有用。如果你想知道“為什麼ssh忽略我的身份文件配置選項”,你可能也會關心這個解決方案?
首先,與ssh_config中的其他選項不同,ssh不使用第一個選項 IdentityFile
它找到了。而是 IdentityFile
選項將該文件添加到使用的標識列表中。你可以堆疊多個 IdentityFile
選項,ssh客戶端將嘗試所有選項,直到服務器接受一個或拒絕連接。
其次,如果你使用ssh-agent,ssh會自動嘗試使用代理中的密鑰,即使你沒有在ssh_config的IdentityFile(或-i)選項中指定它們。這是你可能得到的常見原因 Too many authentication failures for user
錯誤。使用 IdentitiesOnly yes
選項將禁用此行為。
如果你作為多個用戶ssh到多個系統,我建議推 IdentitiesOnly yes
在ssh_config的全局部分中,並放入每個部分 IdentityFile
在適當的Host子部分中。
我通常這樣做:
$ ssh -o IdentitiesOnly=yes -F /dev/null -i ~/path/to/some_id_rsa root@server.mydom.com
選項如下:
-o IdentitiesOnly=yes
- 告訴SSH僅使用通過CLI提供的密鑰,而不使用通過CLI提供的密鑰 $HOME/.ssh
或通過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 root@someserver.mydom.com
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 no-more-sessions@openssh.com
debug1: Entering interactive session.
Last login: Tue Dec 8 19:03:24 2015 from 153.65.219.15
someserver$
注意在上面的輸出中 ssh
只是確定了 my_id_rsa
私鑰通過CLI,並使用它來連接到某個服務器。
特別是這些部分:
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).
在您有許多密鑰的情況下,您將不可避免地遇到“Too many Authentication Failures”錯誤。如果您有密碼,並且只想使用密碼登錄,請按照以下方式進行操作。
要僅使用密碼身份驗證而不使用公鑰,並且不使用有些誤導性的“鍵盤交互”(這是包含密碼的超集),您可以從命令行執行此操作:
ssh -o PreferredAuthentications=password user@example.com
使用IdentityFile但保持使用ssh-agent避免密碼短語重新提交
使用的公認解決方案 IdentitiesOnly yes
意味著你永遠無法利用ssh-agent,導致在加載密鑰時反复提示你的密碼短語。
繼續使用 ssh-agent
並避免“太多身份驗證失敗”錯誤,請嘗試以下操作:
刪除任何自動加載密鑰的交互式控制台啟動腳本 ssh-agent
。
加 AddKeysToAgent yes
到你客戶的ssh配置。這將在第一次連接時提示您輸入密碼,然後將密鑰添加到您的代理。
使用 ssh-add -D
當您收到“太多身份驗證”錯誤時。這只是'重置'(刪除)你的ssh-agent緩存。然後在同一會話中再次嘗試連接。系統將提示您輸入密碼,一旦被接受,它將被添加到您的代理。由於您的代理中只有一個密鑰,因此您將被允許連接。然後ssh-agent在同一會話期間仍然存在以用於將來的連接,以避免重新組合。
Host ex example.com
User joe
HostName example.com
PreferredAuthentications publickey,password
IdentityFile /path/to/id_rsa
AddKeysToAgent yes
ssh客戶端和ssh-agent通過Unix域套接字進行通信,該套接字的名稱通過SSH_AUTH_SOCK環境變量(由代理啟動時設置)指定給客戶端。
因此,為了防止單個調用客戶端查詢代理,可以將此變量顯式設置為無效的內容,如空字符串;
$ SSH_AUTH_SOCK= ssh user@server
像這樣的客戶端調用將無法與代理進行通信,並且只能將〜/ .ssh /中的文件或使用-i在命令行上指定的任何文件提供給服務器。
debug1: pubkey_prepare: ssh_get_authentication_socket: Connection refused
你一直都有答案(差不多):
Host *
PreferredAuthentications keyboard-interactive,password
為我工作。