Jump to content
View in the app

A better way to browse. Learn more.

PHP论坛人

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

SSH登入提示 Part 2 安裝MaxMind GeoLite2並建立自動更新腳本

Featured Replies

SSH登入提示 Part 2 安裝MaxMind GeoLite2並建立自動更新腳本



-------------
前言
-------------

GeoLite2是MaxMind提供的免費IP地理位置資料庫,在論壇伺服器環境中應用廣泛:

安全分析
識別異常登入來源國家,配合防火牆封鎖高風險區域


流量統計
分析訪客地理分佈,生成 國家/城市 統計報表


合規政策
依地區限制內容存取,滿足GDPR等法規需求


Fail2ban整合
攻擊來源國家分析,強化封鎖策略


MOTD 顯示
即時顯示攻擊來源國家 (本教學的核心用途)



-------------------------------------------
註冊MaxMind帳號與取得License Key
-------------------------------------------

自2019年12月起,在MaxMind下載GeoLite2必須使用License Key

註冊帳號:前往官網 https://www.maxmind.com/en/geolite2/signup 註冊頁面,申請免費的GeoLite2資料庫帳號

填寫並提交註冊信息:在頁面上,你需要提供一個有效且真實的郵箱地址,這將是你的用戶名。然後設置一個密碼,並填寫其他必要的資料來完成註冊

驗證郵箱地址:提交註冊信息後,MaxMind 會向你填寫的郵箱發送一封驗證郵件。你需要登入郵箱,點擊郵件中的驗證連結來啟用帳號

取得金鑰:登入網站後,前往「My Account」>「Manage License Keys」。點擊 Generate New License Key。取得 License Key



注意事項:

註冊時,若系統提示使用「一次性郵箱」或懷疑VPN,請先關閉所有VPN或代理後重試。申請時的IP若為資料中心IP,也可能觸發限制,建議使用一般家用網路申請





-------------------------
回到 Debian 13 伺服器
-------------------------

SSH登入你的VPS




----------------
安裝必要工具
----------------

apt update


apt install -y curl tar gzip


apt install -y mmdb-bin libmaxminddb0 libmaxminddb-dev





-----------------------------
建立 存放目錄 與 腳本
-----------------------------

建立目錄結構
mkdir -p /usr/share/GeoIP



建立自動更新腳本
vi /usr/local/bin/update_geoip.sh



貼上以下內容,請務必將 YOUR_LICENSE_KEY 替換為你的 License Key




#!/bin/bash
# MaxMind GeoLite2 資料庫下載腳本
# 適用於 Debian 13

# --- 配置區域 ---
# 請在此填入你的 MaxMind License Key 授權金鑰
LICENSE_KEY="YOUR_LICENSE_KEY"


# 下載基礎 URL
BASE_URL="https://download.maxmind.com/app/geoip_download"


# 目標目錄
GEOIP_DIR="/usr/share/GeoIP"

# 定義要下載的資料庫版本 (edition_id)
EDITIONS=(
    "GeoLite2-ASN"
    "GeoLite2-City"
    "GeoLite2-Country"
)

# --- 腳本本體 ---
echo "$(date): 開始更新 MaxMind GeoLite2 資料庫..."

# 確保目標目錄存在
mkdir -p "$GEOIP_DIR"

# 切換到目標目錄
cd "$GEOIP_DIR" || exit 1

for EDITION in "${EDITIONS[@]}"; do
    echo "正在處理: $EDITION"
    
    # 下載最新的 .mmdb 檔案 (使用 -s 靜默模式,-L 跟隨重定向,-O 指定輸出檔案)
    # 使用 curl 而非 wget,因為它在 Debian 13 上預設安裝且功能完備
    sudo curl -s -L -o "${EDITION}.tar.gz" \
        "${BASE_URL}?edition_id=${EDITION}&license_key=${LICENSE_KEY}&suffix=tar.gz"
    
    # 檢查下載是否成功
    if [ $? -ne 0 ] || [ ! -s "${EDITION}.tar.gz" ]; then
        echo "錯誤:下載 $EDITION 失敗" >&2
        continue
    fi
    
    # 解壓縮,並直接將 .mmdb 檔案提取到目前目錄,覆蓋舊檔案
    # --strip-components=1 會移除壓縮包內的第一層目錄 (例如 GeoLite2-City_20250101)
    sudo tar -xzf "${EDITION}.tar.gz" \
        --strip-components=1 \
        --wildcards '*.mmdb'
    
    # 檢查解壓縮是否成功
    if [ $? -ne 0 ]; then
        echo "錯誤:解壓縮 $EDITION 失敗" >&2
    else
        echo "$EDITION 更新完成"
    fi
    
    # 清理下載的壓縮檔
    sudo rm -f "${EDITION}.tar.gz"
    
    echo "------------------------"
done

# 設定正確的權限,讓 Web 服務 (如 PHP-FPM 執行的使用者) 可以讀取
# 假設你的 PHP-FPM 執行使用者是 www-data,Nginx 也是 www-data
sudo chown -R www-data:www-data "$GEOIP_DIR"
sudo chmod 750 "$GEOIP_DIR"
sudo find "$GEOIP_DIR" -type f -name '*.mmdb' -exec sudo chmod 640 {} \;

echo "$(date): GeoLite2 資料庫更新程序結束。"






儲存檔案並離開vi編輯器
按 Esc,輸入 :wq,按 Enter






----------------------
賦予腳本執行權限
----------------------

chmod +x /usr/local/bin/update_geoip.sh


確認所有權
chown root:root /usr/local/bin/update_geoip.sh





------------------
檢查是否成功
------------------


手動執行一次測試
/usr/local/bin/update_geoip.sh


檢查是否成功
ls -lh /usr/share/GeoIP/


預期輸出 (檔案大小約 數MB 至 數十MB)
GeoLite2-ASN.mmdb
GeoLite2-City.mmdb
GeoLite2-Country.mmdb


常見錯誤:若下載後執行MOTD腳本時出現「Permission denied」,通常是因為MOTD腳本以root執行,但資料庫權限設為 640 (只有 www-data 可讀)


解決方式:將 root 加入 www-data 群組,或暫時改為 644 (視安全需求決定)


方法一:將 root 加入 www-data 群組
usermod -aG www-data root

需重新登入才生效



方法二:放寬權限 (較不安全,不建議用於生產環境)
chmod 644 /usr/share/GeoIP/*.mmdb






-----------------------------
設定自動更新服務
-----------------------------

這種方式不修改官方 geoipupdate.service,而是建立相依服務,讓兩者在開機時依序執行


1. 建立自訂 Service 單元
vi /etc/systemd/system/update-geoip-custom.service


貼上內容


[Unit]
Description=Custom GeoIP Update Script
Before=geoipupdate.service
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/update_geoip.sh
User=root
Group=root
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=geoipupdate.service




儲存檔案並離開vi編輯器
按 Esc,輸入 :wq,按 Enter




2. 建立關聯關係

建立軟連結,讓 geoipupdate.service 啟動時一併觸發

mkdir -p /etc/systemd/system/geoipupdate.service.wants/

ln -sf /etc/systemd/system/update-geoip-custom.service /etc/systemd/system/geoipupdate.service.wants/




3. 重新載入並測試

systemctl daemon-reload

systemctl restart geoipupdate


現在執行 systemctl restart geoipupdate 時,systemd 會依序:

先執行 update-geoip-custom.service (你的自訂腳本)

再執行 geoipupdate.service (MaxMind官方更新程序)




----------------
驗證資料庫
----------------


查詢國家:
mmdblookup --file /usr/share/GeoIP/GeoLite2-Country.mmdb --ip 8.8.8.8



查詢城市:
mmdblookup --file /usr/share/GeoIP/GeoLite2-City.mmdb --ip 8.8.8.8


查詢 ASN:
mmdblookup --file /usr/share/GeoIP/GeoLite2-ASN.mmdb --ip 8.8.8.8


若顯示 "United States"、"Mountain View"、"AS15169" 等資訊,代表資料庫運作正常





---------------------------
檢查服務狀態
---------------------------

systemctl status geoipupdate.service



systemctl status geoipupdate.timer



systemctl status update-geoip-custom.service




-----------------------
驗證SSH登入效果
-----------------------

登出
exit


重新SSH登入,確認動態MOTD正常顯示








--------------------
常見問題排查
--------------------


MOTD登入後完全不顯示

1. 確認腳本有執行權限
ls -la /etc/update-motd.d/10-system-info


2. 直接執行腳本,看是否有錯誤輸出
bash /etc/update-motd.d/10-system-info


3. 確認 PAM 設定
grep -n motd /etc/pam.d/sshd


4. 確認 SSH 設定
sshd -T | grep printmotd






MOTD顯示但顏色全部消失

部分 SSH 客戶端(如舊版 PuTTY)預設不啟用 ANSI 色彩。在客戶端開啟 ANSI / xterm-256color 終端機模擬即可







GeoIP 查詢顯示 Permission denied

確認資料庫擁有者與權限
ls -lah /usr/share/GeoIP/

臨時解法:允許 root 讀取
usermod -aG www-data root

重新登入後生效





sysstat 磁碟 IO 顯示空白

確認已啟用
grep ENABLED /etc/default/sysstat

應顯示:ENABLED="true"


確認服務運行
systemctl status sysstat


等待至少10分鐘讓 sysstat 收集資料後再次查看




SSH登入速度明顯變慢 (超過3 秒)

原因通常是腳本中的 sleep 1 (QPS計算) 加上GeoIP查詢。調整方式:

減少 GeoIP 分析的日誌行數(在腳本中從 200 改為 50)
tail -n 50 "$_LOG"

或完全停用 GeoIP 分析(將對應程式碼區塊註解掉)





------------------------
結語與後續擴展建議
------------------------

透過妥善規劃的動態MOTD,管理員在每次SSH登入時即可立刻掌握伺服器的健康狀態

對於資源受限的 2 GB VPS 而言,在問題影響論壇之前提早察覺記憶體或磁碟的異常,是非常實際的維運價值



後續可考慮的擴展方向:


更完整的即時監控
Netdata (輕量,適合 2 GB VPS)


指標長期儲存與警報
Prometheus + Grafana


日誌分析自動化
GoAccess (Nginx 日誌即時分析)


多伺服器集中管理
Zabbix、Grafana Alloy


自動封鎖惡意來源國
Nginx GeoIP 模組 + MaxMind

Edited by Jack

Create an account or sign in to comment

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.