본문으로 건너뛰기

ssh-key를 생성하여 서버에 패스워드 없이 접속하기

💡 요약 정리

  • 클라이언트에서 ssh-keygen 명령으로 개인키·공개키 쌍을 생성합니다.
  • 공개키(id_rsa.pub)를 서버의 계정 폴더에 authorized_keys로 복사합니다.
  • 비밀번호 없이 SSH 접속이 가능한지 테스트합니다.
  • 서버의 ssh 설정 파일에서 공개키 인증이 활성화되어 있는지 확인해야 합니다.

1. ssh-keygen 명령으로 ssh-key 생성

  • 서버에 접속하려는 클라이언트 컴퓨터에서 아래 명령어를 실행하여 ssh key를 생성합니다.
  • 생성이 완료되면 사용자 홈 디렉토리의 .ssh 폴더에 id_rsa, id_rsa.pub 두 개의 파일이 생성됩니다.
[root@localhost root]# cd /root/.ssh/
[root@localhost .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
b5:69:64:dd:16:70:b9:6d:b7:2f:36:da:39:ba:b0:e3 root@jook-test
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub  known_hosts

2. ssh-key 서버에 복사

  • 생성된 id_rsa.pub 파일을 접속하려는 서버의 계정 홈 디렉토리 .ssh 폴더 내에 authorized_keys라는 파일명으로 복사합니다.
  • 여러 대의 클라이언트가 같은 서버에 접속하려면 각 클라이언트의 id_rsa.pub 파일 내용을 서버의 authorized_keys 파일에 추가로 붙여넣으면 됩니다.
[root@localhost .ssh]# scp id_rsa.pub 192.168.100.32:/root/.ssh/authorized_keys
The authenticity of host '192.168.100.32 (10.10.100.32)' can't be established.
RSA key fingerprint is 26:fe:28:d9:de:a6:1e:36:2c:73:5e:c2:13:21:c4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.32' (RSA) to the list of known hosts.
root@192.168.100.32's password:
id_rsa.pub               100% |***************************************************************************|   224     00:00

3. 접속 테스트

  • 비밀번호 입력 없이 서버에 접속이 가능한지 확인합니다.
[root@localhost .ssh]# ssh 192.168.100.32
Last login: Tue Jun 24 14:40:23 2008 from 192.168.100.118
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:50:EA:D4:5C:F3
          inet addr:192.168.100.32  Bcast:192.168.101.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5668 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2536 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:549777 (536.8 KiB)  TX bytes:297273 (290.3 KiB)
          Interrupt:16 Base address:0xcc00

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
[root@localhost ~]#

4. ssh 설정 파일 확인

  • 서버의 SSH 설정 파일 sshd_config(경로: /etc/ssh/sshd_config)에 다음 설정값이 주석 처리되어 있으면 ssh-key 기능이 적용되지 않습니다.
  • 주석 처리가 되어 있다면 # 문자를 제거하고 적용해야 합니다.
PubkeyAuthentication yes
AuthorizedKeysFile    .ssh/authorized_keys
위 설정이 적용되지 않으면, ssh-key를 통한 비밀번호 없는 접속이 불가능합니다.