쉘 환경 변수에 저장된 키를 SSH로 전달하는 방법은 무엇입니까?

쉘 환경 변수에 저장된 키를 SSH로 전달하는 방법은 무엇입니까?

아마도 다중 ssh액세스를 시도했지만 첫 번째 읽기 후에 파괴될 것입니다.fdfd

# ssh -i <(echo $KEY) [email protected]
Warning: Identity file /dev/fd/11 not accessible: Bad file descriptor.
[email protected]: Permission denied (publickey).

임시파일을 쓰거나 삭제하지 않고 다른 방법이 있나요?

답변1

export MYKEY=`cat key.pem`
ssh-add - <<< "$MYKEY"
ssh [email protected]

답변2

너무 길어요.아니 근데 좀 봐주세요XY 문제.

$ strace -f ssh -i <(cat ~/.ssh/id_rsa) user@server echo test
...
close(63)
...
newfstatat(AT_FDCWD, "/dev/fd/63", 0x7ffc4f1d0c60, 0) = -1 ENOENT (No such file or directory)
write(2, "Warning: Identity file /dev/fd/6"..., 77Warning: Identity file /dev/fd/63 not accessible: No such file or directory.) = 77

그래서 어떤 이유로 sshfd가 처음에 닫혔습니다. 어쩌면 표준 설명자를 제외한 모든 설명자를 닫을 수도 있습니다.

그럼에도 불구하고 귀하의 가정은 잘못되었습니다.

아마도 ssh는 fd에 여러 번 액세스하려고 시도하지만 첫 번째 읽기 후에 fd가 파괴됩니다.

fd를 처리하기 전에 단 하나의 시스템 호출이 있습니다 stat: close(63). 응, 그 이후에 망했지.

답변3

이는 사용되는 솔루션 ssh-agent이며 실행하는 데 필요하지 않습니다.

ssh-agent bash -c "ssh-add <(echo '$MY_SSH_KEY') && ssh ..."

따라서 에이전트는 "데몬화"되지 않으며 SSH 세션이 활성화된 동안에만 존재합니다.

답변4

대안으로 키가 쓰여지면일시적인kill -9(a 없이) 치료가 보장되는 보안 파일을 허용할 수 있습니다. 이 Bash 도우미 함수를 사용하여 ssh, scp, 를 래핑 하고 sftp키를 첫 번째 인수로 전달할 수 있습니다.

with-ssh-key() (
    # Bash wrapper for OpenSSH CLI tools (ssh, scp, sftp, or any that accept
    # the `-i /path/to/keyfile` option) that allows for passing an SSH private
    # key as the first parameter rather than via a persistent identity file.
    # This is something that is not possible with ssh tools by default.
    #
    # This command is useful when storing the key in an environment variable
    # that is set in some secure way, such as set via secrets mastered in the
    # AWS Parameter Store, or as the result another command in a subshell.
    #
    # The "OPENSSH PRIVATE KEY" lines at the beginning and end of the passed
    # key string will be added if they are not on already present.
    #
    # Usage: with-ssh-key {ssh | scp | sftp} KEY ADDITIONAL_ARGS...
    #
    # Examples:
    #   key="b3blbnnzaC1rZXktdjEAAAAABG5vbmUAAAA[etc...]"
    #   with-ssh-key "$key" ssh myserver echo Hello-from-myserver
    #   with-ssh-key "$key" scp myserver:file.txt ./
    #   echo get file.txt | with-ssh-key "$key" sftp myserver
    #
    # A note regarding zsh: this wrapper is not necessary in zsh because zsh
    # has the extremely convenient `=(cmd)` syntax which will create a real
    # on-disk tempfile and clean it up immediately after the calling command
    # exits. Thus you can easily do something like this in zsh to accomplish
    # the same effect as this wrapper:
    #
    #   ssh -i =(echo "$key") ...
    #
    # Or, if you need to add the header/footer lines:
    # 
    #   ssh -i =(echo "-----BEGIN OPENSSH PRIVATE KEY-----\n$(echo "$key")\n-----END OPENSSH PRIVATE KEY-----")
    set -euo pipefail
    key="$1"; shift
    cmd="$1"; shift
    tempkeyfile=$(mktemp)
    chmod 600 "$tempkeyfile" # ensure file is locked down
    trap "rm \"$tempkeyfile\"" 0 2 3 15 # ensure tempfile cleanup in event of various signals
    [[ $key =~ "BEGIN OPENSSH PRIVATE KEY" ]] || echo "-----BEGIN OPENSSH PRIVATE KEY-----" >> $tempkeyfile
    echo "$key" >> $tempkeyfile
    [[ $key =~ "END OPENSSH PRIVATE KEY" ]] || echo "-----END OPENSSH PRIVATE KEY-----" >> $tempkeyfile
    $cmd -i "$tempkeyfile" "$@"
)

관련 정보