Computer >> 컴퓨터 >  >> 프로그래밍 >> Bash 프로그래밍

비밀번호 없는 SSH 로그인을 활성화하는 방법:ssh-keygen 및 ssh-copy-id를 사용한 간단한 3단계

비밀번호 없는 SSH 로그인을 활성화하는 방법:ssh-keygen 및 ssh-copy-id를 사용한 간단한 3단계 이 문서에 설명된 대로 ssky-keygen 및 ssh-copy-id를 사용하여 간단한 3단계를 거쳐 비밀번호를 입력하지 않고 원격 Linux 서버에 로그인할 수 있습니다.

SSH-keygen 공개 키와 개인 키를 생성합니다. ssh-copy-id 로컬 호스트의 공개 키를 원격 호스트의 Authorized_keys 파일에 복사합니다. ssh-copy-id는 또한 원격 호스트의 홈, ~/.ssh 및 ~/.ssh/authorized_keys에 적절한 권한을 할당합니다.

이 기사에서는 ssh-copy-id 사용 시 3가지 사소한 문제와 ssh-agent와 함께 ssh-copy-id를 사용하는 방법도 설명합니다.

1단계:로컬 호스트에서 ssh-key-gen을 사용하여 공개 및 개인 키 생성

jsmith@local-host$ [Note: You are on local-host here]
jsmith@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host

2단계:ssh-copy-id를 사용하여 공개 키를 원격 호스트에 복사

jsmith@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
jsmith@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

참고: ssh-copy-id 추가 원격 호스트의 .ssh/authorized_key에 대한 키입니다.

3단계:비밀번호를 입력하지 않고 원격 호스트에 로그인

jsmith@local-host$ ssh remote-host
Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2
[Note: SSH did not ask for password.]
jsmith@remote-host$ [Note: You are on remote-host here]


대부분의 경우 위의 3가지 간단한 단계를 통해 작업을 완료할 수 있습니다.

또한 비밀번호를 입력하지 않고 openSSH에서 openSSH로 SSH 및 SCP를 수행하는 방법에 대해서도 앞서 자세히 논의했습니다.

SSH2를 사용하는 경우 SSH2에서 SSH2로, OpenSSH에서 SSH2로, SSH2에서 OpenSSH로 비밀번호 없이 SSH 및 SCP를 수행하는 방법에 대해 앞서 논의했습니다.

ssh-add/ssh-agent와 함께 ssh-copy-id 사용

옵션 -i에 값이 전달되지 않은 경우 ~/.ssh/identity.pub인 경우 사용할 수 없습니다. ssh-copy-id 다음과 같은 오류 메시지가 표시됩니다.

jsmith@local-host$ ssh-copy-id -i remote-host
/usr/bin/ssh-copy-id: ERROR: No identities found


ssh-agent에 키를 로드한 경우 ssh-add 사용 , ssh-copy-id ssh-agent에서 키를 가져옵니다. 원격 호스트에 복사합니다. 즉, ssh-add -L에서 제공한 키를 복사합니다. 옵션 -i를 전달하지 않은 경우 원격 호스트에 대한 명령 ssh-copy-id에 .

jsmith@local-host$ ssh-agent $SHELL

jsmith@local-host$ ssh-add -L
The agent has no identities.
jsmith@local-host$ ssh-add
Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa)
jsmith@local-host$ ssh-add -L
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79DyBV
aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa
jsmith@local-host$ ssh-copy-id -i remote-host
jsmith@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[Note: This has added the key displayed by ssh-add -L]

ssh-copy-id의 세 가지 사소한 문제

다음은 ssh-copy-id의 몇 가지 사소한 문제입니다.

  1. 기본 공개 키: ssh-copy-id는 ~/.ssh/identity.pub를 기본 공개 키 파일로 사용합니다(즉, 옵션 -i에 값이 전달되지 않은 경우). ). 대신 id_dsa.pub, id_rsa.pub 또는 Identity.pub를 기본 키로 사용하고 싶습니다. 즉, 그 중 하나라도 존재하면 이를 원격 호스트에 복사해야 합니다. 그 중 2~3개가 존재하는 경우에는 ID.pub를 기본값으로 복사해야 합니다.
  2. 상담원의 신원이 없습니다: ssh-agent 실행 중이고 ssh-add -L "에이전트에 ID가 없습니다"(즉, ssh-agent에 키가 추가되지 않음)를 반환하더라도 ssh-copy-id는 여전히 "에이전트에 ID가 없습니다"라는 메시지를 원격 호스트의 Authorized_keys 항목에 복사합니다.
  3. authorized_keys에 중복된 항목이 있습니다: ssh-copy-id가 원격 호스트의 Authorized_keys에 대한 중복 항목의 유효성을 검사하기를 바랍니다. 로컬 호스트에서 ssh-copy-id를 여러 번 실행하면 중복을 확인하지 않고 원격 호스트의 authenticate_keys 파일에 동일한 키를 계속 추가합니다. 항목이 중복되어도 모든 것이 예상대로 작동합니다. 하지만 Authorized_keys 파일을 깔끔하게 정리하고 싶습니다.


이 기사가 마음에 드셨다면 Delicious에 북마크해 주세요. 그리고 실패 .