openGauss鉴权配置文件pg_hba.conf

openGauss鉴权配置文件pg_hba.conf

准确来说是 Postgres 的鉴权配置文件,因为 openGauss 源于 PostgresSQL 数据库,主要对内核进行和很大的改动,客户端和服务端的驱动协议修改比较少,但支持了 SHA256 等新的密码存储鉴权方式。

首先进入 docker 容器,

1
2
docker ps # 查看 openGauss 容器的 id
docker exec -it <container-id> /bin/bash; exit # 进入 docker 容器进行操作

切换到 omm 用户,启动 gsql:

1
2
su - omm # 切换到 omm 用户
gsql # 启动 gsql

在 gsql 交互式终端中输入以下命令获取 pg_hba.conf 鉴权配置文件的路径:

1
2
3
4
5
6
7
8
9
10
[omm@4ae1997a3f9a opengauss]$ gsql
gsql ((openGauss 1.1.0 build 392c0438) compiled at 2020-12-31 20:07:42 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

omm=# show hba_file;
hba_file
-------------------------------------
/var/lib/opengauss/data/pg_hba.conf
(1 row)

此外,对于 enmotech/opengauss 镜像,openGauss 的安装路径在:

1
/usr/local/opengauss

数据路径在:

1
/var/lib/opengauss

pg_hba.conf

  • hba.conf 为 trust,跳过认证;
  • 配置 hba.conf 为 MD5 方式,修改 postgresq.conf 配置文件中的 GUC 参数 password_encryption_type 为 0,表示 MD5 密码存储方式。在这种方式下,与原来PG基本是一样,方便调通流程。可以看一下MD5 认证的基本流程;
  • 配置 hba.conf 为 SHA256 方式,修改 postgresq 配置文件中的 GUC 参数 pssword_encryption_type 为 2 表示 SHA256 密码存储方式(这是 openGauss 新的鉴权方式)调整 SHA256 认证方式;

对于 authmethod-options,其支持以下选项 (cert 和 gss 不要求,但如果有时间的话可以试试):

  • trust: 不验密,禁止远程主机使用trust 方式访问集群
  • reject: 拒绝访问
  • md5: md5认证,默认不支持
  • sha256: sha256认证(推荐使用)
  • cert: 客户端证书认证
  • gss: kerberos认证

password_encryption_type 参数说明

该字段决定采用何种加密方式对用户密码进行加密存储。修改此参数的配置不会自动触发已有用户密码加密方式的修改,只会影响新创建用户或修改用户密码操作。该参数属于 SIGHUP 类型参数,请参考表4-134中对应设置方法进行设置。

取值范围 0、1、2

  • 0 表示使用md5 方式对密码加密
  • 1表示采用sha256和md5两种方式分别对密码加密
  • 2表示采用sha256方式对密码加密

将 trust 方式改为 sha256 认证后,通过 gsql 登录数据库需要指定数据库名,用户名及密码,如:

1
2
3
4
5
6
7
8
9
[omm@4ae1997a3f9a ~]$ gsql -d postgres -U gaussdb -W Enmo@123
gsql: invalid option -- 'D'
Try "gsql --help" for more information.
[omm@4ae1997a3f9a ~]$ gsql -d postgres -U gaussdb -W Enmo@123
gsql ((openGauss 1.1.0 build 392c0438) compiled at 2020-12-31 20:07:42 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

postgres=>

其中,postgres 是数据库名,gaussdb 是用户名,Enmo@123 是密码。

更多详细信息查阅参考链接中的官方文档 The pg_hba.conf - Postgres Documentation

参考链接

  1. How do I find the path to pg_hba.conf from the shell?
  2. The pg_hba.conf - Postgres Documentation