PostgreSQL disable pg_hba.conf trust auth method

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介:
在PostgreSQL中有一项认证为trust认证, 使用该认证方法不需要输入密码即可登录数据库.
也就是说数据库服务器管理员只要有操作系统root权限, 很容易就可以登录数据库查询数据.
在比较高的安全场合, 可能需要禁止掉trust方法. 这样的话即使配置了trust认证方法也需要提供密码.
方法很简单, 只要修改一下hba.c即可. 如下.
vi src/backend/libpq/hba.c
        if (strcmp(token->string, "trust") == 0)
                //parsedline->auth_method = uaTrust;
                parsedline->auth_method = uaMD5;
        else if (strcmp(token->string, "ident") == 0)
                parsedline->auth_method = uaIdent;
        else if (strcmp(token->string, "peer") == 0)
                parsedline->auth_method = uaPeer;
        else if (strcmp(token->string, "password") == 0)
                //parsedline->auth_method = uaPassword;
                parsedline->auth_method = uaMD5;
        else if (strcmp(token->string, "krb5") == 0)

修改后重新编译一下postgresql软件.
修改后在pg_hba.conf中配置trust和password认证方法和使用md5效果一样 . 都需要使用密码. 否则无法登陆数据库
[root@db-192-168-9-35 libpq]# su - pg93
pg93@db-192-168-9-35-> cd $PGDATA
pg93@db-192-168-9-35-> cat pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust

都是trust的, 但是实际连接数据库时会提示输入密码.
pg93@db-192-168-9-35-> psql
Password: 
psql (9.3beta1)
Type "help" for help.
postgres=# 


使用这种方法后, 可以规避本地不需要密码校验的漏洞.
但是这个方法并不彻底, 因为如果你有本地操作系统root权限, 你可以重新编译一个正常的postgresql软件. 
重启数据库即可, 这样的话又可以配置trust了.
[root@db-192-168-9-35 postgresql-9.3beta1]# vi /opt/soft_bak/postgresql-9.3beta1/src/backend/libpq/hba.c
        if (strcmp(token->string, "trust") == 0)
                parsedline->auth_method = uaTrust;
                //parsedline->auth_method = uaMD5;
        else if (strcmp(token->string, "ident") == 0)
                parsedline->auth_method = uaIdent;
        else if (strcmp(token->string, "peer") == 0)
                parsedline->auth_method = uaPeer;
        else if (strcmp(token->string, "password") == 0)
                parsedline->auth_method = uaPassword;
                //parsedline->auth_method = uaMD5;
        else if (strcmp(token->string, "krb5") == 0)
[root@db-192-168-9-35 postgresql-9.3beta1]# cd /opt/soft_bak/postgresql-9.3beta1
[root@db-192-168-9-35 postgresql-9.3beta1]# gmake world
[root@db-192-168-9-35 postgresql-9.3beta1]# gmake install-world

然后重启数据库
pg93@db-192-168-9-35-> pg_ctl restart
waiting for server to shut down.... done
server stopped
server starting

又不需要密码验证了.
pg93@db-192-168-9-35-> psql
psql (9.3beta1)
Type "help" for help.
postgres=# 


[参考]
1. src/backend/libpq/hba.c
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
存储 网络协议 安全
|
网络协议 关系型数据库 网络安全
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 20 章 客户端认证_20.1. pg_hba.conf文件
20.1. pg_hba.conf文件 客户端认证是由一个配置文件(通常名为pg_hba.conf并被存放在数据库集簇目录中)控制(HBA表示基于主机的认证)。在initdb初始化数据目录时,它会安装一个默认的pg_hba.conf文件。
1618 0
|
安全 关系型数据库 数据库
PostgreSQL 连接问题 FATAL: no pg_hba.conf entry for host
The server doesn't grant access to the database: the server reports FATAL: no pg_hba.conf entry for host "192.
5845 0