简体   繁体   English

ssh-将命令参数添加到 su

[英]ssh-add on command argument to su

I want to start a docker container that adds a ssh key at startup:我想启动一个 docker 容器,在启动时添加一个 ssh 密钥:

My entrypoint looks like this:我的入口点如下所示:

#!/bin/bash
set -e

service ssh start

su anotherUser -s /bin/bash -c "eval \"$(ssh-agent)\" && ssh-add /Keys/id_rsa"

I've seen many posts that use sudo , but I do not have sudo available.我看过很多使用sudo的帖子,但我没有sudo可用。 I've found this solution but at the startup it shows me:我找到了这个解决方案,但在启动时它向我显示:

[....] Starting OpenBSD Secure Shell server: sshd 7[ ok 8. [....] 启动 OpenBSD 安全 Shell 服务器:sshd 7 [好的 8。
Agent pid 36代理 pid 36
Error connecting to agent: Permission denied连接到代理时出错:权限被拒绝

But when I execute the same lines at the promp everythings is ok:但是当我在提示符处执行相同的行时,一切正常:

xxx# su anotherUser xxx# su anotherUser
anotherUser@xxx:~$ eval $(ssh-agent) anotherUser@xxx:~$ eval $(ssh-agent)
Agent pid 47代理 pid 47
anotherUser@xxx:~$ ssh-add /keys/id_rsa anotherUser@xxx:~$ ssh-add /keys/id_rsa
Identity added: /keys/id_rsa (yyy@yyy-HP-EliteBook-850-G4)添加的身份:/keys/id_rsa (yyy@yyy-HP-EliteBook-850-G4)

You are running ssh-agent before su runs.su运行之前,您正在运行ssh-agent The $ needs to be escaped so that the literal command substitution is passed to bash for execution. $需要转义,以便将文字命令替换传递给bash以执行。

su anotherUser -s /bin/bash -c 'eval $(ssh-agent) && ssh-add /Keys/id_rsa'

(Untested; probably needs more details about how the container is run and why ssh-add needs to be run as a different user.) (未经测试;可能需要有关容器如何运行以及为什么ssh-add需要以不同用户身份运行的更多详细信息。)

It may be simpler, though, to run your entry point with ssh-agent .不过,使用ssh-agent运行入口点可能更简单。 For example,例如,

# In the Dockerfile...
ENTRYPOINT ["ssh-agent", "entry.sh"]

Inside entry.sh , your environment will already have access to the agent.entry.sh中,您的环境已经可以访问代理了。

#!/bin/bash
set -e

service ssh start

su anotherUser -s ssh-add /Keys/id_rsa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM