以下是Debian PostgreSQL安全加固策略:
系統與軟件更新
定期執行 sudo apt update && sudo apt upgrade
,確保系統及PostgreSQL軟件包為最新版本,修補安全漏洞。
用戶與權限管理
postgres
用戶設置強密碼:sudo -u postgres psql -c "ALTER USER postgres PASSWORD '強密碼';"
。GRANT/REVOKE
命令精細化控制用戶對數據庫的訪問權限。配置文件優化
postgresql.conf
:
listen_addresses
為'*'
(允許遠程連接時需謹慎,建議限制為特定IP段)。max_connections
限制并發連接數,避免資源耗盡。pg_hba.conf
:
host all all 192.168.1.0/24 md5
。hostssl all all 0.0.0.0/0 md5
。啟用加密傳輸
mkdir /etc/postgresql/ssl
openssl req -new -x509 -days 365 -nodes -text -out /etc/postgresql/ssl/server.crt -keyout /etc/postgresql/ssl/server.key
chmod 600 /etc/postgresql/ssl/server.key
postgresql.conf
中啟用SSL:ssl on
ssl_cert_file '/etc/postgresql/ssl/server.crt'
ssl_key_file '/etc/postgresql/ssl/server.key'
防火墻與網絡隔離
sudo ufw allow from <可信IP> to any port 5432
。審計與日志監控
log_statement = 'all'
log_connections = true
log_disconnections = true
數據安全增強
pgcrypto
擴展加密存儲:CREATE EXTENSION IF NOT EXISTS pgcrypto;
。pg_dump
工具自動化備份流程。最小化服務暴露
uuid-ossp
(若無需生成UUID)。注:配置后需重啟PostgreSQL服務:sudo systemctl restart postgresql
。安全策略需根據實際業務場景調整,建議定期進行滲透測試與安全審計。