溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

pg_hba.conf 和 pg_ident.conf

發(fā)布時(shí)間:2020-06-02 20:37:18 來源:網(wǎng)絡(luò) 閱讀:929 作者:Darren_Chen 欄目:數(shù)據(jù)庫

初始化后pg_hba.conf默認(rèn)的內(nèi)容:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only

local   all             all                                     trust

# IPv4 local connections:

host    all             all             127.0.0.1/32            trust

# IPv6 local connections:

host    all             all             ::1/128                 trust

# Allow replication connections from localhost, by a user with the

# replication privilege.

#local   replication     postgres                                trust

#host    replication     postgres        127.0.0.1/32            trust

#host    replication     postgres        ::1/128                 trust


(1)type定義了數(shù)據(jù)庫的連接方式,有四種方式:

local:使用unix-domain(unix套接字)

host:使用TCP/IP連接,包括SSL和No SSL

hsotssl:使用TCP/IP連接,只能使用SSL加密方式

hostnossl:使用TCP/IP連接,不能使用SSL加密方式


(2)database指定哪些庫可以被連接

all匹配所有庫,要指定多個(gè)庫,可以通過commas分隔


(3)user指定哪些用戶可以連接

all匹配所有角色,要指定多個(gè)角色,可以通過commas分隔


(4)address指定哪些機(jī)器可以連接

首先如果type是local方式,address可以不用寫;

如果type是其他格式,address可以是主機(jī)名,IP范圍,IP地址

0.0.0.0/0 代表所有IP地址

172.20.143.89/32 允許這個(gè)ip登錄

10.1.1.0/24 允許10.1.1.0~10.1.1.255網(wǎng)段登錄數(shù)據(jù)庫


(5)method指定客戶端連接數(shù)據(jù)庫認(rèn)證方式


trust:只要知道數(shù)據(jù)庫用戶名就不需要密碼或ident就能登錄,建議不要在生產(chǎn)環(huán)境中使用。


md5:是常用的密碼認(rèn)證方式,如果你不使用ident,最好使用md5。密碼是以md5形式傳送給數(shù)據(jù)庫,較安全,且不需建立同名的操作系統(tǒng)用戶


password:以明文密碼傳送給數(shù)據(jù)庫,建議不要在生產(chǎn)環(huán)境中使用


ident:

ident是Linux下PostgreSQL默認(rèn)的local認(rèn)證方式,凡是能正確登錄操作系統(tǒng)用戶(注:不是數(shù)據(jù)庫用戶)就能使用本用戶映射的數(shù)據(jù)庫用戶不需密碼登錄數(shù)據(jù)庫;

如果操作系統(tǒng)用戶在pg_ident.conf文件中沒有映射用戶,則默認(rèn)的映射數(shù)據(jù)庫用戶與操作系統(tǒng)用戶同名;

用戶映射文件為pg_ident.conf,這個(gè)文件記錄操作系統(tǒng)用戶和數(shù)據(jù)庫用戶映射關(guān)系,

比如,服務(wù)器上有名為user1的操作系統(tǒng)用戶,同時(shí)數(shù)據(jù)庫上也有同名的數(shù)據(jù)庫用戶,user1登錄操作系統(tǒng)后可以直接輸入psql,以u(píng)ser1數(shù)據(jù)庫用戶身份登錄數(shù)據(jù)庫且不需密碼。


reject:拒絕認(rèn)證


配置監(jiān)聽地址

PostgreSQL默認(rèn)只監(jiān)聽本地端口,

[root@Darren2 postgresql-9.6.3]# netstat -nltup|grep postgres

tcp        0      0 127.0.0.1:5432              0.0.0.0:*                   LISTEN      49675/postgres      

tcp        0      0 ::1:5432                    :::*                        LISTEN      49675/postgres      

通過修改postgres.conf文件,修改監(jiān)聽

Darren1:postgres:/usr/local/pgsql/data:>vim postgresql.conf

#listen_addresses = 'localhost'         # what IP address(es) to listen on;

listen_addresses = '*'          # what IP address(es) to listen on;

[root@Darren2 postgresql-9.6.3]# netstat -nltup|grep postgres

tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      50694/postgres      

tcp        0      0 :::5432                          :::*                          LISTEN      50694/postgres     


eg:

先創(chuàng)建一個(gè)可以登錄的用戶cdhu

postgres=# create role cdhu1 password '147258' login;


(1)修改pg_hba.conf,來自任何IP的客戶端都可以登錄,但是需要密碼驗(yàn)證

host    all             all             0.0.0.0/0               md5

Darren2:postgres:/usr/local/pgsql/data:>pg_ctl reload

Darren2:postgres:/usr/local/pgsql/data:>psql -h292.168.163.102 -U postgres -d postgres -W

Password for user postgres:147258

Darren2:postgres:/usr/local/pgsql/data:>psql -h292.168.163.102 -U cdhu1 -d postgres -W

Password for user cdhu1:147258


(2)來自192.168.163.*的網(wǎng)段IP都可以登錄,但是需要密碼驗(yàn)證

host    all             all             192.168.163.0/24               md5

Darren2:postgres:/usr/local/pgsql/data:>pg_ctl reload

Darren2:postgres:/usr/local/pgsql/data:>psql -h292.168.163.102 -U cdhu1 -d postgres -W

Password for user cdhu1:147258


(3)只允許來自192.168.163.101的客戶端連接數(shù)據(jù)庫,但是需要密碼驗(yàn)證

host    all             all            192.168.163.101/32               md5

Darren2:postgres:/usr/local/pgsql/data:>pg_ctl reload

#登錄成功

Darren1:postgres:/usr/local/pgsql/data:>hostname -i

192.168.163.101

Darren1:postgres:/usr/local/pgsql/data:>psql -h 192.168.163.102 -U cdhu1 -d postgres -W

Password for user cdhu1:147258 可以正常登錄

#登錄失敗

Darren2:postgres:/usr/local/pgsql/data:>hostname -i

192.168.163.102

Darren2:postgres:/usr/local/pgsql/data:>psql -h292.168.163.102 -U cdhu1 -d postgres -W

Password for user cdhu1:

FATAL:  no pg_hba.conf entry for host "192.168.163.102", user "cdhu1", database "postgres"

psql: FATAL:  no pg_hba.conf entry for host "192.168.163.102", user "cdhu1", database "postgres


(4)只允許來自192.168.163.101的客戶端連接數(shù)據(jù)庫,無需密碼驗(yàn)證

host    all             all            192.168.163.101/32               trust

Darren1:postgres:/usr/local/pgsql/data:>psql -h 192.168.163.102 -U cdhu1 -d postgres


(5)如果操作系統(tǒng)用戶在pg_ident.conf文件中沒有映射用戶,則默認(rèn)的映射數(shù)據(jù)庫用戶與操作系統(tǒng)用戶同名;

Darren2:postgres:/usr/local/pgsql/data:>vim pg_ident.conf

mapname1                cdhu1           cdhu1  (默認(rèn)存在系統(tǒng)用戶和數(shù)據(jù)庫用戶名相同的映射)

Darren2:postgres:/usr/local/pgsql/data:>vim pg_hba.conf

local   all             all                                     ident

[root@Darren2 postgresql-9.6.3]# useradd cdhu1

[root@Darren2 postgresql-9.6.3]# passwd cdhu1

postgres=# create role cdhu1 password '147258' login;

[root@Darren2 postgresql-9.6.3]# su - cdhu1

#系統(tǒng)用戶cdhu1,數(shù)據(jù)庫用戶cdhu1,此時(shí)不需要密碼即可登錄數(shù)據(jù)庫

[root@Darren2 postgresql-9.6.3]# su - cdhu1

[cdhu1@Darren2 ~]$ /usr/local/pgsql/bin/psql -h localhost -U cdhu1 -d postgres


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI