netstat commands to run on unix server, what commands should I use for my use-case and why?

StackOverflow https://stackoverflow.com/questions/19614348

  •  01-07-2022
  •  | 
  •  

Domanda

Sorry in advance for such a noob question, but I'm certainly a noob. My question is what does it mean to LISTEN or ACCEPT on a port as it relates to my example?

EXAMPLE: I have a tomcat server, and It will use port 8080. I want to make sure that port is available for me to use.

What commands should I perform on my unix server and why?

what information would a command like this give me: netstat -an | grep LISTEN

È stato utile?

Soluzione

If a port shows up as LISTEN in netstat, it means the port is in use by a server process, so you can't use it. Here is an example:

tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN

which shows that port 631 is in use.

Ignore the UNIX type sockets at the end - they are irrelevant.

Altri suggerimenti

For checking port 8080 is in use or not, you can simply use the command netstat -an|grep 8080. If you get an output in below format, that means 8080 is already in use and you need to assign a new port for the tomcat.

# netstat -an
tcp        0      0 127.0.0.1:8080              0.0.0.0:*                   LISTEN

Netstat command displays various network related information such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships etc,

  • a option with netstat will give you both listening and non listening ports

  • n option when you don’t want the name of the host, port or user to be displayed, use netstat -n option. This will display in numbers, instead of resolving the host name, port name, user name. This also speeds up the output, as netstat is not performing any look-up.

For more understand the use of netstat command here are its options:

-a : All ports
-t : Ports TCP
-u : Ports UDP
-l : Listening ports
-n : IP address without domain name resolution
-p : Name of the program and it associated PID

So:

-To display all port (TCP & UDP), PId with the associated name of the program :

 $ netstat -paunt

-To display all Listening ports (TCP), PId with the associated name of the program : (and we can also filter with the grep command)

 $ sudo netstat -plnt | grep ':80'

I hope it will be helpful :)

You can also use telnet to check if the port is open and listening e.g,

Zeeshan$ telnet google.com 80
Trying 173.194.35.5...
Connected to google.com.
Escape character is '^]'.

I am telnetting google.com on port 80. If you see the third line in the output, you will notice it says it is connected with the Google's web server. The same way you have a JAVA application server called Tomcat and it is listening on port 8080. In fact it is asking clients to connect to it on port 8080 so it can give away the JAVA services to client. When I will use from a client side telnet localhost 8080 I will be connected the same way I have connected with Google's web server on port 80. Provided that Tomcat is running and listening on port 8080. If port 8080 is not free and occupied by some other application you can simply change the port 8080 to another free port. Telnet should give you the following status:

accepted (connected), refused, and timeout
  • connection refused - nothing is running on that port
  • accepted - some application is running on the port
  • timeout - a firewall is blocking access

So now there are two possible ways to check. From the same machine you are running Tomcat server:

telnet localhost 8080

Of if you want to check it from some other machine or outside of the network:

telnet 192.168.1.1 8080

I hope that helps.

use can also run the below command, it will list the Port and corresponding PID, if any process is using those ports

   netstat -tulpn
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top