【问题标题】:setting up ssh passwordless in ec2 via chef通过 chef 在 ec2 中设置 ssh 无密码
【发布时间】:2014-10-11 21:46:31
【问题描述】:

我在 Chef 中有以下 recipe/default.rb

# Create empty RSA password
template "#{node[:cluster][:ubuntu]}/my_key.pem" do
   source "keys.pem.erb"
   mode 0400
   owner "ubuntu"
   group "ubuntu"
end

bash "ssh-passwordless" do
   user "ubuntu"
   cwd "#{node[:cluster][:ubuntu]}"
   code <<-EOF
   eval `ssh-agent -s`
   ssh-add #{node[:cluster][:ubuntu]}/my_key.pem
   EOF
end

# Create empty RSA password
execute "ssh-keygen" do
  command "sudo -u ubuntu ssh-keygen -q -t rsa -N '' -f /home/ubuntu/.ssh/id_rsa"
  creates "/home/ubuntu/.ssh/id_rsa"
 action :run
end

# Copy public key to node1; if key doesn't exist in authorized_keys, append it to this file
execute <<EOF
cat /home/ubuntu/.ssh/id_rsa.pub | sudo -u ubuntu ssh ubuntu@localhost "(cat > /tmp/tmp.pubkey; mkdir -p .ssh; touch .ssh/authorized_keys; grep #{node[:fqdn]} .ssh/authorized_keys > /dev/null || cat /tmp/tmp.pubkey >> .ssh/authorized_keys; rm /tmp/tmp.pubkey)

如您所见,我尝试了很多方法来让它发挥作用,但是到目前为止,它们都没有成功。目标是消除 EC2 中对密码/pem 文件的需求,这样我就可以建立一个 hadoop 集群。我怎样才能做到这一点?

【问题讨论】:

    标签: hadoop amazon-web-services ssh amazon-ec2 chef-infra


    【解决方案1】:

    如果我理解得很好,您想在 node1 上创建一个私钥,以便能够通过 ssh 在 node2 上连接。

    您可以通过搜索轻松完成。

    在节点 1 上:

    # Create empty RSA password
    execute "ssh-keygen" do
      command "sudo -u ubuntu ssh-keygen -q -t rsa -N '' -f /home/ubuntu/.ssh/id_rsa"
      creates "/home/ubuntu/.ssh/id_rsa"
    end
    
    ruby_block "expose public key in attribute" do
      block do
        node.default['public_key'] = ::File.read("/home/ubuntu/.ssh/id_rsa.pub")
      end
    end
    

    在node2上,搜索node1的公钥:

    node1 = search(:node, "name:node1").first
    file '/home/ubuntu/.ssh/authorized_keys' do
      content node1['public_key']
    end
    

    当然,如果您需要允许多个主机连接,则需要对此进行调整。

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 2017-12-14
      • 2013-01-19
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 2012-05-01
      • 2012-09-10
      • 2011-10-31
      相关资源
      最近更新 更多