Check Open Ports on Linux

How to Check Open Ports on Linux

Whether you are using Linux as a server or desktop, knowing about open ports or ports in use can be helpful in various situations.

For example, if you are running a web server based on Apache or Ngnix, the port being used should be 80 or 443, checking the port will confirm this, and similarly, you can check for open ports for SMTP or SSH.

You can also check for open ports for intrusion detection.

There are several ways to check ports in Linux, and in this quick tip, I will share my two favorite methods.

Method 1: Use the lsof command to view open ports in the currently logged-in Linux system

If you are logged in directly or via SSH to the system, you can use the lsof command to check its ports.

sudo lsof -i -P -n

This lsof command is used to find the files and processes that the user is using, and the options used here are:

-i: Selects the list of all network files if an IP address is not specified -P: Prevents the conversion of port numbers to network file port names -n: Prevents the conversion of network numbers to network file hostnames

However, this also shows us many additional ports that the computer is not listening to.

You can pipe this output to the grep command and match the pattern “LISTEN,” as shown below:

sudo lsof -i -P -n | grep LISTEN

This will only display the ports that our computer is actively listening on and which service is using the said open ports.

Method 2: Use the netcat command to check ports on any remote Linux server

nc (Netcat) is a command-line utility that reads and writes data between computers over a network using TCP and UDP protocols.

The syntax for the command is as follows:

nc [options] host port

This utility has a nice -z flag that, when used, scans listening daemons with nc without actually sending any data to the port.

Combining this with the -v flag for verbosity, you can get detailed output.

Here is the nc command you can use to scan open ports:

nc -z -v <IP-ADDRESS> 1-65535 2>&1 | grep -v 'Connection refused'

Replace IP-ADDRESS with the IP address of the Linux system you are checking ports on.

As for why I chose the values 1 to 65535, it is because the port range starts at 1 and ends at 65535.

Finally, pipe the output to the grep command. Using the -v option excludes any lines that match the pattern “Connection refused”.

This will display all ports open on the computer that can be accessed by another computer on the network.

Conclusion

Of these two methods, I prefer the lsof command as it is faster than the nc command. However, you need to be logged into the system and have sudo access. In other words, lsof is the more suitable choice if you are managing the system.

The nc command has the flexibility of scanning ports without needing to be logged in.

Both commands can be used to check for open ports in Linux based on the scenario you are in.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *