Last Updated on 2026-08-28 by william
Cockpit 是 Red Hat 主導開發的輕量級 Linux 網頁管理後台,搭配 cockpit-podman 模組,能讓我們直接透過瀏覽器直覺管理 Podman 容器、Pod、映像檔與系統資源。
本文記錄如何在 Rocky Linux 9 從零安裝並啟用 Podman 容器環境與 Cockpit 網頁管理介面,解決預設無法以 root 登入的問題,並透過 PAM 單獨為 Cockpit 啟用 Google Authenticator 雙因子驗證(2FA),兼顧便利與資安且不影響既有 SSH 連線。
1. 安裝與啟用 Podman 容器引擎
Rocky Linux 9 官方軟體庫已原生收錄 Podman,可直接安裝:
# 1. 安裝 Podman
sudo dnf install -y podman
# 2. 檢查版本以確認安裝成功
podman --version
# 3. 測試運行一個 Hello World 容器
podman run --rm hello-world
💡 補充(選用):若需要讓外部工具或遠端 API 連線,可啟動系統層級的 Podman Socket:
sudo systemctl enable --now podman.socket
2. 安裝 Cockpit 與 Podman 網頁管理模組
接著安裝 Cockpit 主程式與專門管理 Podman 的 Web 介面模組:
# 安裝 Cockpit 與 Cockpit-Podman 外掛模組
sudo dnf install -y cockpit cockpit-podman
# 啟用並啟動 Cockpit Socket(按需監聽,不佔常駐資源)
sudo systemctl enable --now cockpit.socket
# 開放防火牆連接埠 (預設 Port 9090)
sudo firewall-cmd --add-service=cockpit --permanent
sudo firewall-cmd --reload
3. 放行 root 帳號登入 Web 介面
Rocky Linux 9 預設會在黑名單中封鎖 root 登入。若需使用 root 帳號管理全系統容器,請修改黑名單設定檔:
sudo nano /etc/cockpit/disallowed-users
找到 root 這一行,將其刪除或加上 # 註解:
# List of users which are not allowed to login to Cockpit
# root
bin
daemon
存檔退出後重啟 Socket:
sudo systemctl restart cockpit.socket
4. 設定 Google Authenticator 雙因子驗證 (2FA)
為避免 Web 介面密碼遭暴力破解,建議搭配 TOTP 動態驗證碼加強防護。
步驟 A:安裝 PAM 模組
# 啟用 EPEL 套件庫並安裝相關工具
sudo dnf install -y epel-release
sudo dnf install -y google-authenticator qrencode
步驟 B:生成 2FA 金鑰與 QR Code
切換至要啟用 2FA 的帳號(例如 root)並執行:
google-authenticator
依提示進行安全配置:
Do you want authentication tokens to be time-based (y/n)➔y- (此時請使用手機 Google Authenticator App 掃描終端機顯示的 QR Code,並妥善備份 5 組 Emergency scratch codes)
Do you want me to update your "~/.google_authenticator" file? (y/n)➔yDo you want to disallow multiple uses of the same authentication token? (y/n)➔yBy default, a new token is generated every 30 seconds... (y/n)➔nDo you want to enable rate-limiting? (y/n)➔y
更新 SELinux 安全標籤:
restorecon -Fv ~/.google_authenticator
5. 設定 Cockpit PAM 規則(完全不影響 SSH)
Linux PAM 的服務配置各自獨立,此處只修改 Cockpit 規則,原本的 SSH 連線機制不會受到任何影響。
編輯 Cockpit PAM 設定檔:
sudo nano /etc/pam.d/cockpit
在 auth substack password-auth 下方加入 pam_google_authenticator.so:
#%PAM-1.0
auth substack password-auth
auth required pam_google_authenticator.so nullok
account include password-auth
password include password-auth
session include password-auth
說明:
nullok參數代表尚未綁定 2FA 的帳號仍可以純密碼登入。若日後所有帳號皆已完成綁定,可移除nullok強制全面 2FA。
6. 驗證成果與加碼安全技巧
- 開啟瀏覽器進入
https://<伺服器_IP>:9090。 - 輸入帳號與系統密碼後,系統會要求輸入手機 App 上的 6 位數 Verification code。
- 驗證通過後即可進入控制台,點選左側 「Podman 容器」 即可透過圖形介面輕鬆管理容器、映像檔與 Pod。
加碼技巧:僅允許特定內網網段連線 Cockpit
# 移除全域開放
sudo firewall-cmd --permanent --remove-service=cockpit
# 僅允許特定內網網段存取 (例: 192.168.1.0/24)
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="cockpit" accept'
# 重新載入防火牆規則
sudo firewall-cmd --reload

搶先發佈留言