本文說明如何使用 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 的連線。
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:
gcloud init
- Set a default region and zone.
支援的作業系統
Compute Engine 提供的所有公開 Linux 映像檔都支援這些連線方法。如果是 Fedora CoreOS 映像檔,您必須先設定 SSH 存取權,才能使用這些方法。
連線至 VM
如要連線至 VM,請按照下列分頁中的步驟進行。
主控台
使用瀏覽器中的 SSH,透過 VM 的內部 IP 位址建立 SSH 連線通道,方法如下:
- In the Trusted 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
使用
gcloud compute ssh
指令和--tunnel-through-iap
標記,透過 VM 的內部 IP 位址建立 SSH 連線通道:執行下列指令,連線至 VM:
gcloud compute ssh VM-NAME \ --tunnel-through-iap
-
如果尚未在工作站上安裝 IAP Desktop,請先完成這項程序。
-
開啟 IAP Desktop。「新增專案」視窗隨即開啟。
-
看到提示訊息後,請使用可存取專案的 Google 帳戶登入,專案中包含您要連線的 VM。
-
在「新增專案」視窗中,輸入要連線的 VM 所在專案的 ID 或名稱。
-
在「Project Explorer」視窗中,再次以滑鼠右鍵按一下 VM 名稱,然後選取「Connect」,即可連線至 VM。
- 如果尚未新增安全殼層金鑰,請新增安全殼層金鑰至 VM。
- 如果工作站尚未安裝 PuTTY 應用程式,請下載 PuTTY 套件檔案。
前往 Trusted Cloud 控制台的「VM Instances」(VM 執行個體) 頁面,並找到要連線的 VM 名稱。
- 開啟 PuTTY 應用程式,連線設定視窗隨即開啟。
在
Host Name
欄位中,輸入安全殼層金鑰的相關使用者名稱,以及要連線的 VM 名稱。請使用下列格式:USERNAME@VM_NAME
更改下列內容:
USERNAME
: 使用者名稱就是您在建立安全殼層金鑰時指定的使用者名稱。-
NAME
:VM 的名稱。
- 在「Category」選單中,依序前往「Connection」>「SSH」>「Auth」。
- 在「Private key file for authentication」欄位中,選取與您新增至 VM 的公開金鑰對應的私密安全殼層金鑰檔案。
- 在「Category」選單中,依序前往「Connection」>「Proxy」。
- 在「Proxy type」部分,選取「Local」。
在「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_NUMBER
為22
。PROJECT_ID
:要連線的 VM 所在專案。ZONE
:VM 所在的區域。
- 按一下「開啟」即可連線至 VM。
In the Trusted Cloud console, activate Cloud Shell.
At the bottom of the Trusted 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,請按照下列步驟操作:
PuTTY 應用程式
使用 PuTTY 透過 VM 的內部 IP 位址建立 SSH 連線通道,方法如下:
疑難排解
如要瞭解如何診斷及解決 SSH 連線失敗的問題,請參閱「排解 SSH 問題」。
後續步驟
- 瞭解如何將檔案轉移至 VM。
- 瞭解如何使用 SSH 連線至 Compute Engine 的 Linux VM。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-08 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-08 (世界標準時間)。"],[[["Identity-Aware Proxy (IAP) TCP forwarding enables secure SSH connections to virtual machines (VMs) via an encrypted tunnel, even if the VM lacks an external IP address."],["To use IAP, you must have the correct IAM permissions and create a firewall rule to allow connections, and for VMs without external IPs it will use IAP to connect."],["Connecting to Linux VMs can be done through the Google Cloud console's SSH-in-Browser feature, the `gcloud compute ssh` command with the `--tunnel-through-iap` flag, IAP Desktop, or the PuTTY app."],["When connecting via the Google Cloud console or gcloud CLI, ephemeral or persistent SSH keys, respectively, are automatically created by Compute Engine."],["The connection methods are supported for public Linux images available on Compute Engine, but Fedora CoreOS images require prior SSH setup."]]],[]] -