Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Ways to Find Out List of All Open Ports in Linux

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we are going to see how to find out the list of all open ports in Linux. Ports are the endpoint of communication in computer networks, or we can also say that the port is working as the door for communication on the computer network. The ports are basically 16-bit numbers (from 0 to 65535). These ports are used to communicate by the Internet transport protocols like the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP)

The ports are categorized by the range of port numbers as follows:

  1. From 0 to 1023: These ports are known as well-known ports. These ports can only be used by system (or root) processes or by programs executed by privileged users.
  2. From 1024 to 49151: These ports are known as the Registered ports. These ports can be used by ordinary user processes or programs executed by ordinary users.
  3. From 49152 to 65535: These ports are known as Dynamic Ports.

Before we learn how to find the list of open ports in the Linux system, we must look at how we can get the list of all ports on the system by using the command mentioned below:

$cat /etc/services 
cat /etc/services

cat /etc/services 

Now let’s see how to find out the list of open ports in the Linux systems.

There are three ways by which we can find the list of open ports on the Linux system. Let’s see them one by one.

Method 1: Using netstat tool

Netstat is a tool which gives information about the Linux networking subsystem. We use netstat to list all open ports on the system. Use the following command to list all open ports on the system.

$ netstat -lntu

In the above command:

  • Option -l: list only listening sockets.
  • Option -n: show the port number.
  • Option -t: list the TCP ports.
  • Option -u: list the UDP ports
netstat -lntu

netstat -lntu

Method 2: Using ss tool.

ss is another tool to investigate sockets, this is the best alternative to the netstat command? So, we can also use the ss tool to list the open ports on the system. Use the following command to list all the ports on the system.

$ ss -lntu

The meaning of all options used with the above command are the same as the previous netstat command.

ss -lntu

ss -lntu

Method 3:  lsof command

lsof is the command which is used to list the files. We can use the lsof command to list the open ports on the system using the following command:

$ sudo lsof -i -P -n | grep LISTEN

In the above command:

  • Option -i: selects the listing of files, any of whose Internet address matches the address specified in i.
  • Option -P: inhibits the conversion of port numbers to port names for network files.
  • Option -n: inhibits the conversion of network numbers to host names for network files.
sudo lsof -i -P -n | grep LISTEN

sudo lsof -i -P -n | grep LISTEN

Frequently asked Questions 

1) How do I find the open ports on a specific IP address?

We can use the methods that are mentioned above with `-a` option to display all the connections and listening ports associated with a specific IP address.

Syntax:

$ netstat -ant | grep <IP address>

Here, enter your desired IP address in place of <IP address>.

2) How do I close an Open port in Linux?

First, we can find the process using `lsof` command and the terminate it with `kill` command.

Syntax:

$ sudo lsof -i :<port number>

Here, enter your desired port number in place of <port number>.

$ sudo kill <process ID>

Here, enter the process ID you get from above step in place of <process ID>.

3) How do I block incoming traffic to a specific port in Linux?

We can use firewall to block incoming traffic to a specific port by using `ufw` command.

Syntax:

$ sudo ufm deny <port_number>/tcp

Here, enter your desired port number in place of <port number>.

My Personal Notes arrow_drop_up
Last Updated : 05 May, 2023
Like Article
Save Article
Similar Reads