Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SSH连接GCP_Ssh_Google Cloud Platform_Ansible_Terraform - Fatal编程技术网

SSH连接GCP

SSH连接GCP,ssh,google-cloud-platform,ansible,terraform,Ssh,Google Cloud Platform,Ansible,Terraform,我看到了僵局。我尝试通过SSH连接到创建的VM,但什么都没有出来。将以下条目添加到地形中 provisioner "remote-exec" { inline = [ "/bin/echo -e \"${element(random_string.password.*.result, count.index)}\n${element(random_string.password.*.result, count.index)}\" | /usr/bin/passwd root" ]

我看到了僵局。我尝试通过SSH连接到创建的VM,但什么都没有出来。将以下条目添加到地形中

provisioner "remote-exec" {
  inline = [
    "/bin/echo -e \"${element(random_string.password.*.result, count.index)}\n${element(random_string.password.*.result, count.index)}\" | /usr/bin/passwd root"
 ]
connection {
  type            = "ssh"
  user            = "root"
  private_key     = file(var.privat_google_key)
  agent           = false
  timeout         = "5m"
  host            = google_compute_instance.webserver[count.index].network_interface[0].access_config[0].nat_ip
 }
}

...

resource "google_compute_project_metadata_item" "ssh-keys" {
  key   = "ssh-keys"
  value = file(var.pub_google_key)
}
ssh密钥被添加到实例中

当我完成时,我得到

google_compute_instance.webserver[0] (remote-exec): Connecting to remote host via SSH...
google_compute_instance.webserver[0] (remote-exec):   Host: 1.1.1.1
google_compute_instance.webserver[0] (remote-exec):   User: root
google_compute_instance.webserver[0] (remote-exec):   Password: false
google_compute_instance.webserver[0] (remote-exec):   Private key: true
google_compute_instance.webserver[0] (remote-exec):   Certificate: false
google_compute_instance.webserver[0] (remote-exec):   SSH Agent: false
google_compute_instance.webserver[0] (remote-exec):   Checking Host Key: false
google_compute_instance.webserver[0]: Still creating... [5m0s elapsed]


Error: timeout - last error: SSH authentication failed (root@35.247.121.86:22): ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
当我尝试从终端通过ssh连接时,我得到

ssh -i [PATH_TO_PRIVATE_KEY] [USERNAME]@[EXTERNAL_IP_ADDRESS]

root@1.1.1.1: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

我尝试了不同的选项来添加密钥,但我总是被禁止访问主机。有什么问题吗?

根用户的SSH在GCP上默认被禁用。您将必须使用特定用户进行连接,您仍将具有根权限。如果您必须使用根帐户进行连接,而根帐户是不推荐的,我建议您使用预构建的映像或启动脚本,您可以按照说明启用它,但我不会在本回答中介绍

因此,要在Terraform上实现与特定用户的连接,您需要:

  • 在TF中更改连接用户配置
  • 更改元数据SSH密钥,使其包含用户名和公钥,格式如下所述

  • 谢谢你的帮助!我仍然得到了comand/Users/Husky/googlecloudsdk/bin/gcloud compute的帮助——project-stered-sentry-276613 ssh——zone“us-west1-c”standard webserver--verbosity=debug。使用debug,我看到了到远程主机的连接,这有助于理解我的问题。
    connection {
      user            = "alexey"
      ...
    }
    
    ssh-rsa [KEY_VALUE] [USERNAME]