当前位置: 首页>>技术教程>>正文


postgresql – 在Ubuntu中打开端口

, ,

问题描述

因此,我使用的是使用EC2的AWS,并且试图为Postgresql打开一个端口。在AWS中,我已经打开它了:

TCP
Port (Service)      Source                  Action
0 - 65535           sg-92aadda2 (default)   Delete
22 (SSH)            0.0.0.0/0               Delete
80 (HTTP)           0.0.0.0/0               Delete
5432                0.0.0.0/0               Delete

当我执行netstat时,好像端口正在侦听:

# netstat -an | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN

当我执行本地主机nmap时,得到以下信息:

 Nmap scan report for localhost (127.0.0.1)
 Host is up (0.000010s latency).
 Not shown: 997 closed ports
 PORT      STATE SERVICE
 22/tcp    open  ssh
 80/tcp    open  http
 5432/tcp  open  postgresql

这就是乐趣的开始。当我从备用主机执行nmap时,我得到以下信息:

PORT      STATE  SERVICE
22/tcp    open   ssh
80/tcp    open   http
5432/tcp  closed postgresql

我还查看了iptables以查看是否丢失了某些东西,但是iptables看起来空了(这应该意味着它们实际上并没有做很多事情)

$ iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
REJECT     all  --  anywhere             127.0.0.0/8          reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:postgresql
ACCEPT     icmp --  anywhere             anywhere
LOG        all  --  anywhere             anywhere             limit: avg 5/min burst 5 LOG level debug prefix "iptables denied: "
DROP       all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere

我是否缺少某些原因,因为我似乎无法弄清楚如何访问ip。每当我尝试时,都会出现以下错误:

Is the server running on host "xx.xx.xx.xx" and accepting TCP/IP connections on port 5432?

如何使它开放端口,以便外部服务器可以访问它?在此先感谢=)Lemme知道您是否需要其他数据。

编辑:如下所示,我测试了telnetting,并且能够telnet到localhost,但是从外部尝试时却得到:

$ telnet xx.xx.xx.xx 5432
Trying xx.xx.xx.xx...
telnet: Unable to connect to remote host: Connection refused

另外,我仔细检查了一下,并能够将telnet正确地连接到ssh:

$ telnet xx.xx.xx.xx 22
Trying xx.xx.xx.xx...
Connected to xx.xx.xx.xx.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1

最佳方案

编辑/etc/postgresql/<version>/main/postgresql.conf并将listen_addresses设置为您的传出接口或全部。重新启动postgresql:sudo service postgresql restart

次佳方案

它为我工作的最后一种方法(认为朱利奥):

编辑:postgresql.conf

sudo nano /etc/postgresql/9.3/main/postgresql.conf

启用或添加:

listen_addresses = '*'

重新启动数据库引擎:

sudo service postgresql restart


此外,您可以检查文件:pg_hba.conf

sudo nano /etc/postgresql/9.3/main/pg_hba.conf

并添加您的网络或主机地址:

host all all 192.168.1.0/24 md5

第三种方案

如果您编辑了postgresql.conf和main /pg_hba.conf仍然有问题,请尝试使用sudo ufw allow 5432 /tcp取消阻止psql端口

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/8697.html,未经允许,请勿转载。