最後活躍 8 months ago

從專案切換、建立靜態 IP,到 VM 外部 IP 更換與 SSH 連線,全套 gcloud CLI 操作全收錄,超實用筆記!🛠️

修訂 d849ebc358360530dfd08204f201fffbb0e05ad2

google_cloud_sdk_compute_operations_script.sh 原始檔案
1# 設定當前使用的 GCP 專案
2
3gcloud config set project <YOUR_PROJECT_ID>
4gcloud config set project my-gcp-project-123456
5
6# 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
7
8gcloud compute instances list
9
10# 在 GCP 中建立一個靜態 IP
11
12gcloud compute addresses create <STATIC_IP_NAME> --region <REGION>
13gcloud compute addresses create my-static-ip --region us-central1
14
15# 查看目前專案中的所有靜態 IP
16
17gcloud compute addresses list
18
19# 刪除 VM "<YOUR_VM_NAME>" 目前的外部 IP 配置
20
21# ⚠️ 執行後 VM 會暫時失去外部連線,請確保有內部網路或 Cloud Shell 可用!
22
23gcloud compute instances delete-access-config <YOUR_VM_NAME> \
24 --zone <YOUR_VM_ZONE> \
25 --access-config-name "External NAT"
26
27gcloud compute instances delete-access-config my-vm-1 \
28 --zone us-central1-a \
29 --access-config-name "External NAT"
30
31# 為 VM "<YOUR_VM_NAME>" 設定新的外部 IP
32
33gcloud compute instances add-access-config <YOUR_VM_NAME> \
34 --zone <YOUR_VM_ZONE> \
35 --access-config-name "External NAT" \
36 --address <STATIC_IP>
37
38gcloud compute instances add-access-config my-vm-1 \
39 --zone us-central1-a \
40 --access-config-name "External NAT" \
41 --address 35.234.56.78
42
43# 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
44
45gcloud compute instances list
46
47# 透過 SSH 連線至 VM
48
49gcloud compute ssh <YOUR_USERNAME>@<YOUR_VM_NAME> --zone <YOUR_VM_ZONE>
50gcloud compute ssh timmy@my-vm-1 --zone us-central1-a
51
52# 更新 SSH 設定
53
54gcloud compute config-ssh
55
google_cloud_sdk_installation_script.sh 原始檔案
1# 用 Homebrew 安裝 Google Cloud SDK(適用 macOS)
2brew install google-cloud-sdk
3
4# 初始化 gcloud,進行登入與基本設定
5gcloud init
6
7# 設定當前使用的 GCP 專案(記得換成你的專案 ID)
8gcloud config set project [YOUR_PROJECT_ID]
9
10# 查看目前帳戶底下有哪些 GCP 專案
11gcloud projects list
12
13# 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
14gcloud compute instances list
15