bash 从变量添加 SSH 密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46253162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:27:42  来源:igfitidea点击:

Add SSH key from variable

bashsshssh-keys

提问by Lyberta

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?

我在 Docker 容器中有一个 bash 脚本,需要通过 ssh 访问远程服务器。我在变量中有 SSH 密钥。如何将其添加到 SSH?

回答by Lyberta

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

回答by Dieter Casier

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

如果您使用 Gitlab CI/CD 并且想要使用变量作为 SSH 密钥,您可以执行以下操作:

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

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

  • 添加您的变量Settings-> CI/CD->Variables
  • 在您的.gitlab-ci.yml文件中使用该变量:

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

Gitlab documentation

Gitlab 文档

回答by typelogic

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

我没有奢侈做ssh-add因为我想通过ssh -i /path/to/key

My answer therefore here

我的回答因此在这里