无法 git 在用户数据部分克隆为 ec2-user

Can't git clone as ec2-user in userdata section

我在我的 cloudformation 模板中设置了一个自动缩放组,用于启动单个 ec2 实例。在 launchconfiguration 的用户数据部分,我执行以下语句:

sudo -u ec2-user git clone https://github.com/...git

到运行这是作为ec2用户,而不是root。在 cloud-init-output.log 中,出现以下错误: "fatal: unable to access 'https://github.com/...git/': Could not resolve host: github.com; Name or service not known"

关于我如何 运行 作为 ec2-user 的任何建议?

对我来说似乎是网络问题。您需要确保您的 ec2 能够连接到互联网。直接从 CLI 尝试 运行 git clone 命令。

您看到的错误消息是:

Could not resolve host: github.com; Name or service not known

那是网络连接问题。参见例如this 相关的 Stack Overflow 答案。

你还提到:

I have to run this as an ec2-user, not as root.

你还没有说为什么,但是,一般来说,没有理由不使用 root 用户从 Github.

克隆代码

如果您必须 运行 作为 ec2 用户执行命令,请尝试:

cd /home/ec2-user
su ec2-user -c "git clone https://github.com/...git"

如果只是需要root用户使用属于其他用户的私钥:

GIT_SSH_COMMAND="ssh -i /home/ec2-user/.ssh/id_rsa" git clone git@github.com:....git

否则,只需以 root 用户身份通过​​ HTTPS 克隆代码即可(如果您解决了网络问题)。