本文件說明如何使用 Identity-Aware Proxy (IAP) TCP 轉送功能,透過虛擬機器 (VM) 執行個體的內部 IP 位址連線。
IAP TCP 轉送功能可讓您建立加密通道,將 SSH 連線轉送至 VM。當您連線至使用 IAP 的 VM 時,IAP 會先將 SSH 連線包裝在 HTTPS 內,再將連線轉送至 VM。接著,IAP 會檢查您是否具備必要的 IAM 權限,如果您具備這些權限,系統就會授予 VM 存取權。
如果您需要連線至沒有外部 IP 位址的 VM,且無法使用 IAP,請參閱僅限內部 VM 的連線選項中列出的其他方法。
事前準備
- 建立防火牆規則,以啟用 IAP 的連線。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
-
支援的作業系統
這些連線方法適用於 Compute Engine 提供的所有公開 Linux 映像檔。針對 Fedora CoreOS 映像檔,您必須先設定 SSH 存取權,才能使用這些方法。
連線至 VM
如要連線至 VM,請按照下列任一分頁中的步驟操作。
主控台
如要透過瀏覽器中的 SSH 建立 SSH 連線通道,請按照下列步驟操作:
- In the Google Cloud console, go to the VM instances page.
- In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.
gcloud
使用 --tunnel-through-iap
標記的 gcloud compute ssh
指令,透過 VM 的內部 IP 位址建立 SSH 通道連線:
請執行下列指令,連線至 VM:
gcloud compute ssh VM-NAME \ --tunnel-through-iap
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
將 VM_NAME
替換為您要連線的 VM 名稱。
IAP Desktop
如要使用 IAP Desktop 連線至 VM,請按照下列步驟操作:
-
如果尚未在工作站上安裝 IAP Desktop,請先完成這項操作。
-
開啟 IAP Desktop。系統隨即會開啟「Add projects」視窗。
-
在系統提示時,使用有權存取您要連線 VM 專案的 Google 帳戶登入。
-
在「Add projects」視窗中,輸入專案 ID 或專案名稱,該專案包含您要連線的 VM。
-
在「Project Explorer」視窗中,再次按一下 VM 名稱,然後選取「Connect」,即可連線至 VM。
PuTTY 應用程式
使用 PuTTY 透過 VM 的內部 IP 位址建立 SSH 通道,方法如下:
- 如果尚未在 VM 中新增安全殼層金鑰,請先完成這項操作。
- 如果工作站尚未安裝 PuTTY 應用程式,請下載 PuTTY 套件檔案。
在 Google Cloud 控制台中,前往「VM Instances」(VM 執行個體) 頁面,然後找出要連線的 VM 名稱。
- 開啟 PuTTY 應用程式。連線設定視窗隨即開啟。
在
Host Name
欄位中,輸入安全殼層金鑰的相關使用者名稱,以及您要連線的 VM 名稱。請使用下列格式:USERNAME@VM_NAME
更改下列內容:
USERNAME
: 您的使用者名稱。如果您是在中繼資料中管理安全殼層金鑰, 使用者名稱就是您在建立安全殼層金鑰時指定的名稱。 如果是 OS 登入帳戶,使用者名稱會在您的 Google 個人資料中定義。例如cloudysanfrancisco_example_com
或cloudysanfrancisco
。-
NAME
:VM 名稱。
- 在「Category」選單中,依序前往「Connection」>「SSH」>「Auth」。
- 在「Private key file for authentication」欄位中,選取與您新增至 VM 的公開金鑰相對應的私密安全殼層金鑰檔案。
- 在「Category」選單中,依序前往「Connection」>「Proxy」。
- 在「Proxy type」部分中,選取「Local」。
在「Telnet 指令或本機 Proxy 指令」欄位中輸入下列指令:
gcloud.cmd compute start-iap-tunnel VM_NAME PORT_NUMBER --listen-on-stdin --project=PROJECT_ID --zone=ZONE
更改下列內容:
VM_NAME
:您要連線的 VM 名稱。PORT_NUMBER
:sshd 守護程式執行的通訊埠。預設PORT_NUMBER
為22
。PROJECT_ID
:代管您要連線的 VM 的專案。ZONE
:VM 所在區域。
- 按一下「Open」連線至 VM。
疑難排解
如要瞭解如何診斷及解決 SSH 連線失敗的問題,請參閱「SSH 疑難排解」一文。
後續步驟
- 瞭解如何管理 VM 存取權。
- 瞭解如何將檔案轉移至 VM。
- 瞭解如何透過 SSH 連線至 Linux VM,在 Compute Engine 上執行。