QA 엔지니어 성장기
[ssh-copy-id] ssh 암호 없이 접속하기. 본문
1. 개요
- 로컬서버의 공개키를 원격서버에 배포하는 명령어
- 로컬서버의 ~/.ssh/id_rsa.pub 파일 내용을 원격서버의 ~/.ssh/authorized_keys 파일에 추가하는 명령어
- 원격 서버에 authorized_keys 파일이 없다면 생성되고, 있다면 파일 내용이 추가된다.
- ssh-keygen을 사용해 key 생성 (이미 key가 있다면 하지 않아도 된다.)
jigo@local-host$ [로컬호스트]
jigo@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jigo/.ssh/id_rsa) : [엔터키]
Enter passphrase (empty for no passphrase): [엔터키]
Enter same passphrase again : [엔터키]
Your identification has been saved in /home/jigo/.ssh/id_rsa.
Your public key has been saved in /home/jigo/.ssh/id_rsa.pub.
The key fingerprint is:
32:b1:fa:af:95:95:38:11:31:d5:de:96:2f:f2:35: jigo@local-host
- ssh-copy-id 사용해 public key(공개키) 원격호스트에 복사
jigo@local-host$ ssh-copy-id ~/.ssh/id_rsa.pub 원격호스트계정@ip
원격서버계정@123.45.67.891's password: [원격서버 password 입력]
Now try logging into the machine, with "ssh '원격서버계정@123.45.67.891'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
→ 로컬서버의 공개키[1]를 원격서버의 testuser 홈 폴더 아래 특정위치 에 배포했음
- 암호 없이 원격호스트에 ssh 접속
jigo@local-host$ ssh 원격호스트계정@ip
암호 없이 접속 완료
[원격호스트계정@remote-host] $
- 만약 ssh-copy-id 가 권한 문제나 기타 문제로 실행이 잘 안될 경우
원격호스트에 접속 후 ~/.ssh/authorized_keys 파일을 열어 로컬호스트의 공개키를 추가한다.
jigo@local-host$ cat ~/.ssh/id_rsa.pub
ssh-rsa 키 내용 복사
jigo@local-host$ ssh 원격호스트계정@ip
[비밀번호 입력]
[원격호스트계정@remote-host] $ vi ~/.ssh/authorized_keys
ssh-rsa 키 내용 붙여넣기
참조
https://itzone.tistory.com/694
https://zetawiki.com/wiki/%EB%A6%AC%EB%88%85%EC%8A%A4_ssh-copy-id