How to know if a port is open or not on Linux

Asked

Viewed 58,870 times

5

I have a PHP script that acts as a chat server (websocket) that I have on a given system. I realize that sometimes unexpectedly this service stops running. It runs through the command nohup.

I am using Linux.

I only realize that my websocket script stops working when I see browser error:

Websocket Connection to 'wss://myserver:9001/chat' failed: Error During Websocket Handshake: net::ERR_CONNECTION_RESET

Well, I would like to know how to detect if my websocket server script actually stopped running, because I don’t know if the browser error always means that my server "crashed".

So how can I detect whether a particular port is in use or not?

What command on Linux can use to know if a door is open or not?

  • That question interests me too, for the Opensuse.

  • 4

    although it is out of context follow nmap -St -The localhost

  • You want to detect inside a script in some specific language or you want to use the linux terminal for such detection?

  • @Marcoauréliodeleu want to use the Terminal. Because then I would know if the script really stopped running or not. The browser may have other problems that count as error.

  • @Otto out of context? Linux is the operating system I use on my server. I think talking about the server or the OS it uses here is not out of context.

  • 2

    @Wallacemaxters but effectively would not be about programming, I may be wrong but it is out of context yes

  • Anyway, @Otto already answered the question. I just have to add that if you want to test (from a point outside the server) if the port is available in TCP, you can use telnet meudominio.com 443 (where 443 is your door number).

  • 1

    Your question seems to be outside our scope. But fortunately the network Stack Exchange has the website Unix & Linux which is a Q&A aimed at users of Unix like systems. If you prefer you can forward your question to this community, remembering that it is a community independent of ours that therefore has its own rules is managed in English.

Show 3 more comments

1 answer

13


The simplest way to check this would be using the command:

nmap -sT -O localhost 

As added by Milestone can be tested from an external point:

telnet meudominio.com 443 (onde 443 é o número da sua porta)

There is an option to also use netstat:

# netstat -tl - lista as conexões abertas de tcp em modo de escuta
# netstat -t - lista as conexões tcp estabelecidas
# netstat -p - lista os programas que estão usando a conexão
# netstat --numeric-ports - não converte o número da porta para ser listado
# netstat --numeric-hosts - não converte o número de ip para nome do host 

# netstat -t -l -p --numeric-ports 

Or else:

lsof -i tcp 
  • In the first case, I would have to install the nmap. The server is semi-dedicated (I don’t have root, sniff) :D

  • 1

    @Wallacemaxters edited the answer, maybe so help you.

  • The netstat -tl worked right. Thanks :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.