【问题标题】:ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remainssh:握手失败:ssh:无法验证,尝试的方法[无公钥],没有支持的方法
【发布时间】:2021-02-22 14:56:30
【问题描述】:

我正在尝试使用 Terraform 配置程序 remote-exec 在 ec2 实例上安装 Nginx,但我一直遇到此错误。

ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

这就是我的代码的样子

resource "aws_instance" "nginx" {
  ami                    = data.aws_ami.aws-linux.id
  instance_type          = "t2.micro"
  key_name               = var.key_name
  vpc_security_group_ids = [aws_security_group.allow_ssh.id]

  connection {
    type        = "ssh"
    host        = self.public_ip
    user        = "ec2-user"
    private_key = file(var.private_key_path)
    

  }

  provisioner "remote-exec" {
    inline = [
      "sudo yum install nginx -y",
      "sudo service nginx start"
    ]
  }
}

安全组规则设置为允许从任何地方进行 ssh。 而且我可以从我的本地机器 ssh 进入盒子。

不确定我是否在这里遗漏了非常明显的东西。我尝试过更新版本的 Terraform,但还是同样的问题。

【问题讨论】:

  • 您为var.key_namevar.private_key_path 传递了什么值?
  • 我正在传递同一个 .pem 密钥对文件。

标签: terraform terraform-provider-aws terraform0.12+


【解决方案1】:

如果您的 EC2 实例将 AMI 用于使用 cloud-init(大多数 Linux 发行版的默认映像)的操作系统,那么您可以使用 @987654326 完全避免 Terraform 通过 SSH 登录@ 参数将脚本传递给 cloud-init:

resource "aws_instance" "nginx" {
  ami                    = data.aws_ami.aws-linux.id
  instance_type          = "t2.micro"
  key_name               = var.key_name
  vpc_security_group_ids = [aws_security_group.allow_ssh.id]

  user_data = <<-EOT
    yum install nginx -y
    service nginx start
  EOT
}

对于包含 cloud-init 的操作系统,系统将在系统启动过程中运行 cloud-init,并访问 the metadata and user data API 以检索 user_data 的值。然后它将执行脚本的内容,将来自该操作的任何消息写入the cloud-init logs

我上面描述的是the official recommendation for how to run commands to set up your compute instance。文档说provisioners are a last resort,给出的原因之一是避免必须正确配置 SSH 连接和身份验证的额外复杂性,这正是导致您提出这个问题的复杂性,所以我想尝试遵循文档中的建议是解决它的最佳方法。

【讨论】:

  • 非常感谢马丁。我去看看文档。
猜你喜欢
  • 1970-01-01
  • 2022-11-10
  • 2022-06-21
  • 2017-04-28
  • 2014-01-06
  • 1970-01-01
  • 2021-02-25
  • 2021-04-21
  • 2022-01-11
相关资源
最近更新 更多