Google Cloud Platform SSH 連接配置

首先

  1. 創建實例后設置當前用戶的新密碼
$ sudo passwd ${whoami} // 下面以 user 代替 ${whoami}
# 輸入新密碼
  1. 隨便設置下 root 的新密碼
$ sudo passwd root
# 輸入新密碼

在本地生成私鑰和公鑰

$ 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 user@computer-name.local
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 user@computer-name.local
# 把這長長的一段復制下來,把其中的 user@computer-name.local 改為你在瀏覽器 SSH 登入之后的當前用戶名 ${whoami}

導入公鑰

# 進入谷歌云平臺頁面 -> 計算引擎 -> 元數據 -> SSH 密鑰,粘貼保存
# 谷歌就會把上面這段 public key 寫入到 ~/.ssh/authorized_keys

本地通過私鑰登錄

$ ssh -i myKey user@35.189.175.199
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 user@35.189.175.199
Permission denied (publickey).

# 之所以會出現這種情況,因為谷歌默認把密碼驗證登錄關了,需要自行打開
$ sudo vi /etc/ssh/sshd_config
PasswordAuthentication yes 
:wq!

# 改完要重啟 ssh 服務
$ sudo service sshd restart

# 再次連接
$ ssh user@35.189.175.199
user@35.189.175.199'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

# 至此大功告成
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容