홈 디렉토리 없이 SSH 키를 저장하는 방법은 무엇입니까?

홈 디렉토리 없이 SSH 키를 저장하는 방법은 무엇입니까?

remoteB다른 서버에서 원격 컴퓨터에 로그인하려고 하는데 매번 비밀번호를 입력하고 remoteA싶지 않습니다 . remoteB이를 위해 SSH 키를 생성하고 싶지만 문제는 remoteAremoteB.

.sshA의 (홈이 아닌) 디렉토리 내에 내가 액세스할 수 있는 디렉토리를 생성하려고 시도했지만 그렇게 하면

ssh-copy-id -i id_rsa.pub username@remoteB

그러면 오류가 반환됩니다.

Could not create directory '/home/name/.ssh'
The authenticity of host 'remoteB' can't be established

/home/에 디렉토리가 없기 때문에 이는 의미가 있습니다 remoteA. 하지만 홈 디렉토리 외부의 폴더를 SSH 키로 사용하는 방법이 있습니까?

답변1

이 메시지는 Could not create directory '/home/test3/.ssh'.오류가 아닌 경고입니다. 안전한 곳에 키를 저장할 수 있지만 ssh기본 위치는 홈 디렉터리입니다.

예를 들어 로컬 사용자에게는 test3홈 디렉토리가 없지만 사용자 test4@otherhost에게는 있습니다. 먼저 test3 사용자로 로컬로 로그인합니다.

"보안" 디렉터리 생성 및 인증서 쌍 생성

mkdir -m700 /tmp/ssh
ssh-keygen -t rsa -f /tmp/ssh/id_rsa
Generating public/private rsa key pair.
...
Your public key has been saved in /tmp/ssh/id_rsa.pub.
...

대상에 복사해 보세요

ssh-copy-id -i /tmp/ssh/id_rsa.pub test4@otherhost
/usr/bin/ssh-copy-id: 59: cd: can't cd to /home/test3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/tmp/ssh/id_rsa.pub"
The authenticity of host 'otherhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:pqNd/9gP69W2hzcosj+GI2DY2uw3+Upvvg22KV8sq5A.
Are you sure you want to continue connecting (yes/no)? yes
mktemp: failed to create file via template ‘/home/test3/.ssh/ssh-copy-id_id.XXXXXXXXXX’: No such file or directory
/usr/bin/ssh-copy-id: ERROR: mktemp failed

이때 참고하세요설치 실패이므로 이에 상응하는 수동 프로세스로 돌아가야 합니다. 또한 파일을 사용할 때마다 불평하지 않도록 known_hosts파일을 안전한 곳에 보관합니다 .ssh

ssh -o UserKnownHostsFile=/tmp/ssh/known_hosts test@otherhost 'mkdir -m700 -p .ssh && cat >>.ssh/authorized_keys' </tmp/ssh/id_rsa.pub
Could not create directory '/home/test3/.ssh'.
The authenticity of host 'otherhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:pqNd/9gP69W2hzcosj+GI2DY2uw3+Upvvg22KV8sq5A.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'otherhost' (ECDSA) to the list of known hosts.
Password:

여기에는 많은 주의 사항이 있지만 본질적으로 프로세스는 성공적이었고 키는 이제 원격 계정의 authorized_keys파일에 있습니다. 테스트해보자

ssh -o UserKnownHostsFile=/tmp/ssh/known_hosts -i /tmp/ssh/id_rsa test4@otherhost date
Could not create directory '/home/test3/.ssh'.
Thu 26 Nov 10:16:23 GMT 2020

원격 호스트로부터 날짜 문자열을 얻었으므로 그것이 모두 작동하고 있음을 증명할 수 있습니다.

참고: 파일과 비밀은 사용할 때마다 ssh명시적으로 정의되어야 합니다.known_hostsid_rsa

ssh -o UserKnownHostsFile=/tmp/ssh/known_hosts -i /tmp/ssh/id_rsa ...

관련 정보