원격 호스트 인증을 위한 SSH 키 생성 및 사용
SSH 키는 공개 키 암호 방식 및 인증 확인 응답 인증을 사용하는 SSH 서버에 대해 자체 식별하는 방법입니다. 기존 비밀번호 인증에 비해 이 방법을 사용하면 비밀번호를 네트워크를 통해 보내지 않고 서버에서 인증받을 수 있습니다. 또한 무인 서버 통신이 가능하여 자동화와 함께 이 방법을 사용할 수 있습니다.
Linux에서 SSH 키 생성
Linux SSH키를 생성하려면 ssh-keygen
명령을 실행하십시오. 생성되는 키의 유형 및 키 생성에 사용되는 서명 알고리즘을 사용자 정의하려는 경우 명령이 플래그를 사용할 수 있습니다. 이 예제는 비밀번호 문구 없이 표준 2048비트 RSA 키를 생성합니다. 이 명령은 키를 저장할 위치 (기본값 $HOME/.ssh/) 및 SSH키를 보안하기 위한 비밀번호 문구를 프롬프트합니다.
root@bck2:/etc# 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:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx root@bck2.example.com
The key's randomart image is:
+---[RSA 2048]----+
|. oo*%=+o.. |
|.++.oX+=. . |
|..+ooo=. . |
| E.+. .o. |
| + S..+ |
| . +. = |
| o = o |
| .o+ + |
| +o. |
+----[SHA256]-----+
원격 호스트에 공개 키 복사
ssh-copy-id
명령을 사용하기 위해 공용 SSH키를 사용하여 원격 호스트를 인증하려면 다음을 수행하십시오. 원격 호스트에 복사할 공개 키를 지정하려면 -i
플래그를 사용하십시오.
root@bck2: # ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.176.18.15
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.176.18.15 (10.176.18.15)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)? yes
/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@10.176.18.15's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@10.176.18.15'"
and check to make sure that only the key(s) you wanted were added.
ssh-copy-id 명령은 원격 호스트 .ssh/authorized_key
파일에 키를 추가합니다.
키가 올바르게 복사되었는지 테스트
공용 키가 원격 호스트에 올바르게 복사되었는지 테스트하려면 원격 호스트에 ssh를 실행한다.
root@bck2:/etc# ssh root@10.176.18.15
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-53-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
0 packages can be updated.
0 updates are security updates.
Last login: Fri Feb 10 16:51:51 2017 from 169.46.3.91
root@bck1:~#
원격 호스트에 로그인할 때 비밀번호를 입력하라는 메시지가 표시되지 않습니다.
비밀번호 문구가 있는 SSH 키
SSH키를 위한 비밀번호 문구를 제공하면 추가 보안 계층이 제공되지만, 보호된 키가 필요한 자동화된 스크립트를 실행할 때 문제가 발생할 수도 있다.
ssh-agent는 사용자의 키를 관리할 수 있다. 사용자는 비밀번호 문구를 한 번 입력합니다. ssh-agent가 해당 메모리에 이 키를 보관하고 필요한 경우 가져옵니다. ssh-agent 에서 키를 관리하려면 다음 명령을 실행한다.
eval $(ssh-agent)
프로그램이 ssh-add 명령을 시작하여 에이전트에 공개 키를 추가한 후 ssh-add 유틸리티는 기본 키 이름을 검색합니다. 여기서 id_rsa
는 하나이며 ssh-agent에 추가합니다. 비밀번호를 입력하면 "잠금 해제된" 키가 ssh-agent를 통해 저장되고 다른 서버에 대해 인증하는 데 사용될 수 있습니다.
root@bck1:~# ssh-add
Enter passphrase for /root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
root@bck1:~#
새 터미널 세션을 열 때마다 키 비밀번호 문구에 대한 프롬프트가 표시됩니다. ssh-agent가 모든 bash 세션에서 시작하여 사용자의 키를 추가하도록 다음 명령을 실행하여 .bash_profile
파일을 추가하십시오.
echo ‘eval $(ssh-agent)’ >> ~/.bash_profile
echo ‘ssh-add’ >> ~/.bash_profile