동일한 디렉터리에 있는 개인 키와 공개 키로 인해 ssh가 실패합니다.

동일한 디렉터리에 있는 개인 키와 공개 키로 인해 ssh가 실패합니다.

SSH를 사용하여 원격 서버에 연결하려고 할 때 정말 이상한 문제가 있습니다.

저는 명령줄에서 이 작업을 수행하고 있으며 개인 키와 공개 키는 모두 현재 디렉터리에 있습니다. 이름은 각각 id_rsa 및 id_rsa.pub입니다. 공개 키와 개인 키가 일치하는지 지문을 통해 확인했습니다.

다음 명령을 실행할 때:

ssh -vT -i ./id_rsa user@remotehost

다음 오류가 발생합니다. 권한이 거부되었습니다(공개 키).

그러나 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

권한 문제인 것이 거의 확실합니다. 폴더 권한을 확인하여 폴더 권한이 비슷하지 않은지 확인 770하세요 740. 디렉터리 를 사용하지 않는 경우 .ssh발생한 문제가 쉽게 발생할 수 있습니다.

수정하려면 를 사용하세요 chmod o-w /root. 나고도로홈 폴더에 대한 권한 설정은 까다롭기 때문에 이러한 키에 대한 전용 폴더를 사용하는 것이 좋습니다.

관련 정보