리눅스 SSH RSA 비대칭키로 패스워드 없이 접속

리눅스에서 패스워드 입력 없이 SSH 접속하려면 expect나 sshpass 등을 활용할 수 있겠으나, RSA 비대칭키를 활용하면 패스워드를 평문으로 입력하지 않아도 되기 때문에 보안상 좀 더 안전하다고 판단된다.

RSA 비대칭키 접속 방법은 아래와 같다.

1. 접속을 시도하는(출발지) 서버에는 Private Key가 있어야 한다.
2. 접속 대상이 되는(목적지) 서버에는 Public Key가 있어야 한다.

아래와 같은 시나리오로 테스트를 해보겠다.

출발지 서버 : 192.168.0.5
출발지 계정 : test01
목적지 서버 : 192.168.0.6
목적지 계정 : test02

먼저 출발지 서버의 test01 계정에서 RSA 비대칭키 쌍을 생성해보자.
그냥 ssh-keygen 명령어를 실행하고 엔터만 몇번 누르면 된다.

[test01@centos ~]$
[test01@centos ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test01/.ssh/id_rsa):
Created directory '/home/test01/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/test01/.ssh/id_rsa.
Your public key has been saved in /home/test01/.ssh/id_rsa.pub.
The key fingerprint is:
39:ab:a9:a2:8f:48:8f:93:51:c6:cf:4a:10:7b:20:7f test01@centos.local
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|.o               |
|..=              |
| o.=E    .       |
|  =.o   S        |
| . . o   o       |
| .+ .   .        |
|o++.   o         |
|++oo..o          |
+-----------------+
[test01@centos ~]$


기본값으로 Private 키는 test01 계정 홈 디렉터리에 아래와 같이 생성되며

/home/test01/.ssh/id_rsa

Public 키는 아래와 같이 생성된다.

/home/test01/.ssh/id_rsa.pub

앞서 설명했듯이 접속을 시도하는 서버는 위 2개 파일 중 Private 키만 가지고 있으면 된다. 그럼 Public 키인 id_rsa.pub 파일은 어떻게 해야 할까? 접속하려는 목적지 서버 192.168.0.6의 test02 계정 홈 폴더 아래 다음과 같이 생성해 주면 된다. (기존에 파일이 있으면 키 내용만 추가해주면 된다.)

/home/test02/.ssh/authorized_keys

목적지 서버에 authorized_keys 파일을 생성하는 방법은 여러가지 있을 수 있겠다. 그냥 텍스트로 복사해서 vi 에디터로 파일을 생성해도 되고, scp로 복사해도 되고, 웹서버에 올려서 wget으로 다운로드 받아도 되겠다.

하지만 주의할 사항이 있다. 목적지 서버의 .ssh 디렉터리와 바로 상위 디렉터리, 그리고 authorized_keys 파일에는 Group, Others에 Write 권한이 있으면 안 된다. (기본적으로 700을 권장한다.)

authorized_keys 파일을 복사하는 간단한 방법으로 ssh-copy-id 명령어가 있다.
아래와 같이 목적지 계정@주소를 붙여서 ssh-copy-id를 실행하면
현재 계정에 있는 Public Key인 id_rsa.pub 파일을 목적지 서버 계정에 authorized_keys 파일로 복사해준다.

[test01@centos ~]$
[test01@centos ~]$ ssh-copy-id test02@192.168.0.6
The authenticity of host '192.168.0.6 (192.168.0.6)' can't be established.
RSA key fingerprint is ae:52:0d:b8:85:96:a1:08:97:1d:85:c6:c1:b2:44:2b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.6' (RSA) to the list of known hosts.
test02@192.168.0.6's password:
Now try logging into the machine, with "ssh 'test02@192.168.0.6'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[test01@centos ~]$

목적지 서버에 Public 키가 정상적으로 저장되었다면 아래와 같이 패스워드 없이 SSH 접속이 가능하게 된다.

[test01@centos ~]$
[test01@centos ~]$ ssh test02@192.168.0.6
[test02@centos58 ~]$ exit
logout
Connection to 192.168.0.6 closed.
[test01@centos ~]$

참고로 기 생성했던 RSA 비대칭키에서 Public 키를 지워버리고 Private 키만 가지고 있는 경우 아래와 같이 -y 옵션을 사용하고 -f 옵션으로 기존 Private 키를 지정하면 Public 키를 다시 생성할 수 있다.

ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub

(추가1)
SELinux가 켜져 있으면 접속이 안 될 수 있다.

(추가2)
ssh-keygen과 ssh-copy-id는 각각 아래 패키지에 들어 있다. (RHEL 계열 기준)

[root@CentOS ~]# rpm -qf /usr/bin/ssh-keygen
openssh-5.3p1-94.el6.x86_64
[root@CentOS ~]#
[root@CentOS ~]# rpm -qf /usr/bin/ssh-copy-id
openssh-clients-5.3p1-94.el6.x86_64