본문 바로가기
리눅스

SSH 공개키 authorized_keys 경로 변경

by 좀빅 2020. 5. 7.

CentOS 6/7 기준!

 

OpenSSH 공개키를 배포 시에 특정 계정이 /home 밑에 생성되는 것이 아닌 /var/lib 등 공유 디렉터리에서 생성 시에 authorized_keys 파일을 참조하지 않는 상황이 발생 SELinux를  Disabled 혹은 Permissive 하면 해결된 문제였으나, 훌륭하신 과장님께서 해결 방안을 찾아주셨다.

 


키 생성 및 기본적인 공개키 배포 실시

Server 작업

useradd -d /var/lib/test_ssh test_ssh

passwd test_ssh
#test_ssh 사용자의 비밀 번호 변경 중
#새  암호: test.123
#잘못된 암호: 암호에 어떤 형식으로 사용자 이름이 포함되어 있습니다
#새  암호 재입력: test.123
#passwd: 모든 인증 토큰이 성공적으로 업데이트 되었습니다.

su - test_ssh

pwd
#/var/lib/test_ssh

mkdir .ssh

chmod 700 .ssh

Client 작업

*copy-id가 없을 경우 수동으로 넣어주면됩니다.

ssh-keygen -t rsa
#해당질문에 알아서 Enter

ssh-copy-id test_ssh@[server IP]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "[홈 디렉터리명]/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
test_ssh@[server IP]'s password:

Number of key(s) added:        1

Now try logging into the machine, with:   "ssh 'test_ssh@[server IP]'"
and check to make sure that only the key(s) you wanted were added.

해당 작업이 완료된다면 기본적으로 test_ssh/.ssh 밑에 authorized_keys파일이 생성됨

cd .ssh
ls -ltr
#합계 4
#-rw-------. 1 test_ssh test_ssh 567  5월  7 17:51 authorized_keys

 


로그인시 동일하게 패스워드를 물어봐 버리네?

ssh test_ssh@[server IP]
test_ssh@[server IP]1's password:

 

CentOS 6버전에서 발생하는 SElinux버그다.

기본적으로 User home디렉터리를 지정생성시 자동으로 Selinux에 추가 반영을 해야하나,

하지 않아서 발생하는 문제다. 

*CentOS 7에서는 해당 버그는 패치되었다.


해결책

해결책은 2개다.

1) SELinux Disable

2) vi /etc/selinux/targeted/contexts/files/file_contexts 에서 필드를 수정

 

보안을 위해서라면 2번 방법이 안정적이다.

vi /etc/selinux/targeted/contexts/files/file_contexts

/var/lib/[^/]+/\.ssh(/.*)?     system_u:object_r:ssh_home_t:s0
#/var/lib로 시작하는 지점에 적절한 위치에 추가한다.
#SELinux정규식 구문이니 필요한것 찾아서!

su - test_ssh
#이후 사용자 계정으로 로그인

restorecon -R -v .ssh/
#수정된 SELinux 보안 문맥을 맞추기위해 restorecon 명령어를 사용한다.

#restorecon reset /var/lib/test_ssh/.ssh context unconfined_u:object_r:var_lib_t:s0->unconfined_u:object_r:ssh_home_t:s0
#restorecon reset /var/lib/test_ssh/.ssh/authorized_keys context unconfined_u:object_r:var_lib_t:s0->unconfined_u:object_r:ssh_home_t:s0
#출력은 위와 같다.

 

댓글