How to check if port in use linux

How to check if a certain port is open and unused?

Could you please help and tell how can i find out if port 80 is open and unused so that I can start installation.

For what it’s worth, /etc/services is completely static. Grepping it can tell you if a port is officially designated by IANA or some such, but does not tell whether or not it’s in local use.

9 Answers 9

sudo netstat -anp | grep ':80 ' 

That should give you pid & name of the process that holds port 80

This can be achieved using the nc command as follows:

It will return TRUE if the port is already in use, or FALSE is it (i.e, available not listening currently).

I don’t recommend lsof or netstat method as it first try to scan all running PIDs to get all bounded ports:

# time lsof -i:8888 real 0m1.194s user 0m0.137s sys 0m1.056s``` # time nc -z 127.0.0.1 8888 real 0m0.014s user 0m0.011s sys 0m0.004s 

Here 8888 is an unused port. The nc command is ~85 times faster in the above example.

Eg 1:

$ nc -z 127.0.0.1 80 && echo "IN USE" || echo "FREE" IN USE $ nc -z 127.0.0.1 81 && echo "IN USE" || echo "FREE" FREE 

Eg 2:

If you are trying with a remote IP, it is better to add a timeout to auto-exit if it is not accepting connection for the specified time.

Its Google’s IP which is not used, so it will timeout after trying for 2 seconds.

This also works greatly when running inside the Docker image that uses host network. Inside the image, lsof incorrectly reports the port is not in use when it actually is.

The traditional version of nc does not include the -z option. See the differences between traditional and openbsd.

netstat -tln | tail -n +3 | awk '< print $4 >' 

This one displays bind addresses of TCP listening endpoints. All other endpoints are free; Also if on Unix and you are not root, then you can’t bind to a ‘privileged’ port number (port number lower than 1024).

Explained in more details:

  • netstat -tln — all listening tcp ports
  • tail -n +3 — cut off the header of netstat command
  • awk ‘< print $4 >‘ — print the fourth column that consists of [ip]:[port]

For the general case you still need to care to cut out all irrelevant interfaces; a listening address 0.0.0.0 is listening on all network cards, if there is an IP address then that’s the specific IP of the network card/network interface.

Источник

How to Check If a Port Is in Use in Linux

If you are from a computer science background or even a little bit familiar with networking, then you may have heard of the TCP/IP stack. The TCP/IC stack comprises of five different layers, namely, the Physical Layer, Data Link Layer, Network Layer, Transport Layer, and Application Layer. Every layer of the TCP/IP stack has a different means of communication, and all communication within the Transport Layer is done via port numbers.

Читайте также:  Learn about linux operating system

A port number is used to uniquely identify a device alongside the IP address. Inter-process communication is common when using computer systems. To facilitate this communication, operating systems keep certain ports open, depending upon the entity with which the user wishes to communicate. So, at any single instance, multiple ports can be open in your system.

When we say that a port is in use, we are essentially referring to a port that is open, or, in other words, a port that is in the listening state (ready to accept connections). There are multiple ways of determining the ports that are open in an operating system. This article shows you four possible methods to use to check whether a port is in use in Linux.

Note: All the methods demonstrated in this article have been executed in Linux Mint 20.

To determine whether a port is in use in Linux Mint 20, any of the following four methods can be used.

Method 1: Using the lsof Command

The lsof command can be used to list all the ports in use in your system in the following manner:

First, launch the Linux Mint 20 terminal by clicking on its shortcut icon. The terminal is shown in the image below:

Next, you will have to install the lsof command if you have never used it before. To do so, execute the following command in the terminal:

Upon the successful installation of the command, you will see the following output in the terminal:

Once this command has been installed, it can be used for querying any ports that are in use in Linux. To check your system for open ports, execute the following command in your terminal:

In the output of this command, the ports listed in the “LISTEN” state are the ones that are in use, as shown in the image below:

Method 2: Using the ss Command

The ss command can be used to determine any open TCP and UDP ports in your system in the following manner:

To query both the TCP and UDP ports that are in use, execute the following command in the terminal:

In the output of this command, the ports (both TCP and UDP) that are in use have the “LISTEN” state, whereas all the other ports show the “UNCONN” state.

Method 3: Using the netstat Command

The netstat command can also be used to determine any open TCP and UDP ports in your system in the following manner:

To query for the TCP and UDP ports that are in use, run the following command in the terminal:

If you try to run this command without the “sudo” keyword, you will not be able to access all the ports. If you are logged in with the root user account, then you may skip this keyword.

When you run this command, you will be able to see that all ports in use are in the “LISTEN” state, whereas the states of all other ports are unavailable, as shown in the image below:

Method 4: Using the nmap Command

The nmap command is yet another utility that can be used to determine the TCP and UDP ports that are in use in the following manner:

Читайте также:  Партнерский портал astra linux

If the nmap utility is not yet installed on your Linux Mint 20 system, as it does not come installed by default, you may have to manually install it. To do so, execute the following command:

Once you have successfully installed the nmap utility on your Linux Mint 20 system, your terminal will return you the control back so that you can execute the next command, as shown in the image below:

After installing this utility, query for both the TCP and UDP ports that are in use in your system by running the following command in the terminal:

Once you have executed this command, the state of all ports that are in use will be “open,” as shown in the output in the image below:

Conclusion

This article showed you four different methods for checking whether a port is in use in your Linux system. All of these methods were tested with Linux Mint 20, however, you can also run the commands shown in these methods with any other distribution of Linux, with slight variations. Each of the commands used in these methods takes only a few seconds to execute. So, you have the time to try any of the four methods to see which one works best for you.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Источник

How to Check (Scan) for Open Ports in Linux

In this tutorial, we will discuss many methods for determining which ports on your Linux system are available to the outside world.

How to Check (Scan) for Open Ports in Linux

Introduction

One of the first things to check when troubleshooting network connectivity issues or establishing a firewall is whether ports are truly open on your machine.

In this tutorial, we will discuss many methods for determining which ports on your Linux system are available to the outside world.

What is Open Port?

A program that listens on a network port is known as a listening port. You may retrieve a list of your system’s listening ports by using tools like ss , netstat , or lsof to query the network stack. Using a firewall, each listening port can be opened or blocked (filtered).

A network port that admits incoming packets from faraway destinations is known as an open port.

For instance, if your web server listens on ports 80 and 443 and those ports are open on your firewall, anyone (excluding blocked ips) can use his browser to view websites housed on your web server. Both ports 80 and 443 are open in this scenario.

Open ports can be a security issue since attackers can use them to exploit vulnerabilities or carry out other types of attacks. All other ports should be closed and just the ports required for your application’s operation should be exposed.

Check Open Ports with nmap

Nmap is a network scanning program capable of scanning both single hosts and big networks. It’s mostly used for penetration testing and security assessments.

When it comes to port scanning, nmap should be your first choice if it is available. Nmap can determine the Mac address, OS type, kernel versions, and much more in addition to port scanning.

Which ports are listening for TCP connections from the network can be determined by using the following command from the console:

Читайте также:  Err address unreachable linux

The -sT option instructs nmap to scan for TCP ports, whereas the -p- option instructs it to scan for all 65535 ports. If the -p- option is not specified, nmap will only scan the 1000 most popular ports.

Output Starting Nmap 7.60 ( https://nmap.org ) at 2019-07-09 23:10 CEST Nmap scan report for 10.10.8.8 Host is up (0.0012s latency). Not shown: 998 closed ports PORT STATE SERVICE 22/tcp open ssh 80/tcp open http MAC Address: 08:00:27:05:49:23 (Oracle VirtualBox virtual NIC) Nmap done: 1 IP address (1 host up) scanned in 0.41 seconds 

Only ports 22 , 80 , and 8069 are open on the target system, according to the output.

Instead of -sT , use -sU to scan for UDP ports:

Visit the nmap man page for more information and to learn about all of this tool’s other useful features.

Check Open Ports with netcat

Netcat (or nc ) is a command-line utility that uses the TCP or UDP protocols to read and write data across network connections.

Netcat can scan a single port or a range of ports.

To search for open TCP ports on a distant system with IP address 10.10.8.8 in the range 20-80 , for example, execute the command:

The -z option instructs nc to scan only for open ports and not transfer any data, whereas the -v option provides more detailed information.

This is what the output will look like:

Output The -z option instructs nc to scan only for open ports and not transfer any data, whereas the -v option provides more detailed information. This is what the final product will look like: 

If you just want the lines with open ports written on the screen, use the grep command to filter the results.

nc -z -v 10.10.8.8 20-80 2>&1 | grep succeeded 
Output Connection to 10.10.8.8 22 port [tcp/ssh] succeeded! Connection to 10.10.8.8 80 port [tcp/http] succeeded! 

Pass the -u argument to the nc command to scan for UDP ports:

nc -z -v -u 10.10.8.8 20-80 2>&1 | grep succeeded 

Check Open Ports using Bash Pseudo Device

The Bash shell /dev/tcp/.. or /dev/udp/.. pseudo-device can also be used to determine whether a port is open or closed.

Bash will open a TCP or UDP connection to the specified host on the specified port when a command is run on a /dev/$PROTOCOL/$HOST/$IP pseudo-device.

The if..else statement below will check if port 443 on kernel.org is open:

if timeout 5 bash -c '/dev/null' then echo "Port is open" else echo "Port is closed" fi 

What is the purpose of the code above?

Because the default timeout when connecting to a port via a pseudo-device is so long, we’re utilizing the timeout command to kill the test command after 5 seconds. The test command will return true if the connection to kernel.org port 443 is established.

Use the for loop to check for a port range:

for PORT in ; do timeout 1 bash -c "/dev/null" && echo "port $PORT is open" done 

You will get an output like below:

Output port 22 is open port 80 is open 

Conclusion

We’ve taught you how to scan for open ports with a variety of tools. You can also check for open ports using other utilities and methods, such as the Python socket module, curl , telnet , or wget .

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Источник

Оцените статью
Adblock
detector