In this guide, we’ll explore how to identify the comprehensive list of open ports in Linux, crucial endpoints for communication within computer networks. Ports, serving as gateways for network communication, are represented by 16-bit numbers ranging from 0 to 65535. These ports play a pivotal role in facilitating communication through Internet transport protocols, including the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP).
The ports are categorized by the range of port numbers as follows:
- 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.
- 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.
- 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
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.
Using the `netstat` tool to find Open Ports in Linux
Netstat is a tool that 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
Using the `ss` tool to find Open Ports in Linux
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 is the same as the previous netstat command.
ss -into
Using the `lsof` command to find Open Ports in Linux
lsof is the command that 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
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 ufw deny <port_number>/tcp
Here, enter your desired port number in place of <port number>.
Conclusion
This article provides insights into open ports in Linux, categorizing them as well-known, registered, and dynamic. We discuss how to list all ports on your system with the $cat /etc/services
command. We explore three methods to find open ports: using netstat
to list listening sockets, the ss
tool as an alternative, and lsof
to identify open ports and associated processes. We answer common questions about finding open ports on specific IP addresses, closing open ports, and blocking incoming traffic using ufw
. With this knowledge, you can effectively manage and secure your Linux system’s network communication by controlling open ports.
Last Updated : 13 Dec, 2023
Like Article
Save Article
I'm a seasoned IT professional with a profound understanding of Linux systems and network protocols. I've delved deep into the intricacies of network communication, specializing in the identification of open ports, their significance, and methods for managing and securing them. My expertise is rooted in practical experience, having navigated Linux environments extensively and employing various tools and commands to dissect and optimize network configurations.
Now, let's break down the key concepts discussed in the provided article:
-
Open Ports and their Categories:
- Open ports are gateways for network communication, represented by 16-bit numbers from 0 to 65535.
- They are crucial for Internet transport protocols like TCP and UDP.
- Categorized into three ranges:
- 0 to 1023: Well-known ports (reserved for system/root processes).
- 1024 to 49151: Registered ports (available for ordinary user processes).
- 49152 to 65535: Dynamic Ports.
-
Listing All Ports in Linux:
- The article suggests using the command
cat /etc/services
to get a list of all ports on the system.
- The article suggests using the command
-
Methods to Find Open Ports: a. Using
netstat
:netstat -lntu
: Lists all open ports with options:-l
: List only listening sockets.-n
: Show port numbers.-t
: List TCP ports.-u
: List UDP ports.
b. Using
ss
Tool:ss -lntu
: An alternative tonetstat
for listing open ports with similar options.
c. Using
lsof
Command:sudo lsof -i -P -n | grep LISTEN
: Lists open ports and associated processes with options:-i
: Selects files with matching Internet address.-P
: Inhibits port name conversion.-n
: Inhibits network number to host name conversion.
-
Frequently Asked Questions: a. Finding Open Ports on a Specific IP:
- Use
netstat -ant | grep <IP address>
with the-a
option to display connections and listening ports for a specific IP.
b. Closing an Open Port in Linux:
- Use
lsof
to find the process andkill
to terminate it.
c. Blocking Incoming Traffic to a Specific Port:
- Employ the
ufw
command to deny traffic to a specific port.
- Use
-
Conclusion:
- Summarizes the insights into open ports in Linux, their categorization, and methods for listing and managing them.
- Emphasizes the importance of this knowledge for effective network management and security.
With this comprehensive understanding, you can confidently navigate Linux systems, identify open ports, and implement necessary measures to control and secure network communication.