SSH key 생성하기

작성자 김아름 수정일 2022-12-02 13:21

#Linux, #리눅스, #ssh, #key

들어가며

  • ssh key란, 서버에 접속할 때 비밀번호 대신 key를 제출하는 방식입니다.

  • 비밀번호보다 높은 수준의 보안을 필요로 하거나, 로그인 없이 자동으로 서버에 접속할 때 사용합니다.

  • 이번 내용에서는 ssh 접속할 때 비밀번호 입력 없이 접속할 수 있는 ssh key 생성 방법을 알아보겠습니다.




SSH key 생성하기

Unix 계열에서는 ssh-keygen 프로그램을 사용하여 ssh-key를 생성할 수 있습니다.


ssh를 시도하는 서버를 client 서버, ssh로 접속할 대상이 되는 서버를 target 서버라고 하겠습니다.


먼저, client 서버에서 사용자의 홈 디렉터리 하위에 있는 .ssh 디렉터리의 내용을 확인합니다.

ls -al ~/.ssh
total 8
drwx------.  2 root root   25 Oct 18 04:03 .
dr-xr-x---. 11 root root 4096 Nov 25 03:06 ..
-rw-r--r--.  1 root root 1753 Nov 25 03:04 known_hosts

현재 사용자의 .ssh 디렉터리 하위에는 known_hosts 파일만 존재합니다.

ssh 로 다른 호스트로 접근하면 해당 파일에 내용이 작성되게 됩니다.


그렇다면, key를 통해 ssh 접속을 할 수 있도록 ssh-key를 생성해 보겠습니다.

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):

ssh-keygen 명령어를 입력하게 되면, 가장 먼저 key를 저장할 위치를 물어봅니다.

default 값인 $HOME/.ssh/로 지정하려면 Enter 를 누르면 됩니다.


Enter passphrase (empty for no passphrase): 

다음으로 물어보는 것은 암호입니다.

일종의 비밀번호로, 입력한 비공개키를 암호화합니다.

만약, 비밀번호 없이 로그인을 원한다면 아무것도 입력하지 않고 Enter 를 누르면 됩니다.


Enter same passphrase again:

암호를 한번 더 물어봅니다.

위에서 입력한 동일한 암호를 입력하거나, 아무것도 입력하지 않은 상태에서 Enter 를 누릅니다.


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:
SHA256:/6gdP/0VtbJBA7NEzuqdJJQgS7GWUTptHVMRwdD2RBU root@master
The key's randomart image is:
+---[RSA 2048]----+
|     =oo +*X=..E.|
|    . O o B++.   |
|     B o +.+oo  .|
|    . o . . ... o|
|        So . o o |
|        ..+ . + .|
|         .oo o  .|
|         . =. . .|
|        ..o o. ..|
+----[SHA256]-----+

ls -al ~/.ssh/
total 16
drwx------.  2 root root   57 Nov 29 20:32 .
dr-xr-x---. 11 root root 4096 Nov 29 20:22 ..
-rw-------   1 root root 1675 Nov 29 20:32 id_rsa
-rw-r--r--   1 root root  393 Nov 29 20:32 id_rsa.pub
-rw-r--r--.  1 root root 1753 Nov 25 03:04 known_hosts

ssh-key가 생성되었습니다.

id_rsa
private key
id_rsa.pub
public key


client 서버에서 생성된 public 키를, target 서버의 authorized_keys 에 추가합니다.

ssh-copy-id -i ~/.ssh/id_rsa.pub <target IP>
ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.50.201
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.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
root@192.168.50.201's password: 

Number of key(s) added: 1

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


이제 target 서버에 ssh를 시도해보면, 비밀번호 입력 없이 바로 접속되는 것을 확인할 수 있습니다.

ssh <target IP>



마무리

  • ssh 접속할 때 비밀번호 대신 key를 사용하기 위해, ssh-key 생성하는 방법을 알아보았습니다.

아티클이 유용했나요?

훌륭합니다!

피드백을 제공해 주셔서 감사합니다.

도움이 되지 못해 죄송합니다!

피드백을 제공해 주셔서 감사합니다.

아티클을 개선할 수 있는 방법을 알려주세요!

최소 하나의 이유를 선택하세요
CAPTCHA 확인이 필요합니다.

피드백 전송

소중한 의견을 수렴하여 아티클을 개선하도록 노력하겠습니다.

02-558-8300