大网数据广告活动
服务器运维问答

Git access fails in EC2 instance startup script (Ubuntu)

· 52CE 我爱测

I am creating an ec2 instance from an AMI using aws ec2 run-instances [lots of arguments] --user-data file://my_sh_file.sh my_sh_file.sh looks like this: su ubuntu # fails with or without this cd /home/ubuntu/my_working_dir git p…

问题原文

原帖链接 · Omroth · CC BY-SA 4.0

I am creating an ec2 instance from an AMI using

aws ec2 run-instances [lots of arguments] --user-data file://my_sh_file.sh

my_sh_file.sh looks like this:

su ubuntu   # fails with or without this
cd /home/ubuntu/my_working_dir
git pull origin master
. app_startup_script.sh

Git is failing to connect with this error log (from /var/log/cloud-init-output.log):

Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

If I log in myself (as ubuntu) and call git pull it works fine.

I guess that either (a) su ubuntu isn't working, or (b) (this seems less likely) that the ssh config isn't loaded at this point in the boot sequence... but I'm stumped. Help?

已采纳解答

原帖链接 · MLu · CC BY-SA 4.0

The way you use sudo Isn’t correct.

First of all it should be sudo -u ubuntu not just sudo ubuntu. However even then it wouldn't work.

When you run sudo it spawns a new shell or if a command is given it runs that command under the ubuntu user. In your case sudo -u ubuntu doesn’t have a command to run and because it is in non-interactive mode it won’t spawn a shell either. It simply exits.

The following lines - cd and git - then run in the context of the UserData script, not under ubuntu user, that's why it fails.

You can do this for example:

sudo -u ubuntu sh -c "cd /home/...; git pull ...; ./app.sh"

That will run all the commands under the ubuntu user.

Hope that helps :)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-06-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。