Linux,Unix,BSD

RSA 키 방식으로 ssh 자동 로그인이 안되는 문제

채윤아빠 2019. 6. 17. 14:27
728x90
반응형



문제점 및 증상

클라이언트에서 생성한 id_rsa.pub 파일을 서버의 로그인 대상 계정에 있는 ".ssh/authorized_keys" 파일에 등록한 이후에 ssh로 접속하면 다음과 같이 로그인이 실패하고, 비밀번호를 묻는 문제가 발생하였습니다.

$ sudo ssh min@www.min.kr -v
OpenSSH_7.2p2 Ubuntu-4ubuntu2.8, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to www.min.kr [000.000.000.000] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to www.min.kr:22 as 'min'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:c7q1/FynomHYJmznxlNDLfHU6PGxTFisfUQM+Fr590E
debug1: Host '[www.min.kr]:22' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:5
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /root/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password
min@www.min.kr's password:

위 로그중에서 "Next authentication method: publickey", "Authentications that can continue: publickey,password"로 검색한 결과를 확인합니다.


해결 방안

ssh는 ".ssh" 폴더 및 키 파일들에 다른 사용자가 쓰기 권한을 갖을 수 있다면 키를 전송하지 않습니다. 클라이언트 및 서버 양쪽에서 폴더 및 파일에 대한 권한을 확인해 주어야 합니다.
파일에 대한 권한 및 소유자를 확인하지 않도록 하기 위해서는 ssh 서비스의 설정 (/etc/ssh/sshd_config)에서 "StrictModes"를 "no"로 설정하고, "sshd" 서비스를 재시작합니다.

아래와 같이 서버의 대상 계정에 접속하여, 다음과 같이 ".ssh" 폴더 및 폴더안의 모든 파일에 대한 권한을 변경한 이후에 정상적으로 RSA 키를 이용하여 자동으로 로그인되는 것을 확인 하였습니다.

$ cd ~/.ssh
$ chmod 700 .
$ ll
total 48
-rw-rw-r-- 1 min min 4417 Jun 17 13:43 authorized_keys
-rw------- 1 min min 1675 May 21 20:51 id_rsa
-rw-r--r-- 1 min min  413 May 21 20:51 id_rsa.pub
$ chmod 600 *
$ ll
total 48
-rw------- 1 min min 4417 Jun 17 13:43 authorized_keys
-rw------- 1 min min 1675 May 21 20:51 id_rsa
-rw------- 1 min min  413 May 21 20:51 id_rsa.pub


참고자료