linux 本地終端 SSH 連接 gcp (Google Cloud Platform ) 配置教程

準備

首先在gcp創建實例

1.設置當前用戶的新密碼

$ sudo passwd ${whoami} // 下面以 user 代替 ${whoami}
# 輸入新密碼

2.設置下 root 的新密碼

$ sudo passwd root
# 輸入新密碼

3.在本地生成私鑰和公鑰

$ cd ~/.ssh
$ ssh-keygen -f myKey
或者
$ ssh-keygen -t rsa -f ~/.ssh/my-ssh-key -C [USERNAME]

Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): ( 按enter鍵即可)
Enter same passphrase again: ( 按enter鍵即可)
Your identification has been saved in myKey.
Your public key has been saved in myKey.pub.
The key fingerprint is:
SHA256:EW7ow1aaaaaf8mNvk [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

4.複製公鑰

$ cat myKey.pub
ssh-rsa AAAAaaaaaaaeglRVJzAhNq+W
中間部分省略。。。
dKx8sJ0Rw4aaaaaa845UVp1 [email protected]
# 把這長長的一段複製下來,把其中的 [email protected] 改爲你在瀏覽器 SSH 登入之後的當前用戶名 ${whoami}

5.導入公鑰

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

6.本地通過私鑰登錄

$ ssh -i myKey user@ip
$ ssh-rsa [KEY_VALUE] [USERNAME]

Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.11.2-041102-generic x86_64)

7.或者通過 SSH 密碼驗證登錄

$ ssh user@ip
Permission denied (publickey).

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

8.改完要重啓 ssh 服務

$ sudo service sshd restart

9.再次連接

$ ssh user@ip
user@IP's password: (輸入實例用戶的密碼)
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.11.2-041102-generic x86_64)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章