無法在 Debian 上開啟端口

無法在 Debian 上開啟端口

我有 Debian 安裝

~$ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/

我安裝 PostgreSQL 的地方

 PostgreSQL 11.3 (Debian 11.3-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit

PostgreSQL正在運行,但我可以從另一台機器上使用它:我透過firewalld打開了5432 por

# firewall-cmd --zone=public --list-ports
8443/tcp 5432/tcp

但它不起作用。

這個本地

~$ telnet localhost 5432
Trying ::1...
Connected to localhost.
Escape character is '^]'.

這是來自我的機器(在另一個 VLAN 上)

:> telnet 192.168.13.183 5432
Trying 192.168.13.183...
telnet: Unable to connect to remote host: Connection refused

這是來自另一台機器(與 debian 位於同一 VLAN 中)

telnet 192.168.13.183 5432
Trying 192.168.13.183...
telnet: connect to address 192.168.13.183: Connection refused

所以我關閉了firewalld(僅用於測試),但我得到了相同的結果。之後我嘗試用 iptables 做同樣的事情:

iptables -A INPUT -p tcp --dport 5432 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 5432 -m conntrack --ctstate ESTABLISHED -j ACCEPT

具有相同的結果我在這裡缺少什麼?

只需檢查一下即可(謝謝@托馬斯):

netstat -tulpan | grep postgres
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      6386/postgres       
tcp6       0      0 ::1:5432                :::*                    LISTEN      6386/postgres       
udp6       0      0 ::1:44652               ::1:44652               ESTABLISHED 6386/postgres

lsof -n -i:5432
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
postgres 6386 postgres    3u  IPv6  72390      0t0  TCP [::1]:postgresql (LISTEN)
postgres 6386 postgres    5u  IPv4  72391      0t0  TCP 127.0.0.1:postgresql (LISTEN

答案1

postgresql預設僅偵聽本機主機(127.0.0.1::1)。若要變更此行為,您必須在 中手動設定其他偵聽位址postgresql.conf,通常位於 中$PGDATA

例如,要讓 postgres 也偵聽本機位址 10.0.10.156,您必須指定listen_addresses如下。

listen_addresses = 'localhost, 10.0.10.156'

要讓 postgres 監聽所有介面,您也可以使用通配符。

listen_addresses = '*'

進行此更改後,需要重新啟動 postgres。我還建議檢查結果是否符合您的期望。


lsofpostgres 在 localhost 和 10.0.10.156 上監聽的輸出。

# lsof -n -i:5432
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
postgres 7893 postgres    3u  IPv6  55868      0t0  TCP [::1]:postgres (LISTEN)
postgres 7893 postgres    4u  IPv4  55869      0t0  TCP 127.0.0.1:postgres (LISTEN)
postgres 7893 postgres    5u  IPv4  55870      0t0  TCP 10.0.10.156:postgres (LISTEN)

lsofpostgres 監聽所有介面的輸出。

# lsof -n -i:5432
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
postgres 7527 postgres    3u  IPv4  54903      0t0  TCP *:postgres (LISTEN)
postgres 7527 postgres    4u  IPv6  54904      0t0  TCP *:postgres (LISTEN)

相關內容