Computer System/Linux

[Linux] RSA 비대칭키로 패스워드 없이 SSH 접속

해리팍 2017. 8. 10. 15:40
반응형

지난 포스팅 (http://harryp.tistory.com/624) 에서 sshpass를 통해 비밀번호를 미리 입력해둬서


ssh 접속 시 패스워드를 따로 입력 안할 수 있는 방법을 공유했었는데요.


RSA 비대칭키를 활용하여 접속하는 방법을 알게되어 공유합니다.


* 참고 - http://www.snoopybox.co.kr/1734



접속을 시도하는 곳 (클라이언트)에서 RSA 비대칭키를 생성 후,


Private Key는 클라이언트에,


Public Key는 클라이언트가 접속을 하려는 곳 (서버)에 위치를 시킵니다.


다음과 같이 하시면 됩니다.



1. 클라이언트에서 RSA 비대칭키 접속


# ssh-keygen -> 엔터 -> 엔터 -> 엔터



[root@cli ~]# ssh-keygen 

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:

cf:30:4a:8c:49:4e:93:4e:c5:37:c7:a6:d2:99:44:6a root@cli

The key's randomart image is:

+--[ RSA 2048]----+

|     .....       |

|     o..+ +      |

|    * E+ B       |

|   * *. =        |

|    = o.S        |

|     . . =       |

|      .   o      |

|                 |

|                 |

+-----------------+

[root@cli ~]# 

 


ssh-keygen 에 대한 결과로 


Private 키는 '~/.ssh/id_rsa'


Public 키는 '~/.ssh/id_rsa.pub'


으로 생성이 됩니다.


(참고 : '~/' 는 홈 디렉토리 /home/사용자ID 를 말합니다.)



Private 키는 그대로 클라이언트에 위치하면 되고,


Public 키는 서버의 '~/.ssh/authorized_keys' 로 옮기신 후,


'chmod 770 ~/.ssh/authorized_keys' 명령어로 권한 변경을 해주시면 됩니다.



2018.09.27 추가 내용


ssh-copy-id 를 사용하시면 키 복사를 조금 더 편하게 하실 수 있습니다.


ssh-keygen 으로 키 생성 후


# ssh-copy-id 계정@서버주소


명령어를 실행하시면 키가 자동으로 복사가 됩니다.


# ssh-copy-id parkch0708@10.152.126.118

/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

parkch0708@10.152.126.118's password:


Number of key(s) added: 1


Now try logging into the machine, with:   "ssh 'parkch0708@10.152.126.118'"

and check to make sure that only the key(s) you wanted were added. 



그러면 패스워드 없이 ssh 접속을 하실 수 있습니다.



반응형