Search code examples
centos7firewallnmap

nmap reports closed port Centos 7 while a pid is running on this port


On a CentOS Linux 7 machine, I have a web app served on port 1314

$ netstat -anp | grep 1314
tcp        0      0 127.0.0.1:1314          0.0.0.0:*               LISTEN      1464/hugo
tcp        0      0 127.0.0.1:60770         127.0.0.1:1314          TIME_WAIT   -

and I can curl it locally. I opened port 1314:

iptables-save | grep 1314
-A IN_public_allow -p tcp -m tcp --dport 1314 -m conntrack --ctstate NEW,UNTRACKED -j ACCEPT

I checked with nmap locally:

PORT     STATE SERVICE
1314/tcp open  pdps

Everything seems fine.

Now if I try to curl the web app from another machine I get connection refused. When I try nmap from the remote machine:

PORT     STATE  SERVICE
1314/tcp closed pdps

So the firewall doesn't block the port, but it looks like there is no one listening on port 1314... But we know that the web app is running on this endpoint so what is going on??


Solution

  • Having a process listening to a port (and that port is open and properly configured) is not enough to enable remote communication. The local address needs to be on the same network as the remote address too!

    Here, on the netstat printout, we can see that the local address is localhost (127.0.0.1 or ::1). Localhost is obviously not on the same network as the remote machine I was using to curl my web app. This explains also why nmap was reporting a closed port (meaning that nothing was listening on the local end).

    Note: to listen to all the network interfaces, the local address should be 0.0.0.0 or :::.