本教學課程示範如何在 Compute Engine 的 Microsoft Windows 虛擬機器 (VM) 執行個體上設定 Chrome 遠端桌面服務。如需 Linux VM 的個別操作說明,請參閱「Linux 虛擬機器」。Chrome 遠端桌面可讓您透過本機電腦或行動裝置的圖形使用者介面,遠端存取應用程式。
在本教學課程中,預設的防火牆規則會允許 Chrome 遠端桌面連線,因此您不需要設定任何額外的防火牆規則。
VM 需要存取網際網路 (使用外部 IP 位址或透過 Cloud NAT),且您必須使用 Google 帳戶進行驗證和授權。
本文將說明兩種設定 Chrome 遠端桌面的方法:
使用 Windows 遠端桌面通訊協定 (RDP) 的互動式方法。
使用這個方法時,您必須透過 RDP 用戶端,從本機電腦直接存取 VM,但這在某些情況下可能無法實現。
非互動式方法,使用啟動指令碼在建立 VM 時安裝及設定 Chrome 遠端桌面。
如果防火牆會阻止直接存取 VM,或是您無法存取 RDP 用戶端 (例如在 ChromeOS 上),就應使用此方法。
本教學課程假設您熟悉 Microsoft Windows 和 PowerShell 指令列。
如要瞭解建立虛擬工作站的其他選項,請參閱「建立虛擬工作站」。
目標
- 建立要用以執行 Chrome 遠端桌面的 Windows Compute Engine VM 執行個體。
- 在 VM 執行個體上安裝及設定 Chrome 遠端桌面服務。
- 從您的本機電腦連線至 VM 執行個體上的桌面環境。
費用
本教學課程使用 Google Cloud的計費元件,包括:
- Compute Engine
使用 Pricing Calculator 可根據您的預測使用量來產生費用預估。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Compute Engine API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Compute Engine API.
-
Make sure that you have the following role or roles on the project: roles/compute.admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往「身分與存取權管理」頁面 - 選取專案。
- 按一下 「授予存取權」。
-
在「New principals」(新增主體) 欄位中輸入使用者 ID。 通常是 Google 帳戶的電子郵件地址。
- 在「請選擇角色」清單中,選取角色。
- 如要授予其他角色,請按一下 「Add another role」(新增其他角色),然後新增其他角色。
- 按一下 [Save]。
- 您在本機電腦上使用 Google Chrome 瀏覽器。
- 如果您使用的是互動式方法,本機電腦必須有 RDP 用戶端,並且能夠與遠端 VM 執行個體建立直接 RDP 連線。
-
完成本文件所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱「清除所用資源」。
使用 RDP 進行互動式安裝
如要以互動方式安裝 Chrome 遠端桌面,您必須能夠使用 RDP 用戶端連線至遠端 VM。在本教學課程中,您會在預設 VPC 中建立 VM,並套用預設防火牆規則,這樣就能將 RDP 通訊埠 3339 公開至網際網路。
如果您無法在環境中執行這項操作,請使用本文件稍後所述的非互動式方法。
建立 Compute Engine 執行個體
為配合本教學課程的目的,我們會使用預設的機器類型。如果您要將此環境用於自己的環境,建議您調整機器類型、名稱、區域、啟動磁碟大小或其他設定。
主控台
- 前往 Google Cloud 控制台的「VM Instances」(VM 執行個體) 頁面:
按一下 [建立]。
將執行個體名稱設為
crdhost
。請勾選「Enable display device」核取方塊,因為 Chrome 遠端桌面需要在 Windows VM 上使用顯示裝置。
在「Boot disk」下方,按一下「Change」,開啟「Boot disk」面板。
在「作業系統」清單中,選取「Windows Server」。
在「Version」清單中,選取「Windows Server 2022 Datacenter」。
按一下「選取」關閉面板。
按一下 [建立]。
Cloud Shell
開啟 Cloud Shell。
設定偏好可用區:
ZONE=us-central1-b REGION=us-central1 gcloud config set compute/zone "${ZONE}"
使用 Windows Server 2022 Datacenter 的應用程式映像檔建立 Compute Engine 執行個體:
gcloud compute instances create crdhost \ --machine-type=e2-medium \ --scopes=cloud-platform \ --enable-display-device \ --image-family=windows-2022 \ --image-project=windows-cloud \ --boot-disk-size=50GB \ --boot-disk-device-name=crdhost
這個指令會建立 Windows Server 2022 虛擬機器,其中包含已連結的顯示裝置 (Windows VM 上的 Chrome 遠端桌面需要此裝置) 和 50 GB 的啟動磁碟,並授予執行個體對Google Cloud API 的完整存取權。
忽略磁碟效能警告,因為本教學課程不需要高效能。
使用 RDP 連線至 VM 執行個體
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
請確認
crdhost
執行個體名稱旁有綠色勾號 check,表示執行個體已就緒。按一下執行個體名稱
crdhost
,開啟「VM 執行個體詳細資料」頁面。在「遠端存取」下方,按一下「設定 Windows 密碼」,然後按一下「設定」,在遠端機器上建立帳戶。
系統會在這個步驟產生密碼,記下密碼或將密碼複製到安全的臨時檔案中。
如要連線至遠端執行個體,請按一下「RDP」按鈕旁邊的箭頭 arrow_drop_down,然後選取「下載遠端桌面協定檔案」。您可以使用偏好的 RDP 用戶端開啟 RDP 檔案。
當遠端桌面協定用戶端提示輸入密碼時,請輸入先前產生的密碼。
當系統詢問您是否要讓網路上的其他電腦和裝置偵測到您的電腦時,請按一下「No」(否)。
關閉 Server Manager 資訊主頁 (如果已開啟)。
安裝 Chrome 遠端桌面服務
接下來,請在 VM 執行個體上安裝 Google Chrome 和 Chrome 遠端桌面服務。
在 RDP 工作階段中,按一下 Windows 工作列上的「Start」,輸入
PowerShell
,然後選取 Windows PowerShell 應用程式。在 PowerShell 提示中,下載並執行 Chrome 遠端桌面主機端安裝程式。
$installer = "$env:TEMP\chromeremotedesktophost.msi" $uri = 'https://dl.google.com/edgedl/chrome-remote-desktop/chromeremotedesktophost.msi' (New-Object Net.WebClient).DownloadFile($uri,"$installer") Start-Process $installer -Wait Remove-Item $installer
出現提示時,請確認您要安裝程式進行變更。
設定 Chrome 遠端桌面服務
您現在可以產生 Windows 指令,啟動 Chrome 遠端桌面服務並將其連結至 Google 帳戶。
在本機電腦上使用 Chrome 瀏覽器,前往 Chrome 遠端桌面指令列設定頁面。
如果您尚未登入,請使用 Google 帳戶登入。這是將會用於授權遠端存取權的帳戶。
在「Set up another computer」頁面上,依序按一下「Begin」和「Next」。
按一下 [授權]。
您需要允許 Chrome 遠端桌面存取您的帳戶。如果您允許,頁面會顯示多個指令列,其中一個是 Windows (Powershell) 的指令列,如下所示:
& "${Env:PROGRAMFILES(X86)}\Google\Chrome Remote Desktop\CurrentVersion\remoting_start_host.exe" ` --code="4/ENCODED_AUTHENTICATION_TOKEN" ` --redirect-url="https://remotedesktop.google.com/_/oauthredirect" ` --name=$Env:COMPUTERNAME
按一下「複製」圖示 content_copy,將指令列複製到剪貼簿。
在 RDP 工作階段的 Powershell 提示中,貼上您剛才複製的指令列,然後按下 Enter 鍵。
出現提示時,請確認您要讓應用程式進行變更。
當您看到提示時,請輸入 6 位數的 PIN 碼。這個數字會在您之後連線時用於進一步的授權。
指令執行完畢後,遠端桌面服務就會啟動。
關閉 Powershell 視窗。
關閉 RDP 工作階段。
您現在可以使用 Chrome 遠端桌面連線至 VM。
非互動式安裝
在這種方法中,您會設定 VM 執行個體,讓 VM 建立時執行啟動指令碼。
採用這種做法時,VM 不需要能夠直接從網際網路存取,但仍需要能夠存取網際網路。
授權 Chrome 遠端桌面服務
您現在可以產生 Windows 指令,稍後在 specialize 指令碼中使用。在這個程序中,您需要提供指令中包含的授權資訊。
在本機電腦上使用 Chrome 瀏覽器,前往 Chrome 遠端桌面指令列設定頁面。
如果您尚未登入,請使用 Google 帳戶登入。這是將會用於授權遠端存取權的帳戶。
依序點選「開始」和「下一步」。
按一下 [授權]。
允許 Chrome 遠端桌面存取您的帳戶。
頁面現在包含多個指令列,其中一個是 Windows (Cmd) 的指令列,如下所示:
"%PROGRAMFILES(X86)%\Google\Chrome Remote Desktop\CurrentVersion\remoting_start_host.exe" --code="4/ENCODED_AUTHENTICATION_TOKEN" --redirect-url="https://remotedesktop.google.com/_/oauthredirect" --name=%COMPUTERNAME%
--code
旗標包含一個短效 OAuth 權杖。指令列中的授權碼有效期限只有幾分鐘的時間,而且只能使用一次。
請保持開啟這個頁面。
將啟動指令複製到 Cloud Shell
下一步是在 Cloud Shell 執行個體中建立檔案,其中包含您剛才產生的啟動指令。
開啟 Cloud Shell。
建立開機指令的檔案:
cat > crd-auth-command.txt
前往含有 Chrome 遠端桌面啟動指令的頁面,然後複製 Windows (Cmd) 指令列。
在 Cloud Shell 中貼上指令,將其加入檔案。
按下 Enter 鍵結束指令列,然後按下 Control-D 鍵關閉檔案。
建立開機指令碼
複製下列程式碼區塊,然後貼到 Cloud Shell。
cat << "EOF" > crd-sysprep-script.ps1 <# .SYNOPSIS GCESysprep specialize script for unattended Chrome Remote Desktop installation. #> $ErrorActionPreference = 'stop' function Get-Metadata([String]$metadataName) { try { $value = (Invoke-RestMethod ` -Headers @{'Metadata-Flavor' = 'Google'} ` -Uri "http://metadata.google.internal/computeMetadata/v1/instance/attributes/$metadataName") } catch { # Report but ignore REST errors. Write-Host $_ } if ($value -eq $null -or $value.Length -eq 0) { throw "Metadata value for ""$metadataName"" not specified. Skipping Chrome Remote Desktop service installation." } return $value } # Get config from metadata # $crdCommand = Get-Metadata('crd-command') $crdPin = Get-Metadata('crd-pin') $crdName = Get-Metadata('crd-name') if ($crdPin -isNot [Int32] -or $crdPin -gt 999999 -or $crdPin -lt 0) { throw "Metadata ""crd-pin""=""$crdPin"" is not a 6 digit number. Skipping Chrome Remote Desktop service installation." } # Prefix $crdPin with zeros if required. $crdPin = $crdPin.ToString("000000"); # Extract the authentication code and redirect URL arguments from the # remote dekstop startup command line. # $crdCommandArgs = $crdCommand.Split(' ') $codeArg = $crdCommandArgs | Select-String -Pattern '--code="[^"]+"' $redirectArg = $crdCommandArgs | Select-String -Pattern '--redirect-url="[^"]+"' if (-not $codeArg) { throw 'Cannot get --code= parameter from crd-command. Skipping Chrome Remote Desktop service installation.' } if (-not $redirectArg) { throw 'Cannot get --redirect-url= parameter from crd-command. Skipping Chrome Remote Desktop service installation.' } Write-Host 'Downloading Chrome Remote Desktop.' $installer = "$env:TEMP\chromeremotedesktophost.msi" $uri = 'https://dl.google.com/edgedl/chrome-remote-desktop/chromeremotedesktophost.msi' (New-Object Net.WebClient).DownloadFile($uri,"$installer") Write-Host 'Installing Chrome Remote Desktop.' & msiexec.exe /I $installer /qn /quiet | Out-Default Remove-Item $installer Write-Host 'Starting Chrome Remote Desktop service.' & "${env:ProgramFiles(x86)}\Google\Chrome Remote Desktop\CurrentVersion\remoting_start_host.exe" ` $codeArg $redirectArg --name="$crdName" -pin="$crdPin" | Out-Default Write-Host 'Downloading Chrome.' $installer = "$env:TEMP\chrome_installer.exe" $uri = 'https://dl.google.com/chrome/install/latest/chrome_installer.exe' (New-Object Net.WebClient).DownloadFile($uri,"$installer") Write-Host 'Installing Chrome.' & $installer /silent /install | Out-Default Remove-Item $installer EOF
這個程式碼區塊是 PowerShell 指令碼,會在建立 VM 時執行。這個程式會執行下列動作:
- 下載並安裝 Chrome 遠端桌面主機服務。
- 擷取下列中繼資料參數:
crd-command
- Windows 驗證和啟動指令。crd-pin
:用於額外驗證的 6 位數 PIN 碼。crd-name
:這個例項的名稱。
- 設定及啟動 Chrome 遠端桌面主機服務。
- 下載並安裝 Chrome 瀏覽器。
建立新的 Windows 虛擬機器
您現在可以使用先前建立的檔案建立新的 Windows VM,以便設定 Chrome 遠端桌面。
在本教學課程中,我們會使用 e2-medium
機器類型。如果您要將這個環境用於自己的環境,建議您調整機器類型、名稱、地區、啟動磁碟大小或其他設定。
在 Cloud Shell 中設定偏好的區域:
ZONE=us-central1-b REGION=us-central1 gcloud config set compute/zone "${ZONE}"
設定 6 位數 PIN 碼,為 Chrome 遠端桌面提供額外驗證機制:
CRD_PIN=your-pin
將 your-pin 替換為 6 位數字。
為這個 VM 執行個體設定名稱:
INSTANCE_NAME=crdhost
建立執行個體:
gcloud compute instances create ${INSTANCE_NAME} \ --machine-type=e2-medium \ --scopes=cloud-platform \ --enable-display-device \ --image-family=windows-2022 \ --image-project=windows-cloud \ --boot-disk-size=50GB \ --boot-disk-device-name=${INSTANCE_NAME} \ --metadata=crd-pin=${CRD_PIN},crd-name=${INSTANCE_NAME} \ --metadata-from-file=crd-command=crd-auth-command.txt,sysprep-specialize-script-ps1=crd-sysprep-script.ps1
這個指令會在預設 VPC 中建立 Windows Server 2022 虛擬機器,其中包含已連結的顯示裝置 (Windows VM 上的 Chrome 遠端桌面需要此裝置) 和 50 GB 的啟動磁碟,並授予執行個體對 Google Cloud API 的完整存取權。
中繼資料值會指定專用指令碼、Windows 開機指令列,以及啟動 Chrome 遠端桌面服務所需的參數。
監控 VM 啟動
您可以查看在建立時記錄到 VM 序列埠的訊息,確認開機指令碼是否成功。
在 Cloud Shell 中,顯示 VM 啟動期間記錄的訊息:
gcloud compute instances tail-serial-port-output ${INSTANCE_NAME}
如果 Chrome 遠端桌面設定成功,您會看到下列記錄列:
Found sysprep-specialize-script-ps1 in metadata. sysprep-specialize-script-ps1: Downloading Chrome Remote Desktop. sysprep-specialize-script-ps1: Installing Chrome Remote Desktop. sysprep-specialize-script-ps1: Downloading Chrome. sysprep-specialize-script-ps1: Installing Chrome. sysprep-specialize-script-ps1: Starting Chrome Remote Desktop service. Finished running specialize scripts.
您也可能會看到下列行:
sysprep-specialize-script-ps1: ... Failed to read 'C:\ProgramData\Google\Chrome Remote Desktop\host_unprivileged.json'.: The system cannot find the path specified. (0x3)
這是正常現象,可以忽略。
如果啟動 Chrome 遠端桌面服務失敗,系統會顯示錯誤訊息,指出問題所在,例如:
sysprep-specialize-script-ps1: Couldn't start host: OAuth error.
這則錯誤訊息表示 Chrome 遠端桌面驗證頁面中的 OAuth 權杖已失效,可能是因為權杖已使用過,或是已過期。
如要修正這項錯誤,請使用 RDP 連線,然後按照前述方式執行互動式設定,或是刪除 VM 並重試設定程序。
當您在序列埠監控器中看到下列訊息時,表示 VM 已就緒。
GCEInstanceSetup: ------------------------------------------------------------ GCEInstanceSetup: Instance setup finished. crdhost is ready to use. GCEInstanceSetup: ------------------------------------------------------------
按下 Control-C 即可停止顯示啟動訊息。
建立 Windows 使用者帳戶
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下執行個體名稱
crdhost
,開啟「VM 執行個體詳細資料」頁面。在「遠端存取」下方,按一下「設定 Windows 密碼」,然後按一下「設定」,在遠端機器上建立帳戶。
系統會在這個步驟產生密碼,記下使用者名稱和密碼,或將其複製到安全的臨時檔案中。
使用 Chrome 遠端桌面連線至 VM 執行個體
您可以使用 Chrome 遠端桌面網頁應用程式連線至 VM 執行個體。
在本機電腦上,前往 Chrome 遠端桌面網站。
按一下「存取我的電腦」。
如果您尚未登入 Google,請使用您用於設定 Chrome 遠端桌面服務的 Google 帳戶登入。
您會在「Remote Devices」清單中看見新的 VM 執行個體
crdhost
。按一下遠端桌面執行個體的名稱。
出現系統提示時,輸入您之前建立的 PIN 碼,然後按一下箭頭 arrow_forward 按鈕進行連線。
您現在已連線至遠端 Compute Engine 執行個體上的 Windows 登入畫面。
如果系統顯示提示,請一律允許遠端桌面應用程式讀取剪貼簿,並讓您在本機和遠端應用程式之間複製及貼上內容。
按下任一鍵,然後輸入先前產生的 Windows 使用者密碼。請注意,預設的遠端鍵盤採用美式英文版面配置,因此輸入的字元可能與本地鍵盤上的字元不符。也無法複製及貼上密碼。
您現在已連線並登入遠端 Windows 桌面。
改善遠端桌面體驗
本節提供變更設定的操作說明,可協助您改善遠端桌面體驗。
安裝 Chrome 遠端桌面應用程式
Chrome 遠端桌面應用程式可提供獨立的視窗體驗,並允許在遠端系統上使用 Chrome 通常會攔截的鍵盤快速鍵。
如果未安裝這個應用程式,請按照下列步驟操作:
- 將滑鼠移至視窗側邊,然後使用 chevron_left 按鈕開啟「工作階段選項」面板。
- 在「安裝應用程式」部分中,按一下「開始」。
- 按一下 [安裝]。
遠端桌面工作階段會在自己的應用程式視窗中重新開啟。
只要在網址列中按一下「Open With」open_in_new 圖示,即可將任何遠端桌面工作階段從 Chrome 分頁移至應用程式視窗。
改善螢幕解析度
您可以修改預設的遠端桌面解析度,以便更符合本機電腦的桌面解析度。
- 在遠端桌面的背景上按一下滑鼠右鍵,然後選取「顯示設定」。
- 在「解析度」下拉式清單中,選取其他螢幕解析度。
- 在對話方塊中確認新的螢幕解析度。
重新啟用服務
如果您在用戶端應用程式中錯誤地停用了遠端執行個體的連線,可以重新設定服務並按照「設定 Chrome 遠端桌面服務」中的操作說明重新啟用連線。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除 Compute Engine 執行個體
您不必刪除完整專案,而是可以刪除針對本教學課程建立的 VM 執行個體:
找出您之前建立的執行個體名稱 (
crdhost
),並選取旁邊的核取方塊。點選頁面頂端的 [Delete] (刪除) 按鈕。
刪除執行個體需要一些時間。
取消執行個體的 Chrome 遠端桌面授權
如果您不想再連線至 VM 執行個體,可以將其停用,並從「Remote Devices」清單中移除該執行個體。
- 在本機電腦上,前往 Chrome 遠端桌面遠端裝置清單網站。
- 按一下執行個體名稱
crdhost
旁的 。 - 按一下 [OK] 以確認停用該遠端裝置連線。
後續步驟
- 瞭解如何在 Linux 虛擬機器上設定 Chrome 遠端桌面。
- 瞭解建立虛擬工作站的其他選項。
- 探索 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽我們的雲端架構中心。