簡體   English   中英

在 Ubuntu 中打開端口

[英]Open Port in 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

當我執行 localhost 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?

我如何才能打開端口以便外部服務器可以訪問它? 提前致謝 =) 讓我知道您是否需要任何其他數據。

編輯:如下所述,我測試了 telnet 連接,並且能夠 telnet 到本地主機,但是當從外部嘗試時,我得到:

$ 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

最后一種方法對我有用(感謝 Julio):

編輯: 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.confmain/pg_hba.conf並且仍然有問題,請嘗試

sudo ufw allow 5432/tcp

解鎖 psql 端口

如果您使用 docker 連接到主機的 postgresql,您必須使用主機的 ip,您可以通過運行ip addr show docker0獲得該 ip,希望它可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM