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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

6 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

7 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

12 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

13 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

14 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

15 hours ago