1. 程式人生 > >04-postgresql-9.6.1安全管理之pg_hba.conf配置(2017-06-12)

04-postgresql-9.6.1安全管理之pg_hba.conf配置(2017-06-12)

1、測試需求

192.168.181.141上的postgresql資料庫,192.168.1.138要無密碼登入進來,192.168.181.181要md5登入

2、步驟

1)檢查192.168.181.141上的postgresql資料庫的postgres使用者l密碼

postgres=# select pg_user.usename,pg_user.passwd from pg_user;
 usename  |  passwd 
----------+----------
 postgres | ********
(1 row)

postgres=# select usename,passwd from pg_shadow;
 usename  | passwd
----------+--------
 postgres |
(1 row)

postgres=# select
md5('');
md5 ---------------------------------- d41d8cd98f00b204e9800998ecf8427e (1 row) postgres=# select rolname,rolpassword from pg_authid; rolname | rolpassword -------------------+------------- postgres | pg_signal_backend | (2 rows) postgres=# select
rolname,rolpassword from pg_authid;
rolname | rolpassword -------------------+------------- postgres | pg_signal_backend | (2 rows)

2)修改密碼為postgres

postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE

3)配置192.168.181.141的pg_hba.conf檔案。

host    all             all             192.168.181.180/32      md5
host    all             all             192.168.181.138/32      trust

備註:例如如下的配置,會按順序從上往下執行。

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    replication     postgres        192.168.1.145/32        trust
host    replication     postgres        192.168.1.146/32        trust
host    all             postgres        192.168.1.0/32          md5
host    all             all             0.0.0.0/0               md5

0.0.0.0/32表示 允許地址在0.0.0.0-255.255.255.255範圍內的客戶端
192.168.1.0/24表示子網掩碼為255.255.255.0,此時允許192.168.1.0-192.168.1.255範圍內的客戶端地址連結。
192.168.1.0/32表示IP為192.168.1.0

4)測試

pg961為192.168.181.180

[postgres@pg961 opt]$ psql -h 192.168.181.141 -p 5432 -U postgres
Password for user postgres:
psql (9.6.1)
Type "help" for help.

postgres=#

zabbix為192.168.181.138

[postgres@zabbix /]$psql -h 192.168.181.141 -p 5432
psql (9.5.4, 伺服器 9.6.1)
警告:psql 主版本9.5,伺服器主版本為9.6.
     一些psql功能可能無法正常使用。
輸入 "help" 來獲取幫助資訊.

postgres=

5)附加測試:配置.pgpass

因為從192.168.181.180登入到192.168.181.141的資料庫中,每次要輸入密碼。那麼可以在/home/postgre/目錄下建立一個檔案
格式:hostname:port:database:username:password

[postgres@pg961 opt]$ vim /home/postgres/.pgpass
192.168.181.141:5432:postgres:postgres:postgres

再次進行登入就可以不用密碼了。

[postgres@pg961 opt]$ psql -h 192.168.181.141 -p 5432 -U postgres
psql (9.6.1)
Type "help" for help.

postgres=#

測試完成