Add SSH key from variable

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?

ssh-add - <<< "${SSH_PRIVATE_KEY}"

Add SSH key from variable, 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. To redirect the contents of the environment variable into ssh-addinstead of a real file simply use: eval $(ssh-agent -s) ssh-add <(echo "$PRIVATE_KEY") # List out your new key's fingerprintssh-add -l# Don't forget to cleanup your agent after you're done using it if you're not on an ephemeral build server.ssh-agent -k.

[Support Guide] Using an SSH key via environment variable during , First, create an ssh key and add the public key (shorter one) to your service (e.g. on your GitHub repo settings, or in ~/.ssh/authorized_keys on your server). Then � ssh-add It automatically registers the $HOME\.ssh\id_rsa key pair, and now you can authenticate with that key pair. Note: Sometimes the system may have several ssh.exe binary files installed, and

I did not have the luxury to do ssh-add because i wanted to do it by ssh -i /path/to/key

My answer therefore here

Add a private key to your ssh agent from a variable., I always have a hard time remembering how to add an ssh key ( ssh-add ) from an environment variable containing the private key. The steps below will walk you through generating an SSH key and adding the public key to the server. Step 1: Check for SSH Keys. First, check for existing SSH keys on your computer. Open Git Bash, Cygwin, or Terminal, etc. and enter: $ ls -al ~/.ssh # Lists all the files in your .ssh directory, if they exist Generating public/private rsa key pair. Check the directory listing to see if you already have a public SSH key.

How to pass to ssh the key stored in a shell environment variable , export MYKEY=`cat key.pem` ssh-add - <<< "$MYKEY" ssh ec2-user@100. 123.111.63. Use the ssh-keygen command to generate SSH public and private key files. By default, these files are created in the ~/.ssh directory. You can specify a different location, and an optional password (passphrase) to access the private key file. If an SSH key pair with the same name exists in the given location, those files are overwritten.

Trying to use SSH key as environment viariable, I'd like to use Pipelines environment variables to handle a SSH key. I literally Add a '-v' flag to your ssh call and require the id_rsa file as well:. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.

Generating SSH Keys :: Wercker Documentation, Wercker will generate the key for you in the background and create two environment variables. SSH key� Adding an Arbitrary Key. To add an arbitrary private key, give the path of the key file as an argument to ssh-add. For example, ssh-add ~/.ssh/tatu-aws-key. would add the file ~/.ssh/tatu-aws-key. Keys with Passphrases. If the key being added has a passphrase, ssh-add will run the ssh-askpass program to obtain

Comments
  • How did the key get in the variable in the first place? You might want to skip that step and have ssh-add read directly from the original file.
  • In my particular case SSH key is a GitLab secret variable.
  • Or since it is bash ssh-add - <<<"${SSH_PRIVATE_KEY}" and use a herestring.
  • Is it faster? And why <<<? What about ssh-add - < "${SSH_PRIVATE_KEY}"?
  • Well, the variable "${SSH_PRIVATE_KEY}" holds the value. You need it as input on stdin for ssh-add. As a convenience feature bash has both a heredoc and herestring. You can use the herestring` <<< to make the variable data in "${SSH_PRIVATE_KEY}" appear as input on stdin for ssh-add with ssh-add - <<<"${SSH_PRIVATE_KEY}". It is faster in that no additional *subshells are generated. You have 3 generated with echo key | ssh-add - (1-for echo, 1-for the pipe | and 1-for ssh-add). (note: you may or may not need the '-' after ssh-add, but I suspect you do.)
  • @DavidC.Rankin Yes you need the '-' or ssh-add will look for the key in $HOME/.ssh