開機指令碼是指在虛擬機器 (VM) 執行個體的啟動程序中執行工作時使用的檔案。開機指令碼可套用至專案中的所有 VM,或套用至單一 VM。由 VM 層級中繼資料指定的開機指令碼會覆寫由專案層級中繼資料指定的開機指令碼,且開機指令碼只會在網路可用時執行。本文件說明如何在 Linux VM 執行個體上使用啟動指令碼。如要瞭解如何新增專案層級的啟動指令碼,請參閱 gcloud compute project-info add-metadata
。
針對 Linux 啟動指令碼,您可以使用 bash 或非 bash 檔案。如要使用非 Bash 檔案,請在檔案頂端新增 #!
來指定解譯器。舉例來說,如要使用 Python 3 啟動指令碼,請在檔案頂端新增 #! /usr/bin/python3
。
如果您使用本文件中的其中一個程序指定開機指令碼,Compute Engine 會執行以下操作:
將開機指令碼複製到 VM
設定開機指令碼的執行權限
在 VM 啟動時,以
root
使用者身分執行開機指令碼
如要瞭解與啟動指令碼相關的各種工作,以及執行每項工作的時機,請參閱總覽。
事前準備
- 閱讀開機指令碼總覽。
- 瞭解中繼資料伺服器。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 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.
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.
-
Linux 開機指令碼的中繼資料鍵
開機指令碼會從中繼資料鍵指定的位置傳遞至 VM。中繼資料鍵會指定開機指令碼是儲存在本機、儲存在 Cloud Storage 中,還是直接傳遞至 VM。您使用的中繼資料鍵也可能取決於開機指令碼的大小。
下表列出可用於 Linux 啟動指令碼的中繼資料鍵,並提供資訊,說明應根據開機指令碼的儲存位置和大小使用哪個鍵。
中繼資料鍵 | 用於 |
---|---|
startup-script |
傳遞儲存在本機或直接新增的 bash 或非 bash 開機指令碼,且大小不超過 256 KB |
startup-script-url |
傳遞儲存在 Cloud Storage 中且大小超過 256 KB 的 bash 或非 bash 開機指令碼。
您在這裡輸入的字串會原封不動地用於執行 gcloud storage 。如果 startup-script-url 包含空格字元,請勿將空格替換成 %20 ,也不要在 startup-script-url 字串中加入雙引號 ("" )。 |
Linux 開機指令碼的執行順序
您可以使用多個開機指令碼。儲存在本機或直接新增的開機指令碼,會在儲存在 Cloud Storage 的開機指令碼之前執行。下表根據中繼資料鍵,顯示 Linux 啟動指令碼的執行順序。
中繼資料鍵 | 執行順序 |
---|---|
startup-script |
第一次開機後的每次開機 |
startup-script-url |
在初始開機後的每次開機期間執行 |
直接傳遞 Linux 開機指令碼
建立 VM 時,您可以直接將開機指令碼的內容新增至 VM。以下程序說明如何使用開機指令碼建立 VM,以便安裝 Apache 並建立基本網頁。
主控台
直接將 Linux 開機指令碼傳遞至新的 VM
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
如要使用 Linux 作業系統,請按照下列步驟操作:
在導覽選單中,按一下「OS 和儲存空間」。
按一下 [變更]。
在隨即顯示的「Boot disk」(開機磁碟) 窗格中,選取 Linux 作業系統。
如要直接新增 Linux 啟動指令碼,請按照下列步驟操作:
在導覽選單中,按一下「進階」。
在「Automation」(自動化) 專區的「Startup script」(開機指令碼) 欄位中,輸入以下內容:
#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Linux startup script added directly.</p></body></html> EOF
選用:指定其他設定選項。詳情請參閱「建立執行個體時的設定選項」。
如要建立並啟動執行個體,請按一下「建立」。
直接將 Linux 開機指令碼傳遞至現有 VM
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下執行個體的「名稱」。
按一下 [編輯]。
在「自動」下方新增開機指令碼內容。
驗證開機指令碼
執行個體啟動後,請在網路瀏覽器中查看外部 IP,確認啟動指令碼是否已建立網站。您可能需要等待約 1 分鐘,等待範例啟動指令碼完成。
gcloud
直接將 Linux 開機指令碼傳遞至新的 VM
使用下列 gcloud compute instances create
指令,即可在建立 VM 時,將開機指令碼的內容直接傳遞給 VM。
gcloud compute instances create VM_NAME \ --image-project=debian-cloud \ --image-family=debian-10 \ --metadata=startup-script='#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Linux startup script added directly.</p></body></html> EOF'
將 VM_NAME 替換為 VM 名稱。
直接將 Linux 開機指令碼傳遞至現有 VM
使用下列 gcloud compute instances add-metadata
指令,即可直接將開機指令碼新增至現有的 VM:
gcloud compute instances add-metadata VM_NAME \ --zone=ZONE \ --metadata=startup-script='#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Linux startup script added directly.</p></body></html> EOF'
更改下列內容:
VM_NAME:VM 名稱
ZONE:VM 的可用區
驗證開機指令碼
VM 啟動後,請在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 1 分鐘,等待範例啟動指令碼完成。
REST
直接將 Linux 開機指令碼傳遞至新的 VM
建立開機指令碼時,請使用下列 instances.insert
方法,將開機指令碼內容直接傳遞至 VM。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { ... "networkInterfaces": [ { "accessConfigs": [ { "type": "ONE_TO_ONE_NAT" } ] } ], "metadata": { "items": [ { "key": "startup-script", "value": "#! /bin/bash\napt update\napt -y install apache2\ncat <<EOF > /var/www/html/index.html\n<html><body><p>Linux startup script added directly.</p></body></html>\nEOF" } ] }, ... }
更改下列內容:
PROJECT_ID:專案 ID
ZONE:建立 VM 的可用區
直接將 Linux 開機指令碼傳遞至現有 VM
使用
instances.get
方法取得 VM 的tags.fingerprint
值。GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID:專案 ID
ZONE:VM 的可用區
VM_NAME:VM 的可用區
在呼叫
instances.setMetadata
方法時,使用fingerprint
值,以及開機指令碼的中繼資料鍵和值,傳遞開機指令碼:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata { "fingerprint": FINGERPRINT, "items": [ { "key": "startup-script", "value": "#! /bin/bash\napt update\napt -y install apache2\ncat <<EOF > /var/www/html/index.html\n<html><body><p>Linux startup script added directly.</p></body></html>\nEOF" } ], ... }
更改下列內容:
PROJECT_ID:專案 ID
ZONE:VM 的可用區
VM_NAME:VM 的可用區
FINGERPRINT:使用
instances.get
方法取得的tags.fingerprint
值
驗證開機指令碼
VM 啟動後,請在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 1 分鐘,等待範例啟動指令碼完成。
從本機檔案傳遞 Linux 開機指令碼
您可以在工作站的本機檔案中儲存開機指令碼,並在建立 VM 時將本機檔案做為中繼資料傳遞給 VM。您無法使用 VM 上儲存的檔案做為開機指令碼。
將 Linux 啟動指令碼從本機檔案傳遞至 VM 前,請先執行下列操作:
建立本機檔案來儲存開機指令碼。
請注意 gcloud CLI 與啟動指令碼之間的相對路徑。
在檔案中新增下列開機指令碼:
#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Linux startup script from a local file.</p></body></html> EOF
gcloud
將 Linux 開機指令碼從本機檔案傳遞至新的 VM
使用 --metadata-from-file
標記搭配 gcloud compute instances create
指令建立 VM,並傳遞本機檔案的內容,以做為開機指令碼。
gcloud compute instances create VM_NAME \ --image-project=debian-cloud \ --image-family=debian-10 \ --metadata-from-file=startup-script=FILE_PATH
更改下列內容:
VM_NAME:VM 名稱
FILE_PATH:啟動指令碼檔案的相對路徑
將 Linux 啟動指令碼從本機檔案傳遞至現有 VM
使用下列 gcloud compute instances add-metadata
指令,從本機檔案將開機指令碼傳遞至現有的 VM:
gcloud compute instances add-metadata VM_NAME \ --zone=ZONE \ --metadata-from-file startup-script=FILE_PATH
更改下列內容:
VM_NAME:VM 名稱
ZONE:VM 的可用區
FILE_PATH:啟動指令碼檔案的相對路徑
驗證開機指令碼
在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 1 分鐘,等待範例啟動指令碼完成。
透過 Cloud Storage 傳遞 Linux 開機指令碼
您可以將開機指令碼儲存在 Cloud Storage 中,並在建立 VM 時將其傳遞給 VM。將開機指令碼新增至 Cloud Storage 後,您就能在建立 VM 時使用網址參照開機指令碼。
在從 Cloud Storage 值區新增開機指令碼之前,請先執行下列操作:
建立用於儲存開機指令碼的檔案。本範例使用 bash (
.sh
) 檔案。將下列內容新增至 bash 檔案,以便安裝 Apache 並建立簡單的網頁:
#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Linux startup script from Cloud Storage.</p></body></html> EOF
安全性影響
根據預設,專案擁有者和專案編輯者可以存取相同專案中的 Cloud Storage 檔案,除非有明確存取權控管機制禁止。
如果 Cloud Storage 值區或物件的安全性低於中繼資料,在修改啟動指令碼並重新啟動 VM 時,就有可能發生權限提升的風險。這是因為在 VM 重新啟動後,啟動指令碼會以
root
身分執行,然後可使用所附服務帳戶的權限存取其他資源。
限制
主控台
將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
如要使用 Linux 作業系統,請按照下列步驟操作:
在導覽選單中,按一下「OS 和儲存空間」。
在「Operating system and storage」(作業系統和儲存空間) 專區,點選「Change」(變更)。接著,選取 Linux 作業系統。
如要讓執行個體存取含有 Linux 開機指令碼的 Cloud Storage 值區,請執行下列操作:
如要透過指定 Cloud Storage 中的檔案來新增 Linux 開機指令碼,請按照下列步驟操作:
在導覽選單中,按一下「進階」。
在「中繼資料」專區中,按一下
「新增項目」。畫面上會顯示「Key」和「Value」欄位。在「Key」欄位中輸入
startup-script-url
。在「Value」欄位中,使用下列其中一種格式輸入開機指令碼檔案的 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
gcloud 儲存空間 URI:
gs://BUCKET/FILE
更改下列內容:
BUCKET:包含開機指令碼檔案的 bucket 名稱
FILE:開機指令碼檔案的名稱
- 已驗證的網址:
選用:指定其他設定選項。詳情請參閱「建立執行個體時的設定選項」。
如要建立並啟動執行個體,請按一下「建立」。
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有的 VM
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下執行個體的「名稱」。
按一下 [編輯]。
在「中繼資料」下方新增下列值:
鍵:
startup-script-url
值:開機指令碼檔案的 Cloud Storage 位置,格式如下:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
- 已驗證的網址:
驗證開機指令碼
在網路瀏覽器中查看外部 IP,確認開機指令碼已建立網站。您可能需要等待約 1 分鐘,等待範例啟動指令碼完成。
gcloud
將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM
建立 VM 時,請使用下列 gcloud compute instances create
指令,將儲存在 Cloud Storage 中的開機指令碼傳遞給 VM。如要設定 --scope
標記的值,請使用 storage-ro
,讓 VM 能夠存取 Cloud Storage。
gcloud compute instances create VM_NAME \ --image-project=debian-cloud \ --image-family=debian-10 \ --scopes=storage-ro \ --metadata=startup-script-url=CLOUD_STORAGE_URL
更改下列內容:
VM_NAME:VM 名稱。
CLOUD_STORAGE_URL:中繼資料值。使用下列格式,將其設為開機指令碼檔案的 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
- 已驗證的網址:
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM
使用下列 gcloud compute instances add-metadata
指令,將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM:
gcloud compute instances add-metadata VM_NAME \ --zone=ZONE \ --metadata startup-script-url=CLOUD_STORAGE_URL
更改下列內容:
VM_NAME:VM 名稱。
ZONE:VM 的可用區。
CLOUD_STORAGE_URL:中繼資料值。使用下列格式,將其設為開機指令碼檔案的 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
- 已驗證的網址:
驗證開機指令碼
在網路瀏覽器中查看外部 IP,確認開機指令碼已建立網站。您可能需要等待約 1 分鐘,等待範例啟動指令碼完成。
REST
將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM
請使用下列 instances.insert
方法,在建立 VM 時將儲存在 Cloud Storage 中的開機指令碼傳遞給 VM。在 scopes
欄位中新增 https://www.googleapis.com/auth/devstorage.read_only
,讓 VM 能夠存取 Cloud Storage。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { ... "networkInterfaces": [ { "accessConfigs": [ { "type": "ONE_TO_ONE_NAT" } ] } ], "serviceAccounts": [ { "email": "default", "scopes": [ "https://www.googleapis.com/auth/devstorage.read_only" ] } ], "metadata": { "items": [ { "key": "startup-script-url", "value": "CLOUD_STORAGE_URL" } ] }, ... }
更改下列內容:
PROJECT_ID:專案 ID。
ZONE:建立新 VM 的可用區。
CLOUD_STORAGE_URL:中繼資料值。使用下列格式,將其設為開機指令碼檔案的 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
- 已驗證的網址:
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM
使用
instances.get
方法取得 VM 的tags.fingerprint
值。GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID:專案 ID
ZONE:VM 的可用區
VM_NAME:VM 的可用區
在呼叫
instances.setMetadata
方法時,使用fingerprint
值,以及開機指令碼的中繼資料鍵和值,傳遞開機指令碼:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata { "fingerprint": FINGERPRINT, "items": [ { "key": "startup-script-url", "value": "CLOUD_STORAGE_URL" } ], ... }
更改下列內容:
PROJECT_ID:專案 ID。
ZONE:VM 的可用區。
VM_NAME:VM 的可用區。
FINGERPRINT:使用
instances.get
方法取得的tags.fingerprint
值。CLOUD_STORAGE_URL:中繼資料值。請使用下列格式之一,將其設為開機指令碼檔案的 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
- 已驗證的網址:
驗證開機指令碼
在網路瀏覽器中查看外部 IP,確認開機指令碼已建立網站。您可能需要等待約 1 分鐘,等待範例啟動指令碼完成。
透過 Linux 開機指令碼存取中繼資料
您可以在開機指令碼中存取中繼資料值。舉例來說,您可以為多個 VM 使用相同的指令碼,並將不同的中繼資料值傳遞至每個 VM,以便個別設定每個指令碼的參數。
如要透過開機指令碼存取自訂中繼資料值,請按照下列步驟操作:
建立開機指令碼,查詢中繼資料鍵的值。舉例來說,下列 bash 檔案 (
.sh
) 開機指令碼會查詢foo
中繼資料鍵的值。#! /bin/bash METADATA_VALUE=$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/foo -H "Metadata-Flavor: Google") apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Accessing metadata value of foo: $METADATA_VALUE</p></body></html> EOF
建立 VM 時,請使用下列
gcloud compute instances create
指令設定foo
中繼資料鍵的值。在本例中,開機指令碼會從本機檔案傳遞至 VM。gcloud
gcloud compute instances create VM_NAME \ --image-project=debian-cloud \ --image-family=debian-10 \ --metadata-from-file=startup-script=FILE_PATH \ --metadata=foo=bar
更改下列內容:
VM_NAME:VM 名稱
FILE_PATH:啟動指令碼檔案的相對路徑
如要進一步瞭解如何指定中繼資料鍵/值組合,請參閱「設定自訂中繼資料」。
在本機工作站中,透過網路瀏覽器查看外部 IP,確認啟動指令碼是否會輸出
foo
的值。您可能需要等待約 1 分鐘,等待範例啟動指令碼完成。
重新執行 Linux 開機指令碼
如要重新執行開機指令碼,請按照下列步驟操作:
執行下列指令:
sudo google_metadata_script_runner startup
查看 Linux 開機指令碼的輸出內容
您可以執行下列任一操作,查看 Linux 啟動指令碼的輸出內容:
連線至執行個體並執行下列指令:
sudo journalctl -u google-startup-scripts.service
在 Google Cloud 控制台中透過序列埠 1 查看輸出內容,並檢查
google_metadata_script_runner
事件。