本文說明如何設定 OS 登入和 OS 登入的雙重驗證 (2FA) 功能。
您可以使用 OS 登入功能,根據 IAM 權限控管虛擬機器 (VM) 執行個體的存取權。您可以使用 OS Login 搭配或不搭配 2FA,但如果不使用 OS Login,就無法使用 2FA。如要進一步瞭解 OS 登入和 OS 登入 2FA,包括 OS 登入支援的驗證類型,請參閱「關於 OS 登入」。
事前準備
- 如果您想使用 OS Login 2FA,請在網域或帳戶中啟用 2FA:
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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
-
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.
- Set a default region and zone.
Terraform
To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
-
限制
下列虛擬機器不支援 OS 登入:- Windows Server 和 SQL Server VM
- Fedora CoreOS VM。如要管理使用這些映像檔建立的 VM 執行個體存取權,請使用 Fedora CoreOS 點火系統
指派 OS Login 身分與存取權管理角色
將所有必要的 IAM 角色指派給已啟用 OS 登入功能的 VM 連線使用者。
角色 | 必要使用者 | 授權層級 |
---|---|---|
roles/compute.osLogin 或 roles/compute.osAdminLogin |
所有使用者 |
如果使用者需要透過 Google Cloud 控制台或 Google Cloud CLI 存取 SSH,您必須在專案層級授予這些角色,或是在專案層級授予含有 |
roles/iam.serviceAccountUser |
所有使用者 (如果 VM 有服務帳戶) | 在「Service account」(服務帳戶)。 |
roles/compute.osLoginExternalUser
| 使用者所屬機構與連線 VM 不同 |
此角色必須由機構管理員授予。 |
啟用 OS 登入功能
您可以設定 OS Login 中繼資料,為單一 VM 或專案中的所有 VM 啟用 OS Login 或 OS Login 雙重驗證。
設定 OS 登入中繼資料後,Compute Engine 會刪除 VM 的 authorized_keys
檔案,且不再接受來自專案或執行個體中繼資料中儲存的安全殼層金鑰連線。
為專案中的所有 VM 啟用 OS 登入功能
如要為專案中的所有 VM 啟用 OS 登入功能,請在專案中繼資料中設定下列值:
- 啟用 OS 登入:
- 鍵:
enable-oslogin
- 值:
TRUE
- 鍵:
- (選用) 啟用雙重驗證:
- 鍵:
enable-oslogin-2fa
- 值:
TRUE
- 鍵:
為單一 VM 啟用 OS 登入功能
如要為單一 VM 啟用 OS 登入功能,請在執行個體中繼資料中設定下列值:
- 啟用 OS 登入:
- 鍵:
enable-oslogin
- 值:
TRUE
- 鍵:
- (選用) 啟用雙重驗證:
- 鍵:
enable-oslogin-2fa
- 值:
TRUE
- 鍵:
在建立 VM 時啟用 OS 登入功能
使用 Google Cloud 控制台或 gcloud CLI 建立 VM 時,啟用 OS 登入功能 (可選,搭配 2 步驟驗證)。
主控台
使用公開映像檔建立 VM,並指定下列設定,即可建立可在啟動時啟用 OS 登入和 (選用) OS 登入 2FA 的 VM:
- 展開「Advanced options」(進階選項) 區段。
- 展開「安全性」專區。
- 展開「管理存取權」部分。
- 選取「透過 IAM 權限控管 VM 存取權」。
- 選用:如要啟用 OS 登入雙重驗證,請選取「要求兩步驟驗證」。
- 按一下「建立」,即可建立並啟動 VM。
gcloud
-
In the Google Cloud console, 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.
請執行下列任一
gcloud compute instance create
指令,建立可在啟動時啟用 OS 登入功能和 (選用) OS 登入 2FA 的 VM:如要僅啟用 OS 登入功能,請執行下列指令:
gcloud compute instances create VM_NAME \ --image-family=IMAGE_FAMILY \ --image-project=IMAGE_PROJECT \ --metadata enable-oslogin=TRUE
如要啟用 OS Login 2FA,請執行下列指令:
gcloud compute instances create VM_NAME \ --image-family=IMAGE_FAMILY \ --image-project=IMAGE_PROJECT \ --metadata enable-oslogin=TRUE,enable-oslogin-2fa=TRUE
更改下列內容:
Terraform
您可以使用下列其中一個選項,將中繼資料值套用至專案或 VM:
選項 1:在全專案範圍的中繼資料內設定
enable-oslogin
,以將其套用至專案中的所有 VM。使用
google_compute_project_metadata
Terraform 資源,並在oslogin=TRUE
中設定中繼資料值:或者,您也可以將
enable-oslogin
設為FALSE
,停用 OS 登入功能。選項 2:在新或現有 VM 的中繼資料中設定
enable-oslogin
。請使用
google_compute_instance
Terraform 資源,並設定oslogin=TRUE
。將oslogin_instance_name
替換為您的 VM 名稱。或者,您也可以將
enable-oslogin
設為FALSE
,藉此停用 VM 的 OS 登入功能。
連線至已啟用 OS 登入功能的 VM
使用「連線至 Linux VM」一文所述的方法,連線至已啟用 OS 登入功能的 VM。
當您連線至已啟用 OS 登入功能的 VM 時,Compute Engine 會使用機構管理員為您設定的使用者名稱。如果貴機構的管理員尚未為您設定使用者名稱,Compute Engine 會產生格式為 USERNAME_DOMAIN_SUFFIX
的使用者名稱。如要進一步瞭解使用者名稱,請參閱「OS 登入的運作方式」。
當您連線至已啟用 OS Login 2FA 的 VM 時,也會看到系統根據所選兩步驟驗證方法或驗證類型所發出的訊息。如果使用的方法是手機提示,請在您的手機或平板上接受提示,才能繼續操作。如果是其他方法,請輸入安全碼或動態密碼。
排解 OS 登入相關問題
如要瞭解如何診斷及解決 OS 登入錯誤,請參閱「OS 登入疑難排解」一文。
後續步驟
- 瞭解如何透過 SSH 連線至 Linux VM,在 Compute Engine 上執行工作負載。
- 瞭解如何搭配安全金鑰使用 SSH,進一步限制 VM 存取權。