Cloud Service >> Knowledgebase >> How To >> How to Check if a Port is Open or Closed on a Linux Server
submit query

Cut Hosting Costs! Submit Query Today!

How to Check if a Port is Open or Closed on a Linux Server

Checking whether a port is open or closed on a Linux server is crucial for network security, troubleshooting, and server management. Open ports can be potential entry points for cyber threats, while closed ports might indicate connectivity issues. By regularly monitoring ports, administrators can ensure smooth network operations, prevent unauthorized access, and optimize performance.

This guide provides step-by-step methods to check open and closed ports on a Linux server using various command-line tools. Whether you are a system administrator, developer, or IT professional, understanding these techniques will help you maintain a secure and efficient server environment.

Methods to Check Open and Closed Ports

1. Using the netstat Command

The netstat command is a powerful tool for network diagnostics and monitoring active connections on a Linux system.

To check open ports using netstat:

Open the terminal on your Linux server.

Run the following command:

nginx
CopyEdit
netstat -tulnp

  1.  

-t shows TCP connections

-u displays UDP connections

-l lists listening ports

-n displays numerical addresses instead of resolving hostnames

-p shows the process associated with the port

Review the output, which will list open ports and the services using them.

2. Using the ss Command

The ss command is a modern alternative to netstat and provides detailed socket statistics.

To check open ports with ss:

nginx

CopyEdit

ss -tulwn

-t for TCP connections

-u for UDP connections

-l for listening ports

-w for raw socket information

-n for numerical addresses

This command quickly lists open ports and their associated services.

3. Using the lsof Command

The lsof (List Open Files) command can also be used to check open ports on a Linux system.

To find open ports using lsof:

css

CopyEdit

lsof -i -P -n

-i lists network connections

-P prevents port name resolution for faster output

-n prevents hostname resolution

To check if a specific port is open, use:

css

CopyEdit

lsof -i :PORT_NUMBER

Replace PORT_NUMBER with the actual port you want to check.

4. Using the nmap Command

nmap (Network Mapper) is a popular network scanning tool. If nmap is not installed, install it first:

nginx

CopyEdit

sudo apt install nmap  # For Debian-based systems

sudo yum install nmap  # For RHEL-based systems

To scan open ports on the local server:

css

CopyEdit

nmap -p- localhost

This command scans all 65535 ports on the local system.

To check a specific port:

css

CopyEdit

nmap -p PORT_NUMBER localhost

5. Using the netcat (nc) Command

netcat (nc) is another useful tool to check whether a port is open or closed.

To check if a port is open:

nginx

CopyEdit

nc -zv localhost PORT_NUMBER

-z performs a scan without sending data

-v provides verbose output

If the port is open, you will see a successful connection message; otherwise, it will indicate the port is closed.

6. Using the telnet Command

The telnet command can verify whether a port is open by attempting a connection.

To check a port using telnet:

nginx

CopyEdit

telnet localhost PORT_NUMBER

If the connection is successful, the port is open. If it fails, the port is closed or blocked by a firewall.

How to Check for Closed Ports

A port may be closed due to firewall rules, service misconfiguration, or lack of listening services. To check for closed ports:

Use netstat, ss, or nmap and look for missing entries related to the port in question.

Check firewall rules using iptables or firewalld:

css
CopyEdit
sudo iptables -L -n -v

sudo firewall-cmd --list-all

Verify if the service bound to the port is running:

lua
CopyEdit
systemctl status SERVICE_NAME

Replace SERVICE_NAME with the actual service managing the port.

Conclusion

Regularly checking open and closed ports on a Linux server is essential for maintaining security and ensuring proper network functionality. Using tools like netstat, ss, lsof, nmap, netcat, and telnet, administrators can quickly identify active connections and potential vulnerabilities.

 

For businesses looking for secure and high-performance cloud solutions, Cyfuture Cloud offers robust hosting services with built-in security measures to safeguard your infrastructure. With reliable uptime, advanced security features, and scalable resources, Cyfuture Cloud ensures a seamless and protected server environment. Explore our cloud solutions today to enhance your business operations.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!