SSH Key 접속 시 password를 요구하는 문제 해결 방법


ssh 접속하는 방법 중 rsa key를 이용하여 password없이 접속할 수 있다.

설정 방법은 많이 공유되어있다.


특정 계정의 .ssh 디렉토리안에 authorized_keys 파일을 생성 후 안에 내용을 rsa_key.pub 파일의 내용으로 작성하고

sshd_config 파일을 수정하면 ssh -i id_rsa user@host 명령어로 password 없이 접속이 가능하다.


하지만, 설정을 다하여도 접속이 안되는 경우가 있다.

이럴 경우 다음 순서로 확인이 필요하다.


1. authorized_keys 권한 확인하기

authorized_keys 파일의 권한에서 소유자만 읽고 쓰는 권한을 가지고 groupother에는 쓰기 권한이 없어야한다.

만약 쓰기 권한이 있다면 처리가 불가능하다.

[user@localhost ~/.ssh]$ chmod 600 authorized_keys
[user@localhost ~/.ssh]$ ls -al

-rw-------  8 user   user   403 Jan 25 11:54 authorized_keys


2. ssh 접속 상세 로그 확인하기

-v 옵션을 사용하여 상세 로그를 확인 할 수 있다.

  • ex) ssh - v -i id_rsa user@host
[user@localhost ~/.ssh]$ ssh -v -i id_rsa user@host

debug1: Unspecified GSS failure.  Minor code may provide more information
Cannot determine realm for numeric host address

debug1: Next authentication method: publickey
debug1: Offering RSA public key: id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: password
user@host's password :

명령어를 실행하여 로그를 확인하였을 때,

debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password

라고 나오게 되면 접속하려는 서버의 auth log를 확인해야한다.


3. Auth log 확인하기

centos 의 경우는 /var/log/secure 파일을 확인한다.

Jan 25 13:50:17 centos70-64-170330 sshd[29923]: 
Authentication refused: bad ownership or modes for directory /home/user

위와 같은 오류를 확인하였다면, 접속하려는 서버의 해당 계정 디렉토리 권한을 확인해야한다.


4. 디렉토리 권한 확인하기

drwxrwx---+  8 user   user   4096 Jan 25 11:54 user

해당 계정의 디렉토리 권한이 770으로 잡혀 있었다.

ssh 접속을 하기 위해서는, 해당 계정 디렉토리 권한이 755를 초과해서는 안된다.

[user@localhost /home]$ chmod 755 user
[user@localhost /home]$ ls -al

drwxr-xr-x+  8 user   user   4096 Jan 25 11:54 user

이제 다시 ssh key로 접속하게 되면 password없이 정상적으로 로그인되는 것을 확인할 수 있다.


참고