AWS EMR bootstrap 作为 sudo 的操作

AWS EMR bootstrap action as sudo

我需要为我的 EMR 集群 (EMR AMI 4.3) 中的所有实例更新 /etc/hosts

整个脚本无非就是:

#!/bin/bash
echo -e 'ip1 uri1' >> /etc/hosts
echo -e 'ip2 uri2' >> /etc/hosts
...

此脚本需要 运行 为 sudo 否则会失败。

从这里开始:https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-plan-bootstrap.html#bootstrapUses

Bootstrap actions execute as the Hadoop user by default. You can execute a bootstrap action with root privileges by using sudo.

好消息...但我不知道该怎么做,也找不到示例。

我已经尝试了很多东西...包括...

我一直得到:

On the master instance (i-xxx), bootstrap action 2 returned a non-zero return code

如果我检查 "Setup hadoop debugging" 步骤的日志,那里什么也没有。

从这里开始:https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-overview.html#emr-overview-cluster-lifecycle

摘要 emr 设置(按顺序):

  1. 配置 ec2 实例
  2. 运行s bootstrap 动作
  3. 安装本机应用程序...例如 hadoop、spark 等

所以似乎存在一些风险,因为我在安装 hadoop 之前以用户 Hadoop 的身份四处乱逛,我可能在那里搞砸了一些东西,但我无法想象是什么。

我想一定是我的脚本没有 运行ning 为 'sudo' 并且无法更新 /etc/hosts.

我的问题...我如何在 EMR 上使用 bootstrap 操作(或其他东西)到 运行 一个简单的 shell 脚本作为 sudo? ...专门更新 /etc/hosts?

我在 shell 脚本 运行 中使用 sudo 作为 EMR bootstrap 操作没有遇到问题,所以它应该可以工作。您可以测试它是否可以使用一个简单的脚本来执行 "sudo ls /root".

您的脚本试图通过重定向标准输出附加到 /etc/hosts:

sudo echo -e 'ip1 uri1' >> /etc/hosts

这里的问题是,虽然使用 sudo 回显 运行,但重定向 (>>) 不是。它是底层 hadoop 用户的 运行,他无权写入 /etc/hosts。修复是:

sudo sh -c 'echo -e "ip1 uri1" >> /etc/hosts'

这 运行 是整个命令,包括 stdout 重定向,在 shell 和 sudo 中。