建立安全殼層金鑰


本文說明如何為 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.

    REST

    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the 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.

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

建立安全殼層金鑰組

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

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

Linux 和 macOS

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

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

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。詳情請參閱「以 root 使用者身分連線至 VM」。

    如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\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。詳情請參閱「以 root 使用者身分連線至 VM」。

    如果是使用 Active Directory (AD) 的 Windows VM,使用者名稱必須在前方加上 AD 網域,格式為 DOMAIN\。舉例來說,ad.example.com 廣告中的使用者 cloudysanfranciscoexample\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. 在「參數」下方指定下列項目:

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

    工具會顯示公開金鑰值。

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

    對於 Linux VM,除非您將 VM 設為允許根目錄登入,否則 Key comment 不得為 root。詳情請參閱「以 root 使用者身分連線至 VM」。

    對於使用 Active Directory (AD) 的 Windows VM,金鑰註解必須以 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

後續步驟