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