Ubuntu系统下pgAdmin数据加密配置指南
数据加密是pgAdmin安全体系的核心环节,主要涵盖传输层加密(SSL/TLS)与配置文件/数据加密两部分,以下是具体实施步骤:
传输层加密可防止数据在pgAdmin与PostgreSQL服务器、pgAdmin与客户端之间被窃取或篡改,需分别配置pgAdmin服务端与PostgreSQL服务器的SSL。
生成SSL证书与私钥:使用OpenSSL工具生成自签名证书(生产环境建议使用CA颁发的有效证书)。执行以下命令:
sudo openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout /etc/pgadmin4/server.key -out /etc/pgadmin4/server.crt
命令说明:-newkey rsa:2048
生成2048位的RSA私钥,-x509
表示生成自签名证书,-days 3650
设置证书有效期为10年,server.key
为私钥文件,server.crt
为证书文件。
配置pgAdmin读取证书:编辑pgAdmin本地配置文件(Ubuntu默认路径为/etc/pgadmin4/config_local.py
),添加SSL证书路径:
SSL_CERTFILE = '/etc/pgadmin4/server.crt' # 证书文件路径
SSL_KEYFILE = '/etc/pgadmin4/server.key' # 私钥文件路径
设置文件权限:确保证书与私钥仅pgAdmin用户可访问,避免敏感信息泄露:
sudo chown root:pgadmin /etc/pgadmin4/server.key /etc/pgadmin4/server.crt
sudo chmod 640 /etc/pgadmin4/server.key /etc/pgadmin4/server.crt
重启pgAdmin服务:使配置生效:
sudo systemctl restart pgadmin4
验证SSL连接:通过浏览器访问https://<服务器IP>:5051
(默认端口为5051,若修改过需替换),确认地址栏显示锁图标(表示HTTPS加密连接成功)。
pgAdmin连接PostgreSQL时需启用SSL,需修改PostgreSQL的配置文件:
修改postgresql.conf
:编辑PostgreSQL主配置文件(路径通常为/etc/postgresql/<版本>/main/postgresql.conf
),开启SSL并指定证书路径:
ssl = on # 开启SSL
ssl_cert_file = '/etc/postgresql/<版本>/main/server.crt' # 证书文件路径
ssl_key_file = '/etc/postgresql/<版本>/main/server.key' # 私钥文件路径
修改pg_hba.conf
:编辑客户端认证文件(路径通常为/etc/postgresql/<版本>/main/pg_hba.conf
),强制要求SSL连接(针对远程客户端):
hostssl all all 0.0.0.0/0 md5 # 所有远程客户端需通过SSL连接,认证方式为md5
重启PostgreSQL服务:应用配置:
sudo systemctl restart postgresql
pgAdmin中配置SSL连接:在pgAdmin创建PostgreSQL连接时,勾选**“Use SSL”选项,并选择“Certificate”**模式(若使用自签名证书,需提前将证书导入pgAdmin信任库)。
pgAdmin的配置文件(如config_local.py
)包含数据库连接信息、认证凭证等敏感数据,需通过加密或权限控制保护。
可使用GnuPG(GPG)工具加密config_local.py
文件(需提前安装GPG):
gpg -c /etc/pgadmin4/config_local.py # 生成加密文件config_local.py.gpg
sudo mv /etc/pgadmin4/config_local.py.gpg /etc/pgadmin4/config_local.py # 替换原文件
使用时需解密(输入密码):
gpg -d /etc/pgadmin4/config_local.py > /tmp/config_local.py.tmp && sudo mv /tmp/config_local.py.tmp /etc/pgadmin4/config_local.py
注意:此方法会增加每次修改配置的复杂度,建议仅在极端安全场景下使用。
确保pgAdmin配置目录及文件的权限仅允许必要用户访问:
sudo chown -R root:pgadmin /etc/pgadmin4/ # 所有者为root,所属组为pgadmin
sudo chmod -R 750 /etc/pgadmin4/ # 目录权限750(所有者可读写执行,组可读执行,其他无权限)
通过Ubuntu防火墙(ufw)限制pgAdmin端口的访问,仅允许特定IP地址或网段访问:
sudo ufw allow from <允许的IP地址> to any port 5051 proto tcp # 例如允许公司IP访问
sudo ufw enable # 开启防火墙
log_statement = 'all'
、log_connections = on
),监控异常访问行为。