宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

众所周知,GCP 的中国台湾节点对大陆十分友好,但小白在新建、配置实例的时候会碰到很多困难,我在第一次使用的时候,也遭遇不少坑,有许多设定是极其反人类的。

下面分享一些经验,给后面的同学做参考。

阅读本文的前提:

  1. 你已经有了一个 Google 账号;

2. 能够正常访问 http://google.com 域名的爱国工具;

3. 已注册 GCP;

4. 已绑定信用卡并获得新注册用户 300 美金的赠送余额。

一、创建实例

登陆 GCP 平台的控制面板:

https://console.cloud.google.com​console.cloud.google.com谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

打开侧边栏,找到「Compute Engine」,在弹出的二级列表中,单击「VM 实例」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

GCP 平台的实例,都是依附于对应的项目下的,所以你需要先新建一个项目,项目名 ID 保持默认,项目名称可按照自己的喜好自定,单击「创建」即可:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

首次创建项目,需要一段时间,请耐心等待:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

继续等,如果时间过长,请刷新浏览器。

当「创建」按钮由灰蓝变蓝,就可以创建实例了:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

挨个讲一下创建实例时的选项:

1. 名称自定,使用默认的也行。

2. 区域选择「asia-east1(中国台湾)」,地区都是中国台湾彰化县,有 a、b、c 可选,我选的是 c,据说网络质量更好一些。

3. 机器类型选择「微型」,租用机器本身按时付费,月计 50.23,流量费0.23/GB,不包含在机器价格内,额外计算,也是用多少算多少,挺金贵的。

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

4. 「容器」功能相当于链接一个远程镜像,当做系统安装盘,用 GCP 自带的镜像就好了,都是最新版的系统,不用选。

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

5. 点击「启动磁盘」的「更改」按钮,可以在 GCP 自带的系统安装镜像里选一个,我习惯用 Debian 系 Linux,选择 Debian 9 或是 Ubuntu 16.04 都是可以的。默认磁盘容量 10GB,富强上网足够,选择更大容量的磁盘需额外计费。

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

6. 「身份和默认 API 权限」及关联选项保持默认

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

7. 「防火墙」选项下的 http 和 https 必须开启,否则你将无法通过这两个协议访问到机器,比如在 GCP 实例上建设网站,访客无法访问

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

8. 不用单独展开「管理、安全、磁盘、网络、单独租用」做额外设置,点击「创建」即可新建实例:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

新创建的实例如下:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

二、配置实例(用户管理、网络设置)

下面我们可以在浏览器里远程连接服务器,在命令行里直接设置 root 用户密码,使用 root 用户登录等。

在「连接」功能的下拉菜单里,点击「在浏览器窗口中打开」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

稍后出现命令行操作界面:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

输入:

sudo passwd root

为 root 用户设置密码。

建议在在线随机密码生成器 生成一个包含大小写字母、特殊符号、数字的 30 位以上随机密码,将密码复制下来粘贴(Ctrl+C)到命令行中,按回车,再输入一次密码,按回车,使密码生效:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

使当前用户具备 root 权限:

su root

再次输入密码,授权成功:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

编辑 ssh 服务配置文件:

vim /etc/ssh/sshd_config

找到这一行:

PermitRootLogin prohibit-password

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

将「prohibit-password」改成「yes」,使允许 root 用户登录:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

并且将「PasswordAuthentication」从默认的「no」改成「yes」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

最后是这样的:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

顺便把 ssh 端口号(Port)改成高端口(范围 10000~65535),避免密码在默认端口被暴力破解:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

按 Esc,输入:

:wq

确认保存并退出。

重新启动 ssh 服务,使更改生效:

/etc/init.d/ssh restart

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

特别地,GCP 配置了自己的防火墙(类似阿里云的安全组规则),默认只允许 22、80、443、3306 等常用端口传入,这就意味着如果我们设置了高端口用本地 ssh 工具连接,以及使用带有动态端口切换功能的敬业代理时,防火墙规则会造成许多麻烦,所以我们要把所有端口的访问都打开。

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

设置非 22 端口用 GCP 自带工具做 ssh 连接,如果未在防火墙里允许放行对应的端口,即使指定对应端口做登陆也会登录失败(疯起来我寄几都咬)。

返回实例管理列表,点击最右边的竖排省略号,在展开的列表点击「查看网络详情」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

鼠标移到左侧列表并展开:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

点击「防火墙规则」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

默认处于「入站」选项卡,点击「创建防火墙规则」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

设置好防火墙规则名称后,「流量方向」选择「入站」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

「对匹配项执行的操作」保持默认「允许」。

「目标」选择「网络中的所有实例」,「来源 IP 地址范围」输入「0.0.0.0/0」(允许所有 IP)。

「次要来源过滤条件」保持默认「无」,「协议和端口」选择「全部允许」,最后点击「创建」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

等待创建完成:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

同样给出站规则设置放行所有 IP、协议和端口,因为法治上网的代理需要允许主机去访问目标网站,把获得的流量转发给客户端,方法如法炮制,直接上图:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

特别地,「来源过滤条件」除了可以设置允许全部内网机器访问外网,你还可以指定「子网」规则之一,如勾选「10.140.0.0/20」(亚洲东部 1 区,即中国台湾机房),不同地域的实例处于不同的子网,从逻辑上,它们从属于同一 GCP 账户下的同一项目中,彼此之间构成了一个局域网。

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客在这里「全选」也是可以的谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客不同地域的机房拥有不同的子网 IP 地址谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客GCP 管理面板首页可直接查看当前实例所在子网的 IP 地址

创建:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

至此,所有的设置就完成了。凭服务器 IP、端口、root 用户名、root 用户密码,就可以在 XShell 里连接到机器了。

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

三、意外处理(实例新建后无系统 & 设置新的用户并采用密钥登录)

3.1

在创建实例时,偶尔会发现「启动磁盘」项没有「更改」按钮,也没有标注系统名称,这意味着实例创建后是没有系统的,更无从远程连接,使用后续功能。碰到这种情况,请返回 VM 实例控制面板首页,先将当前磁盘完全为空的机器删除:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

接着,将左侧列表展开,点击「映像」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

在列表中点击需要安装的系统:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

点击「创建实例」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

注意事项和第一章「创建实例」的要求相同:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

3.2

由于安全方面的考虑,你仍然可以选择自定义用户名,并用对应的密钥登录,你可以先在 Xshell 中生成一个新的密钥文件:

工具(T)→新建用户密钥生成向导(W):

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

选项默认,下一步:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

继续:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

密钥名称自定,设置什么样的密钥名称,就意味着你以什么样的用户名登录系统(不可设置为 root),用户密钥加密密码属于可选项,可不填,意思就是在这个密钥的外壳又添加一个密码,使用密钥的时候需要输入密码:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

是(Y):

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

「用户密钥」里,可看到我们刚才创建的,名称叫「exp」的密钥:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

接着,选中要导入到 GCP 钥匙串的密钥,点击右边的「属性(P)」,并切换到「公钥」选项卡:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

接着,将公钥里的内容(以 ssh-rsa 开头的),将到末尾两个等于号(=)处的内容,复制出来:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

然后,登陆 GCP 的控制面板,点击侧边栏的「元数据」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

点击第二个选项卡——「SSH 密钥」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

修改:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

添加一项:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

把刚才复制来的公钥粘贴进来,此时会提示密钥格式无效:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

不要着急,在密钥末尾的两个等于号之后,打一个空格,再输入任意字母 + 英文数字,ssh 密钥就创建成功了,同时空格后面输入的内容,就是你登录 GCP 实例的用户名:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

保存生效:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

然后,在 Xshell 里建立新连接:

名称(N)自定,主机(H)填 IP,端口号(O)默认 22,如果在 sshd 文件里更改了,请自行用新的端口号:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

点击侧边栏的「用户身份验证」,方法(M)选择「PublicKey」,用户名填你刚才在 GCP 后面板→元数据→ssh 密钥里,在「== 」后面设置过的用户名。

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

「用户密钥」选择你刚才用 Xshell 密钥生成工具生成的那个,点击下方「确定」保存。

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客

快捷键「Alt+O」快速启动连接,选中刚才设置好的那台机器,点击连接(C),就可以成功连上 GCP 实例了,用户名就是我们刚才设置的「exp」:

谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客谷歌GCP(Google Cloud Platform)入门教程-风君雪科技博客