4

Instead of ssh -i mykey.pem I would like to pass the content of mykey.pem to the ssh command.

Is that possible somehow?

Background: the mykey.pem is stored encryptedly. I would like to decrypt it on the fly (in a CI tool) and pass it to ssh. Decrypting it and passing the path to the decrypted key would make it accessible to someone with disc access.

3
  • 1
    You do know OpenSSH itself supports password-encrypted key files? Historically it used OpenSSL's 'legacy' format which has a very poor PBKDF, but all versions also support PKCS8 (via OpenSSL PEM API) and versions 6.5 up (since 2014) support OpenSSH's own 'new format'. Oct 13, 2017 at 5:53
  • Thanks. We are using ansible-vault at the moment. I am pretty happy with it.
    – kev
    Oct 16, 2017 at 1:26
  • echo "$KEY" | ssh -i /dev/stdin username@host command — Source: stackoverflow.com/questions/12041688/…
    – KiriSakow
    Apr 21, 2023 at 14:39

1 Answer 1

3

You can use process substitution (pseudo file) in bash like :

ssh -i <( some command to generate file contents ) host

examples:

ssh -i <( cat /tmp/key.id ) host
ssh -i <( echo key contents ) host

You can read more about it here https://en.wikipedia.org/wiki/Process_substitution

EDIT :

After reading your question second time I'm not sure if this is what you wanted

3
  • That's exactly what I was looking for. Unfortunately it doesn't work for some reason. I am getting Warning: Identity file /dev/fd/63 not accessible: No such file or directory.. This is the command I am executing: ssh -i <( cat /path>/key.pem ) <host>. Executing just cat /<path>/key.pem works and this example: diff <(sort file1) <(sort file2) works too. bash version is 4.3.11.
    – kev
    Oct 16, 2017 at 1:22
  • @kev Did you ever find a solution? Process substitution isn't working for me either to pass the identity to ssh. Mar 23, 2018 at 17:04
  • @KingChungHuang unfortunately not, haven't pursued it any further. I am temporarily writing the file to disc and deleting it afterwards. That workaround seemed good enough.
    – kev
    Mar 24, 2018 at 0:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .