Cover photo

GCP 如何采用第三方工具 ssh 连接

GCP(Google Cloud Platform)是 Google 自研的云计算平台,当购买云主机后会碰到如何远程 SSH连接的问题,因此编写该文档用于为新手简化操作。

方法有很多种,这里介绍几个常见的,本次的环境采用 `Ubuntu 18.04 LTS` 系统进行说明:

方法一:通过密码方式登录

  • 点击 SSH

post image

等待上传私钥

post image
  • 登录成功

post image
  • 执行命

post image

涉及命令如下:

  • 修改 sshd 配置

# 提权
sudo -i
# 允许root登录
sed -i 's/PermitRootLogin no/PermitRootLogin yes/' /etc/ssh/sshd_config
# 允许密码认证
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
  • 检查配置是否修改

# 查看修改是否生效
egrep --color '(PermitRootLogin|PasswordAuthentication)' /etc/ssh/sshd_config

确保如下的配置取消注释:

PermitRootLogin yes
PasswordAuthentication yes
# PasswordAuthentication.  Depending on your PAM configuration,
PermitRootLogin yes
  • 设置root密码,并重启ssh服务

# 设置root密码
passwd root
# 重启sshd服务
systemctl restart sshd

生成证书

说明: 以下的方式均需要用到证书,因此需要先生成证书。

  • 生成证书

post image

说明:并复制公钥内容,自行找到自己的目录下面 .pub 结尾的文件,并复制里面的内容即可,我这里的路径是 ~/.ssh/ssh_gcp_iopssre.pub

方式二:证书的方式登录(如何是新建主机)

  • 点击元数据

post image
  • 点击修改

post image
  • 添加公钥

  • 保存

post image
  • 查看公钥信息

保存成功后,可以看到登录的用户名

post image
  • 回到实例页面

然后创建虚拟机时会自动注入

方式三:针对已经创建的虚拟机配置

  • 回到GCP 点击要远程登录的云主机

post image
  • 点击修改

post image
  • 滚动到安全与访问权限

新增一个公钥,并把刚才复制的公钥内容粘贴进去即可

post image

点击保存即可。

通过三方工具登录

  • 调整为证书模式

post image
  • 浏览公钥,保存即可

post image