为什么从网络访问时我的端口关闭?

Ber*_*ner 7 iptables firewall networking lan 20.04

我读了很多页,尝试了很多东西,但被困在这里。

我有一个新安装的 Ubuntu 20.04 服务器。我在上面运行了一个 Django 测试服务器(只是为了尝试一下)。它正在运行并侦听端口 8000。

我知道:

$ sudo netstat -tulpen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      112        62240      47748/postgres      
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      1000       65495      48618/python        
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      101        23068      755/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          27689      850/sshd: /usr/sbin 
tcp6       0      0 :::5432                 :::*                    LISTEN      112        62241      47748/postgres      
tcp6       0      0 :::22                   :::*                    LISTEN      0          27691      850/sshd: /usr/sbin 
udp        0      0 127.0.0.53:53           0.0.0.0:*                           101        23067      755/systemd-resolve 
udp        0      0 192.168.0.15:68         0.0.0.0:*                           100        23071      753/systemd-network 
udp6       0      0 fe80::224:e8ff:fe22:546 :::*                                100        23086      753/systemd-network 
Run Code Online (Sandbox Code Playgroud)

它的反应很好。我可以在服务器上做:

wget localhost:8000

我得到了回应。

所以现在我转移到局域网上的台式机。我尝试 wget:

$ wget server.lan:8000
--2020-10-11 11:42:18--  http://server.lan:8000/
Resolving server.lan (server.lan)... 192.168.0.15
Connecting to server.lan (server.lan)|192.168.0.15|:8000... failed: Connection refused.
Run Code Online (Sandbox Code Playgroud)

果然:

$ nmap server.lan
Starting Nmap 7.80 ( https://nmap.org ) at 2020-10-11 22:43 AEDT
Nmap scan report for server.lan (192.168.0.15)
Host is up (0.00025s latency).
Not shown: 998 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
5432/tcp open  postgresql

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds

$ nmap serverlan -p 8000
Starting Nmap 7.80 ( https://nmap.org ) at 2020-10-11 22:44 AEDT
Nmap scan report for server.lan (192.168.0.15)
Host is up (0.00068s latency).

PORT     STATE  SERVICE
8000/tcp closed http-alt

Nmap done: 1 IP address (1 host up) scanned in 0.03 seconds
Run Code Online (Sandbox Code Playgroud)

好的,服务器上的标准检查:

$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:8000

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        
Run Code Online (Sandbox Code Playgroud)

因为我启用了端口 8000:

$ sudo iptables -A INPUT -p tcp --dport 8000 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

按照此处的说明进行操作:https : //www.e2enetworks.com/help/knowledge-base/how-to-open-ports-on-iptables-in-a-linux-server/

和:

$ sudo ufw status verbose
Status: inactive
Run Code Online (Sandbox Code Playgroud)

可是从台式机上lan 8000端口是关闭的!

我应该注意端口 22 是打开的(因为我使用 ssh 登录到服务器来执行此操作,我也可以在端口 5432 上看到数据库,并从桌面使用 pgadmin4 管理它就好了,所以端口 22 和 5432 是开放,但我看不出iptables -L它们是开放的。

Ubuntu 20.04 的神奇之处在于,我读过的许多页面都没有出现在这里。

我什至可以 ping 得很好:

$ ping arachne.lan
PING arachne.lan (192.168.0.15) 56(84) bytes of data.
64 bytes from 192.168.0.15 (192.168.0.15): icmp_seq=1 ttl=64 time=0.269 ms
64 bytes from 192.168.0.15 (192.168.0.15): icmp_seq=2 ttl=64 time=0.267 ms
64 bytes from 192.168.0.15 (192.168.0.15): icmp_seq=3 ttl=64 time=0.530 ms
64 bytes from 192.168.0.15 (192.168.0.15): icmp_seq=4 ttl=64 time=0.284 ms
64 bytes from 192.168.0.15 (192.168.0.15): icmp_seq=5 ttl=64 time=0.280 ms
64 bytes from 192.168.0.15 (192.168.0.15): icmp_seq=6 ttl=64 time=0.280 ms
Run Code Online (Sandbox Code Playgroud)

因为我喜欢它并使用它,所以我安装了cockpit它,它运行在 9090 端口。它从桌面安装和工作。

现在:

$ netstat -tulpen
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      112        62240      -                   
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      1000       65495      48618/python        
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      101        23068      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          27689      -                   
tcp6       0      0 :::5432                 :::*                    LISTEN      112        62241      -                   
tcp6       0      0 :::9090                 :::*                    LISTEN      0          71091      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      0          27691      -                   
udp        0      0 127.0.0.53:53           0.0.0.0:*                           101        23067      -                   
udp        0      0 192.168.0.15:68         0.0.0.0:*                           100        23071      -                   
udp6       0      0 fe80::224:e8ff:fe22:546 :::*                                100        23086      -  
Run Code Online (Sandbox Code Playgroud)

但 iptables 无动于衷:

$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:8000

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination      
Run Code Online (Sandbox Code Playgroud)

但是台式机上的 nmap 可以看到它:

$ nmap server.lan
Starting Nmap 7.80 ( https://nmap.org ) at 2020-10-11 22:55 AEDT
Nmap scan report for server.lan (192.168.0.15)
Host is up (0.00024s latency).
Not shown: 997 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
5432/tcp open  postgresql
9090/tcp open  zeus-admin

Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds
Run Code Online (Sandbox Code Playgroud)

所以 cockpit 和 postgresql 以及 ssh 在安装时都会以某种方式管理以告诉 Ubuntu 20.04 打开一个端口。

所以在服务器上查看一个内省的 nmap:

$ nmap localhost
Starting Nmap 7.80 ( https://nmap.org ) at 2020-10-11 11:58 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00021s latency).
Not shown: 996 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
5432/tcp open  postgresql
8000/tcp open  http-alt
9090/tcp open  zeus-admin

Nmap done: 1 IP address (1 host up) scanned in 0.09 seconds
Run Code Online (Sandbox Code Playgroud)

端口 8000 看起来是开放的。

台式机和服务器之间没有防火墙,只是一个标准的 LAN 交换机。

驾驶舱、postgresql 和 ssh 是如何实现某些目标的,而且很难弄清楚它们是如何实现的以及如何实现的?他们iptables似乎不使用它。也没有ufw。我错过了什么魔法?如何打开8000端口?

ste*_*ver 10

不幸的是,你走错了兔子洞(但是发布一个经过彻底研究的问题做得很好!)

问题不在于port,而是您的服务正在侦听的接口,显示在Local Addressnetstat 报告的列中:127.0.0.1是 localhost(或“环回”)接口 - 它只接受来自同一主机的连接。

您通常可以将服务配置为侦听特定接口或所有外部接口。所以例如

udp        0      0 192.168.0.15:68         0.0.0.0:*                           100        23071      753/systemd-network     
Run Code Online (Sandbox Code Playgroud)

在分配了 IPv4 地址的任何接口上侦听端口 68 192.168.0.15,而您的 SSH 服务

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          27689      850/sshd: /usr/sbin 
Run Code Online (Sandbox Code Playgroud)

正在侦听所有外部接口,如 netstat 的0.0.0.0本地地址值所示。

我对 Django 一无所知,但这 [so] 问答建议了一些方法来配置它以侦听外部接口:

或者(出于安全意识)您可以让它监听环回接口,并为 Web 流量设置 SSH 隧道。