
私は自分のフォルダー内に ssh アイデンティティ ファイルを保存しています~/.ssh/
。そこにはおそらく約 30 個のファイルがあります。
サーバーに接続するときに、使用するIDファイルを次のように指定します。
ssh -i ~/.ssh/client1-identity[メールアドレス]
ただし、ID ファイルを指定せず、次のようなものを使用した場合:
ssh[メールアドレス]
エラーが発生します
user123 の認証失敗が多すぎます
これは、ID ファイルが指定されておらず、ssh が ID ファイルを見つけることができる場合、それらすべてを試行するためだと理解しています。
~/.ssh/config
また、ファイルを編集して次のような内容を指定できることも理解しています。
ホスト example.com 優先認証 キーボードインタラクティブ、パスワード
その接続が既知の ID ファイルを試行するのを防ぐためです。
つまり、アイデンティティ ファイルをディレクトリ外に移動するか~/.ssh/
、設定ファイルでアイデンティティ ファイル認証を無効にする各ホストを指定することはできると思いますが、デフォルトで SSH にアイデンティティ ファイルを検索しないように指示する方法はありますか? または、検索するファイルを指定する方法は?
答え1
オプションIdentitiesOnly=yes
はIdentityFile
(ssh_config マニュアルページ)。こうすることで、どのファイルを検索するかを指定できます。
この例では、sshはのみssh_config ファイルに指定された ID と、コマンド ラインにリストされている 4 つの ID を調べます (エージェントによって提供される ID は無視されます)。
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 は identityfile 設定オプションを無視するのか」と疑問に思ったことがあるなら、この解決策も気になるかもしれません。
まず、ssh_config の他のオプションとは異なり、ssh は最初にIdentityFile
見つかったものを使用しません。代わりに、このIdentityFile
オプションは、そのファイルを使用する ID のリストに追加します。複数のIdentityFile
オプションを積み重ねることができ、ssh クライアントは、サーバーが 1 つを受け入れるか接続を拒否するまで、それらすべてを試行します。
ssh_config
次に、ssh-agent を使用する場合、のIdentityFile
(または) オプションでキーを指定していない場合でも、ssh は自動的にエージェント内のキーを使用しようとします-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経由では使用しないように指示します。-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$
上記の出力では、CLI 経由でssh
のみ秘密鍵が識別され、それを使用して someserver に接続していることに注意してください。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
。クライアントの SSH 設定に追加します
AddKeysToAgent yes
。これにより、最初の接続時にパスフレーズの入力が求められますが、その後、エージェントにキーが追加されます。「認証が多すぎます」というエラーが発生したときに使用します
ssh-add -D
。これは単に ssh-agent キャッシュを「リセット」(削除) するだけです。その後、同じセッション内で再度接続を試みます。パスフレーズの入力を求められます。パスフレーズを受け入れると、パスフレーズがエージェントに追加されます。エージェントにはキーが 1 つしかないため、接続が許可されます。ssh-agent は、同じセッション中の将来の接続でも引き続き存在し、再度プロンプトが表示されないようにします。Host ex example.com User joe HostName example.com PreferredAuthentications publickey,password IdentityFile /path/to/id_rsa AddKeysToAgent yes