LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to check a port is open or not (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-check-a-port-is-open-or-not-4175643968/)

catiewong 12-10-2018 12:33 AM

How to check a port is open or not
 
If I would like to confirm a port eg . port 111 is open to connect or not , I just use "netstat -na" to find it , if it is in "Listen" , then means it is open ?

or if I "telnet localhost 111" , if it pops the below message , then mean it is opened ?

Code:


Trying ::1...
Connected to localhost.
Escape character is '^]'.


berndbausch 12-10-2018 01:08 AM

"port open" usually means that no firewall blocks it. When a port is blocked by a firewall, all attempts to connect to it will simply be ignored.

The firewall can be on your server. You can check with the iptables command or, if your distro deploys it, firewall-cmd.

The firewall can be around your server, for example using a firewall appliance or, in an Amazon or OpenStack cloud, using security groups. I don't know of a method to check this from the server.

From outside, a command like telnet or nc can tell you if the port is open. In your example however, you use telnet on the same server as the port. You confirm that a process is listening on port 111, but you don't confirm whether the port is blocked. You have to run telnet or nc from a remote system to confirm this.

If your telnet or nc connection attempt succeeds, you know that the port is open and a process is listening.
If you get "connection refused", you know that the port is open but no process listens.
If you get a message like "no route to host", the server is either not accessible at all (it may be down or there is literally no route to it) or that particular port is blocked. You can then use a tool like ping to check if there is any connectivity.

I am sure nmap is also useful for such tests, but I don't know how to use it in this context.

pan64 12-10-2018 04:01 AM

in general: if there was a program running on the system which is listening on that port (=waiting for some kind of data) we used to say the port is open.
But it is not that trivial, because we can have firewalls, proxy servers, port mapping and other tricks which may make that port unavailable or available (depending on their config/usage). So port is opened does/may not mean it is reachable from another host.

Sakralbar 12-11-2018 07:28 AM

May be this help you https://portchecker.co/

TB0ne 12-11-2018 07:44 AM

Quote:

Originally Posted by Sakralbar (Post 5935837)
May be this help you <BAD ADVICE LINK REMOVED>

Sorry, but this is a VERY bad bit of advice; why would anyone go to an unknown website and run something to scan their system for open ports?? You're advertising to some unknown parties what your IP address is, what ports/services are running, etc.

Madhu Desai 12-11-2018 07:47 AM

Code:

sudo nmap -p 111 127.0.0.1


All times are GMT -5. The time now is 10:24 PM.