使用 Identity-Aware Proxy 連線至 Linux VM


本文說明如何使用 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

      1. 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.

      2. Set a default region and zone.

支援的作業系統

Compute Engine 提供的所有公開 Linux 映像檔都支援這些連線方法。如果是 Fedora CoreOS 映像檔,您必須先設定 SSH 存取權,才能使用這些方法。

連線至 VM

如要連線至 VM,請按照下列分頁中的步驟進行。

主控台

使用瀏覽器中的 SSH,透過 VM 的內部 IP 位址建立 SSH 連線通道,方法如下:

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. In the list of virtual machine instances, click SSH in the row of the instance that you want to connect to.

gcloud

使用 gcloud compute ssh 指令--tunnel-through-iap 標記,透過 VM 的內部 IP 位址建立 SSH 連線通道:

    In the Google Cloud console, activate Cloud Shell.

    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.

  1. 執行下列指令,連線至 VM:

    gcloud compute ssh VM-NAME \
        --tunnel-through-iap
  2. VM_NAME 替換為要連線的 VM 名稱。

IAP Desktop

如要使用 IAP Desktop 連線至 VM,請按照下列步驟操作:

  1. 如果尚未在工作站上安裝 IAP Desktop,請先完成這項程序。

  2. 開啟 IAP Desktop。「新增專案」視窗隨即開啟。

  3. 看到提示訊息後,請使用可存取專案的 Google 帳戶登入,專案中包含您要連線的 VM。

  4. 在「新增專案」視窗中,輸入要連線的 VM 所在專案的 ID 或名稱。

  5. 在「Project Explorer」視窗中,再次以滑鼠右鍵按一下 VM 名稱,然後選取「Connect」,即可連線至 VM。

PuTTY 應用程式

使用 PuTTY 透過 VM 的內部 IP 位址建立 SSH 連線通道,方法如下:

  1. 如果尚未新增安全殼層金鑰,請新增安全殼層金鑰至 VM。
  2. 如果工作站尚未安裝 PuTTY 應用程式,請下載 PuTTY 套件檔案
  3. 前往 Google Cloud 控制台的「VM Instances」(VM 執行個體) 頁面,並找到要連線的 VM 名稱。

    前往 VM 執行個體

  4. 開啟 PuTTY 應用程式,連線設定視窗隨即開啟。
  5. Host Name 欄位中,輸入安全殼層金鑰的相關使用者名稱,以及要連線的 VM 名稱。請使用下列格式:

    USERNAME@VM_NAME

    更改下列內容:

    • USERNAME: 您的 使用者名稱。如果您在中繼資料中管理安全殼層金鑰, 使用者名稱就是您在建立安全殼層金鑰時指定的使用者名稱。 如果是 OS 登入帳戶,使用者名稱會定義在 Google 個人資料中。例如 cloudysanfrancisco_example_comcloudysanfrancisco
    • NAME:VM 的名稱。
  6. 在「Category」選單中,依序前往「Connection」>「SSH」>「Auth」
  7. 在「Private key file for authentication」欄位中,選取與您新增至 VM 的公開金鑰對應的私密安全殼層金鑰檔案。
  8. 在「Category」選單中,依序前往「Connection」>「Proxy」
  9. 在「Proxy type」部分,選取「Local」
  10. 在「Telnet command, or local proxy command」欄位中,輸入下列指令:

    gcloud.cmd compute start-iap-tunnel VM_NAME PORT_NUMBER --listen-on-stdin --project=PROJECT_ID --zone=ZONE

    更改下列內容:

    • VM_NAME:要連線的 VM 名稱。
    • PORT_NUMBER:sshd daemon 執行的通訊埠。預設 PORT_NUMBER22
    • PROJECT_ID:要連線的 VM 所在專案。
    • ZONE:VM 所在的區域。
  11. 按一下「開啟」即可連線至 VM。

疑難排解

如要瞭解如何診斷及解決 SSH 連線失敗的問題,請參閱「排解 SSH 問題」。

後續步驟