連線至 Linux VM


本文說明如何連線至具備外部 IP 位址的 Linux 虛擬機器 (VM) 執行個體。如要瞭解如何連線至沒有外部 IP 位址的 VM,請參閱僅限內部 VM 的連線選項

如要瞭解 SSH 連線在 Compute Engine 中的運作方式,包括 SSH 金鑰設定和儲存空間,請參閱「與 Linux VM 的 SSH 連線」。

事前準備

  • 如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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,請按照下列任一分頁中的步驟操作。

主控台

請按照下列步驟,透過 Google Cloud 主控台使用 瀏覽器中的 SSH 連線至 VM:

  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.

    SSH button next to instance name.

gcloud

執行 gcloud compute ssh 指令,即可使用 SSH 連線至 VM:

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

  2. 執行下列指令:

    gcloud compute ssh --project=PROJECT_ID --zone=ZONE VM_NAME

    更改下列內容:

    • PROJECT_ID:含有 VM 的專案 ID
    • ZONE:VM 所在的可用區名稱
    • VM_NAME:VM 名稱

    如果已為 Google Cloud CLI 設定預設屬性,則可省略此指令中的 --project--zone 標記。例如:

    gcloud compute ssh VM_NAME

OpenSSH 用戶端

透過 OpenSSH 用戶端使用 SSH 連線至 VM,請執行下列操作:

  1. 如果尚未將 SSH 金鑰新增至 VM,請先完成這項操作。
  2. 在 Google Cloud 控制台中,前往「VM Instances」(VM 執行個體) 頁面,找出要連線的 VM 外部 IP 位址。

    前往 VM 執行個體

  3. 在工作站上開啟終端機。
  4. 請執行下列指令,連線至 VM:

    ssh -i PATH_TO_PRIVATE_KEY USERNAME@EXTERNAL_IP

    更改下列內容:

    • PATH_TO_PRIVATE_KEY:私密安全殼層金鑰檔案的路徑,對應您新增至 VM 的公開金鑰。
    • USERNAME: 您的使用者名稱。如果您在中繼資料中管理安全殼層金鑰, 使用者名稱就是您在建立安全殼層金鑰時指定的名稱。 對於 OS 登入帳戶,使用者名稱是在 Google 個人資料中定義的名稱。例如 cloudysanfrancisco_example_comcloudysanfrancisco
    • EXTERNAL_IP:VM 的外部 IP 位址。

PuTTY 應用程式

請按照下列步驟,透過 Windows PuTTY 應用程式使用 SSH 連線至 VM:

  1. 如果尚未在 VM 中新增安全殼層金鑰,請先完成這項操作。
  2. 如果工作站尚未安裝 PuTTY 應用程式,請下載 PuTTY 套��檔案
  3. 在 Google Cloud 控制台中,前往「VM Instances」(VM 執行個體) 頁面,找出要連線的 VM 外部 IP 位址。

    前往 VM 執行個體

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

    USERNAME@EXTERNAL_IP

    更改下列內容:

    • USERNAME: 您的使用者名稱。如果您是在中繼資料中管理安全殼層金鑰, 使用者名稱就是您在建立安全殼層金鑰時指定的名稱。 如果是 OS 登入帳戶,使用者名稱會在您的 Google 個人資料中定義。例如 cloudysanfrancisco_example_comcloudysanfrancisco
    • EXTERNAL_IP:VM 的外部 IP 位址。
  6. 在「Category」選單中,依序前往「Connection」>「SSH」>「Auth」
  7. 在「Private key file for authentication」欄位中,選取與您新增至 VM 的公開金鑰相對應的私密安全殼層金鑰檔案。
  8. 按一下「Open」連線至 VM。

Chrome 的 Secure Shell 應用程式

如要透過 Secure Shell Chrome 應用程式使用 SSH 連線至 VM,請執行下列步驟:

  1. 如果尚未將安全殼層金鑰新增至 VM,請先完成這項操作。

  2. 如果尚未在 Chromebook 或 Chrome 瀏覽器上安裝 Secure Shell,請執行此操作。

  3. 在 Google Cloud 控制台中,前往「VM Instances」(VM 執行個體)頁面,找出要連線的 VM 外部 IP 位址。

    前往「VM Instances」(VM 執行個體)

  4. 如要透過 Chrome 瀏覽器分頁網址列開啟 Secure Shell,請按照下列步驟操作:

    • 輸入 ssh
    • 按下 Space 鍵。
    • 按下 Enter 鍵。
  5. 按一下「[新連線]」

  6. 在「使用者名稱」欄位中輸入使用者名稱。如果您在中繼資料中管理安全殼層金鑰,則使用者名稱就是您在建立安全殼層金鑰時指定的名稱。對於 OS 登入帳戶,使用者名稱是在 Google 個人資料中定義的名稱。例如 cloudysanfrancisco_example_comcloudysanfrancisco

  7. 在「主機名稱」欄位中,輸入 VM 的外部 IP 位址。

  8. 在「Identity」欄位中,按一下「Import...」,然後選取私密安全殼層金鑰檔案的路徑,該路徑必須與您新增至 VM 的公開金鑰相符。

  9. 按一下「[ENTER] 連線」,即可連線至 VM。

疑難排解

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

後續步驟