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
Add SSH key from variable
提问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.yml
file:- 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