Google 云主机连接教程

如何创建账号创建实例这里就不说了,网上一大堆教程,而且基本上会有引导,指引一步步完成,这里只说创建好实例之后,如何连接到服务器

在谷歌云后台打开ssh连接控制台

compute Engine ---> 虚拟机实例 就会看到自己创建的实例


1586151128(1).png

设置root密码

  1. 设置下 root 的新密码
$ sudo passwd root
# 输入新密码
1586151640(1).png

在本地生成私钥和公钥

$ cd ~/.ssh
$ ssh-keygen -f myKey
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): (给 private key 设置一个密码,避免私钥被人盗用的风险)
Enter same passphrase again: (再次输入上次相同密码)
Your identification has been saved in myKey.
Your public key has been saved in myKey.pub.
The key fingerprint is:
SHA256:EW7ow1wCKAh1rM/GG08ZAwOy+7+SUiT0rFXY2f8mNvk [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|=.o+= o .        |
|o+.o+= + .       |
|o.o..oo *        |
|..o+ +o+ o       |
|.oo+  =+S o      |
| o. * o. = o     |
| ..o =  . =      |
|. o.. .    E     |
| . .o.           |
+----[SHA256]-----+
# 此时会生成 公钥 myKey.pub 和 私钥 myKey

复制公钥

$ cat myKey.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjHKPaeglRVJzAhNq+W
中间部分省略。。。
dKx8sJ0Rw4kUqm2eU2vo8S5IEA0Nk2f7BtVGE8VOCHgmDbv2tLp9845UVp1 [email protected]

把这长长的一段复制下来,把其中的 [email protected] 改为你在浏览器 SSH 登入之后的当前用户名 ${whoami}

导入公钥

进入谷歌云平台页面 -> 计算引擎 -> 元数据 -> SSH 密钥,粘贴保存
谷歌就会把上面这段 public key 写入到 ~/.ssh/authorized_keys

这里我没有进行复制粘贴这一步,打开之后默认就已经存在在这里了,忘了是哪里添加过了
1586152032(1).png

本地通过私钥登录

$ ssh -i myKey root@34.***.***.***          // 替换为自己的ip地址
Enter passphrase for key 'myKey': (输入 private key 密码)
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.11.2-041102-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

83 packages can be updated.
0 updates are security updates.

Last login: Sun Oct  8 06:40:43 2017 from 115.200.175.117

通过 SSH 密码验证登录

$ ssh root@34.***.***.***
Permission denied (publickey).

之后会出现访问被拒绝
之所以会出现这种情况,因为谷歌默认把密码验证登录关了,需要自行打开

修改 SSH 登录配置

$ sudo vi /etc/ssh/sshd_config
PermitRootLogin yes           //默认为no,需要开启root用户访问改为yes
PasswordAuthentication yes    //默认为no,改为yes开启密码登陆

修改完按ESC 然后按shift + : 输入 wq 回车保存退出

改完要重启 ssh 服务

$ reboot

再次连接

$ ssh root@34.***.***.***
root@34.***.***.***'s password: (输入实例用户的密码)
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.11.2-041102-generic x86_64)
 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

83 packages can be updated.
0 updates are security updates.

Last login: Sun Oct  8 06:59:24 2017 from 115.200.175.117

至此大功告成

你可能感兴趣的:(Google 云主机连接教程)