秘密鍵と公開鍵が同じディレクトリにあると、SSH が失敗する

秘密鍵と公開鍵が同じディレクトリにあると、SSH が失敗する

SSH を使用してリモート サーバーに接続しようとすると、非常に奇妙な問題が発生します。

私はこれをコマンドラインから実行しており、秘密鍵と公開鍵の両方が現在のディレクトリにあります。それぞれ id_rsa と id_rsa.pub という名前が付けられています。フィンガープリントによって、公開鍵と秘密鍵が一致していることを確認しました。

次のコマンドを発行すると:

ssh -vT -i ./id_rsa ユーザー@リモートホスト

次のエラーが表示されます: アクセス許可が拒否されました (公開キー)。

ただし、id_rsa.pub の名前を別の名前に変更すると、正常に動作します。この原因は何でしょうか? リモート サーバーの設定が原因でしょうか?

id_rsa.pub が同じディレクトリにある場合の -vT からの出力は次のようになります (失敗します)。

OpenSSH_6.1p1, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 50: Applying options for *
debug1: Connecting to remotehost port 22.
debug1: Connection established.
debug1: identity file ./id_rsa type 1
debug1: identity file ./id_rsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3p1 Debian-3ubuntu7
debug1: match: OpenSSH_5.3p1 Debian-3ubuntu7 pat OpenSSH_5*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.1
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 <removed>
debug1: Host remotehost is known and matches the RSA host key.
debug1: Found key in /home/user/.ssh/known_hosts:10
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
Ubuntu 10.04.4 LTS
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: ./id_rsa
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

id_rsa.pub の名前を変更したときのデバッグ出力は次のとおりです。

OpenSSH_6.1p1, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 50: Applying options for *
debug1: Connecting to remotehost port 22.
debug1: Connection established.
debug1: identity file ./id_rsa type -1
debug1: identity file ./id_rsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_53p1     Debian-3ubuntu7
debug1: match: OpenSSH_5.3p1 Debian-3ubuntu7 pat OpenSSH_5*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.1
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 <removed>
debug1: Host remotehost is known and matches the RSA host key.
debug1: Found key in /home/user/.ssh/known_hosts:10
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
Ubuntu 10.04.4 LTS
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: ./id_rsa
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
Enter passphrase for key './id_rsa':
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to reoteserver:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8

答え1

公開鍵と秘密鍵が一致しない状態で症状を再現できました。authorized_keys で両方の鍵が許可されていても、公開鍵と秘密鍵が一致しないとログインに失敗します。

私の知る限りでは、次のようなことが起こります。

  1. クライアントは秘密鍵が暗号化されていることを通知する
  2. クライアントが公開鍵ファイルを読み取る
  3. クライアントはこのキーをサーバーに提供します
  4. サーバーは公開鍵を受け入れる
  5. クライアントがパスワードを要求する
  6. ユーザーがパスワードを入力する
  7. クライアントは一致しない秘密鍵を使用して認証を続行します

公開鍵を削除すると、クライアントはサーバーが鍵を受け入れるかどうかわからないままパスワードを要求します。つまり、秘密鍵のパスワードを入力するように求められても、サーバーがそれを受け入れないことが判明することになります。

答え2

これはOpenSSHのバグか、サーバーの鍵authorized_keysと秘密鍵が一致していない可能性があります。認証が成功すると、

debug1: identity file ./id_rsa type -1

つまり、OpenSSH はその段階で ID ファイル (公開鍵だと思います) を読み込むことができません。ソース コードの鍵読み込み部分には、次のスニペット ( authfile.c) があります。

/* try ssh2 public key */
pub = key_new(KEY_UNSPEC);
if (key_try_load_public(pub, filename, commentp) == 1)
    return pub;
if ((strlcpy(file, filename, sizeof file) < sizeof(file)) &&
    (strlcat(file, ".pub", sizeof file) < sizeof(file)) &&
    (key_try_load_public(pub, file, commentp) == 1))
    return pub;

つまり、OpenSSH はパラメータ + ".pub" で指定されたものを公開鍵としてロードしよう-iとし、ログに示されているように成功します。現在のディレクトリに ".pub" というサフィックスを持つ公開鍵がないと、これは失敗します。後で認証 ( sshconnect2.c) を実行するとき:

/*
 * send a test message if we have the public key. for
 * encrypted keys we cannot do this and have to load the
 * private key instead
 */
    if (id->key && id->key->type != KEY_RSA1) {
        debug("Offering %s public key: %s", key_type(id->key),
            id->filename);
        sent = send_pubkey_test(authctxt, id);
    } else if (id->key == NULL) {
        debug("Trying private key: %s", id->filename);
        id->key = load_identity_file(id->filename);
        if (id->key != NULL) {
            id->isprivate = 1;
            sent = sign_and_send_pubkey(authctxt, id);
            key_free(id->key);
            id->key = NULL;
        }
    }

公開鍵が存在する場合、OpenSSH はそれをテスト メッセージ (?) として送信しますが、何らかの理由で失敗します。公開鍵が事前にロードされていない場合は、秘密鍵を試して成功します。

公開キーで失敗するのはなぜかわかりません (時間があれば、もっと調べてみます)。.ssh/他のパスと比較して、ファイルの処理方法が一致していないか、キーに不一致がある可能性があります。

答え3

おそらく、これは権限の問題です。フォルダの権限をチェックして、770but740または類似の権限になっていないことを確認してください。ディレクトリを使用していない場合、.sshこれが発生している問題の原因となる可能性があります。

修正するには、 を使用しますchmod o-w /root非常にホーム フォルダーのアクセス許可設定は難しいため、これらのキーには専用のフォルダーを使用することをお勧めします。

関連情報