首页 文章

无法使用GCloud Compute访问vm

提问于
浏览
2

我无法通过SSH访问它 . 从我的控制台输入有效命令:gcloud compute --project“ssh --zone”“”“

但我一直有以下错误消息:

权限被拒绝(publickey) . 错误:(gcloud.compute.ssh)[/ usr / bin / ssh]退出并返回代码[255] .

  • 然后我撤销了SSH:gcloud auth revoke --all

  • 然后我做了:gcloud auth登录

  • 然后尝试再次访问vm:gcloud compute --project " ssh --zone "“”“

我收到以下错误:错误:(gcloud.compute.ssh)无法获取资源: - '项目所需的'compute.instances.get'权限

请帮忙 . 谢谢

1 回答

  • 3

    权限被拒绝(publickey) . 错误:(gcloud.compute.ssh)[/ usr / bin / ssh]退出并返回代码[255] .

    此错误似乎是指SSH密钥或不完整的Linux Guest环境的某些错误 .

    我想知道运行 gcloud auth revoke --all 的目的是什么

    你能提供以下命令的输出吗?

    • $ gcloud compute instances describe name-of-your-instance --zone zone-the-instance-is-in --project name-of-your-project

    • $ gcloud compute instances get-serial-port-output name-of-your-instance --zone zone-the-instance-is-in --project name-of-your-project

    • $ gcloud compute firewall-rules list --project name-of-your-project

    这些命令很有用,因为:

    • 使用此命令,我们可以检查实例上的 state of the ssh keys 以及实例中启用的范围(以及其他信息)

    • 此命令提供 serial output log entries 来自 can help troubleshoot the connection issues 您希望持久化日志的实例,但此信息对您的案例非常有用 .

    • 此命令在项目中输出 firewall rules ;应该有一个默认或策划的防火墙规则允许端口22上的TCP入口流量,如果没有(你需要创建一个) .

    您是否在隐身模式下尝试了to SSH from the browser?有时,浏览器扩展可能会阻止SSH通过浏览器功能正常运行,这就是我推荐隐身模式的原因 .


    EDIT 为了使社区 post useful (并且更容易阅读)我在这里总结了下面的一些评论:

    Error shown:

    无法获取资源: - 'projects // zones // instances /的必需'compute.instances.get'权限

    What to do: 检查用户角色/权限

    $ gcloud beta iam roles list --account your-account-here
    
    > --- description: Full management of App Engine apps (but not storage).
    > etag: AA== name: roles/appengine.appAdmin stage: GA title: App Engine Admin
    > --- description: Ability to view App Engine app status. etag: AA== name: roles/appengine.appViewer stage: GA title: App Engine Viewer
    

    From the output above: 用户只有App Engine权限(但不是Compute Engine中的权限)

    What to do: 要求项目所有者添加一个角色,授予用户访问GCE实例的权限(实例管理员角色,计算管理员角色)此处可用角色列表:cloud.google.com/compute/docs/access/iam#instance_admin_role

    必填信息,运行2个命令:

    • 以便从实例中检查 log

    $ gcloud compute instances get-serial-port-output name-of-your -zone zone-the-instance-is-in -project name-of-project

    SeaBIOS(版本1.8.2-20180102_145157-google)总RAM大小= 0x000000006cc00000 = 1740 MiB CPUs:1支持最大CPU:256发现virtio-scsi为0:3 virtio-scsi vendor ='Google'product ='PersistentDisk' rev ='1'type = 0 removable = 0 virtio-scsi blksize = 512 sector = 20971520 = 10240 MiB drive 0x000f2330:PCHS = 0/0/0 translation = lba LCHS = 1024/255/63 s = 20971520从硬盘启动0 ... [0.000000]初始化cgroup subsys cpuset [0.000000]初始化cgroup subsys cpu [0.000000]初始化cgroup subsys cpuacct [0.000000] Linux版本3.16.0-0.bpo.4-amd

    • 确认 firewall rule 允许端口22上的入口流量:

    $ gcloud compute firewall-rules list --project name-of-project NAME NETWORK DIRECTION PRIORITY ALLOW default-allow-ssh default INGRESS 65534 tcp:22

    output above firewall rule allowing SSH trafficpriority 65534 . 优先级是0到65535之间的整数,包括0和65535 . 较低的优先级值意味着更高的优先级 . 换句话说,1的优先级高于2.您可以阅读 this document 以获得进一步说明

    更新防火墙规则以设置更高的优先级 . 为此,请运行以下命令:

    $ gcloud compute firewall-rules update --priority 1000 default-allow-ssh
    

    Issue: 我尝试SSH进入实例我仍然遇到同样的错误:ssh:连接到主机X.XX.XX.XX端口22:操作超时错误:(gcloud.compute.ssh)[/ usr / bin / ssh ]退出并返回代码[255]

    检查 ssh service 是否为 running in the instance . 运行以下命令以获取实例的IP:

    $ gcloud compute instances describe [NAME_OF_YOUR_INSTANCE] --format='get(networkInterfaces[0].accessConfigs[0].natIP)'
    

    Install netcat =>是用于读取/写入网络连接的计算机网络实用程序:

    $ sudo apt-get install netcat
    

    Run 以下命令检查命令的输出:

    $ nc [EXTERNAL_IP] 22 
    >
    

    Issue: 运行nc [EXTERNAL_IP] 22没有返回任何内容

    Check 如果你的实例中有 Linux Guest Environment is enabled . 为此,您应该在GCE实例上添加启动脚本 . 要将启动脚本添加到例如:

    • 单击实例名称

    • 点击编辑

    • 转到"custom metadata"部分

    • 在"Key"文本字段中添加:startup-script

    • 在"Value"文本字段中添加: #! /bin/bash sudo systemctl list-unit-files | grep google | grep enabled

    • 保存更改

    More detailed info on startup scripts也可用 .

    然后重新启动实例以允许脚本执行 . Verify that the Linux Guest Environment scripts are installed and running . 为此,请从GCE实例检查串行日志控制台中的启动脚本的输出 . 你可以查看the expected outputs for the different Operating Systems .

    If the Linux Guest Environment is not installed, re-install it . 您可以按照this documentation安装LGE .

相关问题