In Linux, ports play a crucial role in network communication. Every service on a Linux system listens on a specific port, allowing other devices or applications to connect. However, you may need to check whether a particular port is open or closed.
This is essential for troubleshooting network issues, ensuring proper firewall configurations, and securing your system from unauthorised access.
In this guide, we will explore different methods to check whether a port is open in Linux. These methods include using built-in Linux utilities like netstat, ss, nc, telnet, and nmap. Each method serves different purposes, and you can choose the one that best fits your requirements.
Also Read: How to Install RPM files on Different Linux Distributions?
Methods to Check Open Ports in Linux
1. Using netstat Command
netstat is a powerful networking tool that provides information about network connections, routing tables, and port usage.
Command:
netstat -tulnp | grep :PORT_NUMBER |
Example:
If you want to check if port 80 is open:
netstat -tulnp | grep :80 |
Explanation:
-t -> Show TCP ports
-u -> Show UDP ports
-l -> Show listening ports
-n -> Show numerical addresses instead of resolving names
-p -> Show the process using the port
*Note: If netstat is not available, install it using the following:
sudo apt install net-tools # For Debian-based systems sudo yum install net-tools # For RHEL-based systems |
Also Read: Create Zip File With Command in Linux With Examples
2. Using ss Command
ss Command is a faster and more efficient alternative to netstat. Below is the command to check the port in Linux.
Command:
ss -tuln | grep :PORT_NUMBER |
Example:
If you want to check if port 22 is open:
ss -tuln | grep :22 |
Explanation:
-t -> Show TCP ports
-u -> Show UDP ports
-l -> Show listening ports
-n -> Show numerical addresses
Also Read: A Guide to Echo Command in Linux With Examples
3. Using nc (netcat) Command
nc (netcat) is a versatile networking utility that can check whether a port is open by attempting a connection.
Command:
nc -zv HOSTNAME PORT_NUMBER |
Example:
If you want to check if port 433 is open on yourwebsite.com:
nc -zv yourwebsite.com 443 |
Explanation:
-z -> Scan mode (don’t send data, just check connection)
-v -> Verbose mode (shows detailed output)
Expected Output:
If the port is open, you will get this message: Connection succeeded!
If the port is closed, you will get this message: Connection refused
*Note: Install netcat if not available:
sudo apt install netcat # Debian-based sudo yum install nc # RHEL-based |
Also Read: How To Remove Directory in Linux With Command?
4. Using telnet Command
The telnet command can also check if a port is open by attempting a connection.
Command:
telnet HOSTNAME PORT_NUMBER |
Example:
telnet example.com 25 |
Output:
You will see a “successful connection” message if the port is open.
If the port is closed, you will see “Connection refused.”
*Note: If telnet is not installed, install it using:
sudo apt install telnet # Debian-based sudo yum install telnet # RHEL-based |
Also Read: What is Telnet Command in Windows & How to Use it?
5. Using nmap Command
nmap is a powerful network scanning tool that can check for open ports.
Command:
nmap -p PORT_NUMBER HOSTNAME |
Example:
If you want to check if port 3306 is open on yourwebsite.com:
nmap -p 3306 example.com |
Explanation:
-p -> Specifies the port number to scan
*Note: Install nmap if not available:
sudo apt install nmap # Debian-based sudo yum install nmap # RHEL-based |
Conclusion
Checking whether a port is open in Linux is crucial for troubleshooting and maintaining network security. Multiple ways exist, including netstat, ss, nc, telnet, and nmap. These commands provide useful information about open ports, listening services, and network connectivity.
If a port unexpectedly closes, you may need to check firewall rules (iptables or firewalld), ensure the corresponding service runs, or verify network configurations. Regular monitoring of open ports helps prevent unauthorized access and enhances the security of your Linux system.
Using these simple commands, you can efficiently diagnose port-related issues and keep your network running smoothly.
Also Read: How to Change Server Time in Linux?
