Linux

How to Check Open Ports in Linux Using netstat, ss, and lsof

Open ports act as communication endpoints between your Linux system and the outside world. Every service running on a server whether it’s SSH, a web server, or a database; listens on a specific port. While these ports are necessary for operations, leaving unnecessary ports open can expose your system to security risks. For developers, system administrators, and cybersecurity professionals, knowing how to check open ports in Linux is an essential skill.

Regularly auditing open ports helps identify unauthorized services, troubleshoot network issues, verify firewall configurations, and reduce your attack surface. Let’s explore how to check open ports using three powerful Linux commands.

1. Using netstat to Check Open Ports

The netstat command (Network Statistics) is a traditional tool used to display network connections and listening ports.

Run the following command:

netstat -tuln

The flags used here have specific meanings. The -t option shows TCP ports, -u displays UDP ports, -l lists listening ports, and -n ensures addresses and ports are shown numerically instead of resolving hostnames.

This command provides a list of active listening ports along with their associated IP addresses. If you want to see which process is using each port, run:

sudo netstat -tulnp

The -p flag displays the process ID (PID) and the program name responsible for opening the port. Keep in mind that on many modern Linux distributions, netstat is considered deprecated and replaced by the ss command.

2. Using ss (Socket Statistics)

The ss command is a modern replacement for netstat. It is faster and more efficient, especially on systems with a large number of active connections.

To check listening ports, use:

ss -tuln

The output is similar to netstat but generated more efficiently. To display process information along with ports, run:

sudo ss -tulnp

If you need to verify whether a specific service is listening on a port, you can filter the output. For example, to check if SSH is running on port 22:

ss -tuln | grep :22

This command quickly confirms whether the port is active and listening.

3. Using lsof to Identify Processes by Port

The lsof command (List Open Files) is particularly useful when you need detailed information about which process is using a specific port.

To list all open network connections, run:

sudo lsof -i -P -n

The -i option lists network files, -P prevents port number conversion to service names, and -n avoids hostname resolution for faster results.

If you want to check a specific port, such as port 80:

sudo lsof -i :80

This is especially helpful when a service fails to start because a port is already in use. After identifying the PID, you can terminate the process if necessary:

sudo kill -9 <PID>

Use caution when killing processes, particularly on production systems.

Conclusion

Monitoring open ports in Linux is a crucial practice for maintaining system security and stability. The netstat command provides traditional visibility into network connections, ss offers a faster and modern alternative, and lsof gives detailed process-level insights. By routinely checking open ports, you can detect unnecessary services, troubleshoot network problems, and significantly reduce security risks. In Linux administration and cybersecurity, awareness of active ports is a foundational step toward building a secure and well-managed system.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

LAMP Stack Ubuntu 26.04 Installation Guide

Setting up a LAMP Stack Ubuntu server is one of the fastest ways to host…

3 hours ago

How to Change User Password in Ubuntu Quickly and Securely

Keeping your system credentials updated is one of the simplest ways to improve Linux security.…

10 hours ago

Ubuntu Server Setup Guide for Beginners in 2026

A fresh Linux VPS may look ready to use immediately, but skipping the initial security…

13 hours ago

How to Install LEMP Stack on Ubuntu 26.04 for Beginners

If you want to host dynamic PHP websites or applications like WordPress, Laravel, or Magento,…

14 hours ago

How to Install Java on Ubuntu 24.04 Easily in 2026

Java remains one of the most widely used programming platforms for servers, enterprise applications, Android…

1 week ago

How to Install DEB Files on Ubuntu in 2026 (Step-by-Step Beginner Guide)

Ubuntu users often download software directly from developer websites instead of using the default app…

1 week ago