s

ssh

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

0 0 0 3
1 # 設定當前使用的 GCP 專案
2 gcloud config set project <YOUR_PROJECT_ID>
3 gcloud config set project my-gcp-project-123456
4
5 # 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
6 gcloud compute instances list
7
8 # 在 GCP 中建立一個靜態 IP
9 gcloud compute addresses create <STATIC_IP_NAME> --region <REGION>
10 gcloud compute addresses create my-static-ip --region us-central1

此 SSH 設定根據目標 IP 或域名,透過不同的跳板機 (ProxyCommand) 進行連線,確保內外部網路存取的靈活性與自動化。

0 0 0 1
1 Host *.internal.local
2 ProxyCommand bash -c 'timeout 3 nc -z %h %p || ssh -W %h:%p jump.internal.example.com'
3
4 Host *
5 ProxyCommand bash -c 'timeout 3 nc -z %h %p || ssh -W %h:%p jump.external.example.com'
6
7 Host 192.168.0.*
8 ProxyCommand bash -c 'timeout 3 nc -z %h %p || ssh -W %h:%p jump1.example.com'
9
10 Host 172.16.*.*

這個 iptables 防火牆腳本 用於 強化伺服器的網路安全,透過 允許合法流量(如 SSH、HTTP/HTTPS)並封鎖未授權連線,同時防禦各種攻擊(如 暴力破解、SYN Flood、埠掃描、DoS)。此外,它會 記錄異常流量 以便後續分析,並將規則儲存至 /etc/iptables/rules.v4,確保設定在重啟後仍生效。適用於 企業伺服器、內部網路防護及個人伺服器安全強化。

0 0 0 1
1 #!/bin/bash
2
3 # === Basic Settings ===
4
5 # Clear all existing rules
6 iptables -F
7 iptables -X
8 iptables -Z
9
10 # Set default policies: drop all incoming and forwarding traffic, allow outgoing

這個 Bash 腳本用於 清理 /tmp 目錄中無效的 SSH 相關暫存檔案。它會搜尋所有以 ssh- 開頭的檔案或目錄,使用 lsof 檢查是否有行程正在使用它們,若未被使用則刪除,以確保 /tmp 目錄保持乾淨。這適用於 系統維護、提升伺服器安全性,防止長時間未使用的 SSH 暫存文件佔用空間。

0 0 0 1
1 #!/bin/bash
2
3 # 定義要檢查的目錄
4 TMP_DIR="/tmp"
5
6 # 查找所有以 ssh- 開頭的文件或目錄
7 for file in "$TMP_DIR"/ssh-*; do
8 # 如果沒有匹配的文件,跳過
9 if [ ! -e "$file" ]; then
10 continue