Add SSH key from variable

Viewed 24988

I have a bash script inside a Docker container that needs to access remote server via ssh. I have SSH key inside a variable. How can I add it to SSH?

3 Answers

If you are using Gitlab CI/CD and you want to use a variable as an SSH key you can do the following:

  • Add your variable in Settings -> CI/CD -> Variables
  • Use that variable in your .gitlab-ci.yml file:

    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null

Gitlab documentation

Also, you can use:
echo "${SSH_PRIVATE_KEY}" | ssh-add -
or
ssh-add <(echo "$SSH_PRIVATE_KEY")

Related