How to check if any port is open and not in use?

I want to install webserver-apache on a Linux platform that uses port no.80, but I'm not sure if the port is open or not and is being used by some other application or not.

  • The output of grep 80 /etc/services :

http 80/tcp www www-http #World Wide web Http

http 80/udp www www-http #Hypertext transfer protocol

2. netstat -an | grep 80 | more netstat -an | grep 80 | more netstat -an | grep 80 | more :

It gives some IP, one of which has IP: 80 TIME_WAIT

Could you help and tell how I can find out if port 80 is open and not in use so that I can start the installation.

+6
source share
4 answers
  netstat -tln | tail -n +3 | awk '{ print $4 }' 

it displays the tcp listening endpoint binding addresses. all other endpoints are free; In addition, if on Unix you are not a root user, then you cannot bind to a β€œprivileged” port number (port number below 1024)

explained in more detail:

netstat -tln - all TCP listening ports

tail -n +3 - netstat header cut

awk '{print $ 4}' - print the fourth column consisting of [ip]: [port]

for the general case, you still need to take care to cut out all irrelevant interfaces; The listening address 0.0.0.0 listens on all network cards, if there is an IP address than the specific IP address of the network car / network interface.

+4
source

Try the lsof command in grep and find the port number:

 lsof|grep <port> 

If nothing is displayed, it means that the port is not in use

You can kill a process on a specific port using

 kill -9 <pid> 

Where pid is the process identifier obtained from the first command.

+2
source
 sudo netstat -anp | grep ':80' 

This should give you the pid and name of the process that contains port 80

+1
source

As part of the script, you probably want to use something like this:

 resp=`netstat -tunl | grep ":80 "` if [ -z "$resp" ]; then echo "80 Port is free" else echo "80 Port is not free" fi 
0
source

All Articles