提示:第一次连接自备魔法,开了谷歌服务器的估计没有这个问题


前言

在我看来谷歌云这个服务器的SSH连接非常抽象,我的这个应用服务器不上魔法就能访问,但是我想SSH操作又要先登到谷歌云,这显然不合理,还不让密码登录,我折腾了一下Putty可以连上,xshell也行。


一、不必手动创建SSH密钥

一开始我参照了文档,先生成密钥,再添加到元数据。
谷歌云官方文档
折腾了半天后连不上,完全没有必要,直接使用Google Cloud CLI就可以了,会自动帮你生成。

二、使用步骤

1.安装Google Cloud CLI

先安装Putty
点击直接下载 windows 64 位
其他需求访问官网 putty
在这里插入图片描述
没有技巧,直接安装即可,装完关了就行,不用打开设置什么。

然后安装Google Cloud CLI
(推荐)下载Google Cloud CLI安装程序
或者 可以打开 PowerShell 终端,然后运行以下 PowerShell 命令:

(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe") & $env:Temp\GoogleCloudSDKInstaller.exe

效果一样,包比较大,推荐网比较好的XX(有些网络会导致安装失败,确保自己的网络环境比较优)。完事后打开:
安装程序
一路默认点击安装即可,没有多余操作。

2.初始化连接

安装时候没乱点的话,安装完会自动打开Google命令行:
在这里插入图片描述
第一次进来是这样:

Welcome to the Google Cloud CLI! Run "gcloud -h" to get the list of available commands.
---
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

You must sign in to continue. Would you like to sign in (Y/n)?

!耐心等待命令行输出直到最后一行问你 (Y/N)
问你要不要登录 回车即可 会打开浏览器让你登陆谷歌账号
不要关闭命令行 登陆后回来看:

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?一大串链接

You are signed in as: [你的谷歌号@gmail.com].

这个登录成功了,下面让你做一些基础配置,配置你当前默认连接的GoogleCloud的项目和区域(创建实例时选的服务器区域),这样你连的时候直接指定实例名就可以连到机器了,不然你得敲命令指定工程ID和区域代码:

Pick cloud project to use:
 [1] abc-abc-123-c2
 [2] Enter a project ID
 [3] Create a new project
Please enter numeric choice or text value (must exactly match list item):  1

Your current project has been set to: [abc-abc-123-c2].

让你选默认配置的项目ID,选项1是你已有的项目ID,选项2是输入项目ID,选项3是创建一个项目,就是下面这个
在这里插入图片描述
你和我一样只有一个项目并且创建好了实例,无脑选1就行,创建好实例再来做这一篇文章的操作

然后会让你服务器所在的区域

Do you want to configure a default Compute Region and Zone? (Y/n)?

Which Google Compute Engine zone would you like to use as project default?
If you do not specify a zone via a command line flag while working with Compute Engine resources, the default is
assumed.
 [1] us-east1-b
 [2] us-east1-c
 [3] us-east1-d
 [4] us-east4-c
 [5] us-east4-b
 ...
 Did not print [75] options.
Too many options [125]. Enter "list" at prompt to print choices fully.
Please enter numeric choice or text value (must exactly match list item):  12
 

按序号选区域,选你想连的服务器在的区域,当然这得在你刚刚选的项目里,就是下面这个,对着选就行
在这里插入图片描述
但这就结束了配置等待输出:

Your project default Compute Engine zone has been set to [us-west1-c].
You can change it by running [gcloud config set compute/zone NAME].

Your project default Compute Engine region has been set to [us-west1].
You can change it by running [gcloud config set compute/region NAME].

Created a default .boto configuration file at [C:\Users\12345\.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
The Google Cloud CLI is configured and ready to use!

* Commands that require authentication will use 你的谷歌号@gmail.com by default
* Commands will reference project `abc-abc-123-c2` by default
* Compute Engine commands will use region `us-west1` by default
* Compute Engine commands will use zone `us-west1-c` by default

Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the CLI like arg files and output formatting
* Run `gcloud cheat-sheet` to see a roster of go-to `gcloud` commands.

这时候你就可以用命令行操做了,还是在这个命令行窗口,输入以下命令:

gcloud compute ssh 你的虚拟机实例的名称

等待输出,会帮你生产ssh的密钥,如下

C:\Users\12345\AppData\Local\Google\Cloud SDK>gcloud compute ssh 你的虚拟机实例的名称
WARNING: The private SSH key file for gcloud does not exist.
WARNING: The public SSH key file for gcloud does not exist.
WARNING: The PuTTY PPK SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: SSH keygen will be executed to generate a key.
Updating project ssh metadata...⠛Updated [https://www.googleapis.com/compute/v1/projects/你的项目ID].
Updating project ssh metadata...done.
Waiting for SSH key to propagate.

不用管,完事自动给你打开putty帮你连接上,至此就完成了第一次连接。

3.常态连接

之后再连就不要使用googleSDK了,直接打开putty
在这里插入图片描述
这里按官方文档说的,不能直接输ip,用户名是什么呢,你按我的文档生成,那就是你本机的用户名;有更准确的办法,在谷歌云看密钥:
在这里插入图片描述
然后选择你本地的私钥文件,就在C:\Users\你的用户名.ssh文件夹里,找到这个文件就行,软件会帮你筛选
在这里插入图片描述
这样就行了,点putty右下角open即可,这样就连接上了。

4.xshell连接


经过尝试xshell也可以连接
在这里插入图片描述
这里正常输入ip即可,下面比较重要,不需要网上说的配主机密钥,我试了配不上
在这里插入图片描述
用户名要填,就是你ssh密钥的用户名,跟上面putty那个一样的,直接连接
在这里插入图片描述
这个的影响我不太懂,但我觉得都行,我选的的一次性,让后会让你选密钥文件
在这里插入图片描述
选择没有扩展名的私钥文件,确定即可
在这里插入图片描述
大功告成

总结

总的来说我觉得谷歌这一套很扯淡,不方便,有其他好办法麻烦告知。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐