建立安全殼層金鑰


本文說明如何為 Compute Engine 虛擬機器 (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.
      3. REST

        如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

          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.

        詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

建立安全殼層金鑰組

如果您使用 Google Cloud 控制台或 Google Cloud CLI 連線至 VM,Compute Engine 會代您建立 SSH 金鑰。如要進一步瞭解 Compute Engine 如何設定及儲存金鑰,請參閱關於 SSH 連線

如果您使用第三方工具或 OpenSSH 連線至 VM,則必須先將金鑰新增至 VM,才能連線。如果您沒有安全殼層金鑰,請務必建立一個。VM 接受 sshd_config 檔案中列出的金鑰格式。

Linux 和 macOS

在 Linux 和 macOS 工作站上,使用 ssh-keygen 公用程式建立新的 SSH 金鑰組。以下範例會建立 RSA 金鑰組。

開啟終端機,然後使用 ssh-keygen 指令搭配 -C 旗標,建立新的 SSH 金鑰組。

ssh-keygen -t rsa -f ~/.ssh/KEY_FILENAME -C USERNAME

更改下列內容:

  • KEY_FILENAME:安全殼層金鑰檔案的名稱。

    例如,一個名為 my-ssh-key 的檔案會產生一個名為 my-ssh-key 的私密金鑰檔案和一個名為 my-ssh-key.pub 的公開金鑰檔案。

  • USERNAME:VM 上的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

    如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以根使用者身分連線至 VM」。

    如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為 DOMAIN\。舉例來說,ad.example.com AD 中的使用者 cloudysanfrancisco 具有 example\cloudysanfranciscoUSERNAME

ssh-keygen 會將私密金鑰檔案儲存至 ~/.ssh/KEY_FILENAME,並將公開金鑰檔案儲存至 ~/.ssh/KEY_FILENAME.pub

使用者 cloudysanfrancisco 的公開金鑰類似如下所示:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF... cloudysanfrancisco

Windows 10 以上版本

在 Windows 10 以上版本的工作站上,請使用 ssh-keygen 公用程式建立新的 SSH 金鑰組。以下範例會建立 RSA 金鑰組。

開啟命令提示字元,然後使用 ssh-keygen 指令,並加上 -C 旗標,建立新的 SSH 金鑰組。

ssh-keygen -t rsa -f C:\Users\WINDOWS_USER\.ssh\KEY_FILENAME -C USERNAME

更改下列內容:

  • WINDOWS_USER:Windows 電腦上的使用者名稱。

  • KEY_FILENAME:安全殼層金鑰檔案的名稱。

    例如,一個名為 my-ssh-key 的檔案會產生一個名為 my-ssh-key 的私密金鑰檔案和一個名為 my-ssh-key.pub 的公開金鑰檔案。

  • USERNAME:VM 上的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

    如果是 Linux VM,除非您將 VM 設為允許根目錄登入,否則 USERNAME 無法為 root。詳情請參閱「以根使用者身分連線至 VM」。

    如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須加上 AD 網域前置字元,格式為 DOMAIN\。舉例來說,ad.example.com AD 中的使用者 cloudysanfrancisco 具有 example\cloudysanfranciscoUSERNAME

ssh-keygen 會將私密金鑰檔案儲存至 C:\Users\WINDOWS_USER\.ssh\KEY_FILENAME,並將公開金鑰檔案儲存至 C:\Users\WINDOWS_USER\.ssh\KEY_FILENAME.pub

使用者 cloudysanfrancisco 的公開金鑰類似如下所示:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF... cloudysanfrancisco

Windows 8 或更早版本

在 Windows 8 之前的版本,請使用 PuTTYgen 工具建立新的 SSH 金鑰組。以下範例會建立 RSA 金鑰組。

  1. 如果尚未下載 puttygen.exe,請執行此操作。

  2. 開啟 PuTTYgen。

  3. 在「Parameters」下方,指定下列項目:

    • 要產生的金鑰類型RSA
    • 產生的金鑰位元數2048 以上
  4. 按一下「產生」,然後按照畫面上的指示操作。

    工具會顯示公開金鑰值。

  5. 在「Key comment」區段中,將預先填入的文字取代為您的使用者名稱。例如 cloudysanfranciscocloudysanfrancisco_gmail_com

    如果是 Linux VM,除非您將 VM 設定為允許根目錄登入,否則「金鑰註解」不能為 root。詳情請參閱「以根使用者身分連線至 VM」。

    如果 Windows VM 使用 Active Directory (AD),則金鑰註解必須以 AD 網域為開頭,格式為 DOMAIN\。舉例來說,ad.example.com AD 中的使用者 cloudysanfranciscoexample\cloudysanfrancisco重要留言

  6. 選用:輸入「Key passphrase」(金鑰通關密語),為金鑰設定密碼保護。

  7. 按一下「儲存私密金鑰」,選擇私密金鑰的儲存位置。

    PuTTYgen 會將私密金鑰寫入副檔名為 .ppk 的檔案。

  8. 按一下「儲存公開金鑰」,選擇要儲存公開金鑰的位置。請勿關閉 PuTTYgen 視窗。

  9. 複製「Public key for pasting into OpenSSH authorized_keys file」欄位的文字。

  10. 開啟公開金鑰檔案。公開金鑰的格式類似以下範例:

    ---- BEGIN SSH2 PUBLIC KEY ----
    Comment: "USERNAME"
    KEY_VALUE
    ---- END SSH2 PUBLIC KEY ----
    
  11. 將公開金鑰檔案的全部內容,替換為從「Public key for pasting into OpenSSH authorized_keys file」欄位複製的值,使公開金鑰檔案符合以下格式:

    KEY_VALUE USERNAME
    

使用者 cloudysanfrancisco 的公開金鑰類似如下所示:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAu5kKQCPF... cloudysanfrancisco

後續步驟