Ubuntu系统下pgAdmin防火墙配置指南(基于UFW)
pgAdmin作为PostgreSQL的图形化管理工具,默认通过5050端口(Web界面)与用户交互,而PostgreSQL数据库本身默认使用5432端口(TCP)。在Ubuntu系统中,推荐使用**UFW(Uncomplicated Firewall)**配置防火墙规则,因其操作简单且适合新手。以下是详细步骤:
若系统未安装UFW,需先通过以下命令安装:
sudo apt update
sudo apt install ufw
安装完成后,启用UFW以激活防火墙:
sudo ufw enable
启用后,系统会提示“Command may disrupt existing ssh connections”,输入y确认(若需通过SSH远程管理,需提前允许SSH端口,见步骤3)。
pgAdmin的Web服务默认监听5050端口(TCP),需允许该端口的入站流量:
sudo ufw allow 5050/tcp
若将pgAdmin配置为其他端口(如5433),需将命令中的5050替换为实际端口。
pgAdmin需连接PostgreSQL数据库,因此需开放数据库端口:
sudo ufw allow 5432/tcp
若PostgreSQL配置为仅本地访问(listen_addresses = 'localhost'),此步骤可省略;若需远程访问,需确保PostgreSQL的postgresql.conf文件中listen_addresses设置为'*',并修改pg_hba.conf允许远程IP连接。
为避免未授权访问,建议仅允许特定IP地址或IP段访问pgAdmin。例如,允许公司办公IP(192.168.1.100)访问5050端口:
sudo ufw allow from 192.168.1.100 to any port 5050
若需允许多个IP,可重复执行此命令。
配置完成后,通过以下命令验证规则是否生效:
sudo ufw status verbose
输出应显示5050/tcp和5432/tcp端口为“ALLOW IN”,示例如下:
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip
To Action From
-- ------ ----
5050/tcp ALLOW IN Anywhere
5432/tcp ALLOW IN Anywhere
5050/tcp (v6) ALLOW IN Anywhere (v6)
5432/tcp (v6) ALLOW IN Anywhere (v6)
UFW规则默认会自动保存,无需手动操作。若需手动保存(如需确保重启后规则不丢失),可执行:
sudo ufw reload
此命令会重新加载防火墙规则,使更改立即生效。
5050替换为映射后的端口(如443),并配置SSL证书(如Let’s Encrypt)强制HTTPS访问。sudo systemctl start pgadmin4、sudo systemctl start postgresql)。sudo ufw status检查规则是否正确,或通过journalctl -u pgadmin4查看pgAdmin日志排查问题。