Need to quickly check which ports are open on your Linux server? Or perhaps you want to scan your network for vulnerabilities? Discover proven methods and tools — from classic to modern — complete with practical examples and comparisons.
Checking open ports is one of the fundamental tasks for any Linux system administrator. Whether you are managing a server, working in a corporate network, or simply want to understand what services are running on your computer, knowing port diagnostics tools is essential.
In this guide, I will cover the most important tools available in Linux systems: ss, netstat, nmap, as well as alternatives like lsof or reading directly from /proc/net/tcp. You will learn which ones are modern, which have been deprecated, and what their limitations are. Everything is backed by practical examples that you can try out on your system right away.
Why Should You Know Port Checking Tools?
Open ports are the gateways to your system. On the one hand, they are essential for services like SSH, HTTP, FTP, or databases to function. On the other hand, improperly configured or unused ports can pose a security risk. Knowing the state of your ports allows you to:
- detect suspicious connections or open services,
- diagnose application availability issues,
- verify firewall configuration,
- optimize system performance by closing unused ports.
Tool Comparison: ss vs netstat vs nmap
Each of these tools has its place. Below is a detailed comparison of their functionality, performance, and typical use cases.
1. ss — The Modern Successor to netstat
ss (Socket Statistics) is a utility from the iproute2 package designed to replace the legacy netstat. It is available in most modern Linux distributions and runs significantly faster because it reads information directly from the kernel rather than from /proc files.
Basic Options:
-t— displays TCP ports,-u— displays UDP ports,-l— shows listening ports only,-n— disables DNS name resolution (faster),-p— shows processes using the ports (requiressudoprivileges),-a— shows all ports (all connection states).
Typical Usage:
ss -tulnp
This command will display all open TCP and UDP ports on the system, along with port numbers, states, and process names.
Practical Examples:
- Check if port 22 (SSH) is open and which process is using it:
sudo ss -tulnp | grep ':22' - Display listening ports only:
ss -l - Show TCP connection statistics:
ss -s
2. netstat — The Classic Fading into Obscurity
netstat (Network Statistics) is an older utility from the net-tools package that was the standard for network diagnostics for many years. It is now gradually being phased out in favor of ss, especially in newer distributions.
Basic Options:
-t— TCP,-u— UDP,-l— listening,-n— port numbers (no DNS),-p— processes,-s— connection statistics,-r— routing table.
Typical Usage:
netstat -tulnp
This command works similarly to ss -tulnp, but slower and with higher overhead.
Practical Examples:
- Show all active TCP connections:
netstat -t - Display detailed TCP statistics:
netstat -s - Show the routing table:
netstat -r
Limitations:
- Slower than
ss(reads data from/procfiles). - In some newer distributions (e.g., Ubuntu 22.04, Fedora 36+), the
net-toolspackage is not installed by default. - Not recommended for new projects — ss is its direct successor.
If you are using an older distribution, such as centos 7 or Debian 9, netstat still works, but it is worth getting used to ss to prepare for future changes.
3. nmap — A Powerful Port Scanning Tool
nmap (Network Mapper) is not just a port checker — it is a powerful network scanner that allows you to identify open ports both locally and on remote hosts. It is widely used in network security, auditing, and troubleshooting.
Basic Options:
-sT— TCP connect scan (full connection),-sS— TCP SYN scan (fast, stealthy),-p— specify ports to scan (e.g.,-p 80,443),-Pn— skip host discovery (faster, but less accurate),-A— enables OS and service version detection.
Typical Usage:
- Scan the 1000 most popular ports on the local host:
nmap -sT -p- localhost - Scan port 80 on a remote server:
nmap -p 80 8.8.8.8 - Scan with service version detection:
nmap -sV -p 22,80,443 192.168.1.1
Advantages:
- Can scan ports on remote machines.
- Provides detailed information about service versions (e.g., "Apache 2.4.52").
- Supports various scan types (TCP, UDP, SYN, etc.).
- Available on all major platforms (Linux, Windows, macOS).
Limitations:
- Requires installation (not available by default in most distributions).
- May be blocked by firewalls or flagged as suspicious activity (e.g., in corporate networks).
- Slower than
ssfor local diagnostics (primarily designed for remote scanning).
If you need to scan a network for vulnerabilities or verify which ports are open on a remote server, nmap is indispensable.
Alternative Port Checking Methods
Aside from classic tools, there are less known, but sometimes very useful, port diagnostic methods.
1. The /proc/net/tcp File — Raw Kernel Data
The Linux kernel stores TCP connection information in the /proc/net/tcp file. You can read it directly, although the format is less readable than ss or netstat.
File Format: Each line represents a single TCP connection in the format:
sl lokalny_adres:port zdalny_adres:port stan ...
e.g.:
0: 0A000001:0016 00000000:0000 0A 00000000:00000000
How to Read Connection States:
0A– LISTEN state,01– ESTABLISHED state,05– CLOSE_WAIT state.
Example:
cat /proc/net/tcp | awk '{print $2}' | sort -u
The above command will display unique local addresses and listening ports.
Drawbacks:
- TCP connections only (no UDP information).
- Requires knowledge of the data format.
- Does not show processes or service names.
2. lsof — List Open Files, Including Network Sockets
lsof (List Open Files) is a tool that displays all open files on the system, including network sockets. It is useful when you want to see which processes are using specific ports.
Basic Options:
-i— shows network sockets only,-P— disables port name resolution (e.g., shows "80" instead of "http"),-n— disables IP address to DNS name resolution,-l— shows local connections only,-u— filters by user.
Typical Usage:
sudo lsof -i -P -n | grep LISTEN
The above command will show all listening ports along with processes and users.
Practical Examples:
- Check which process is using port 3306 (MySQL):
sudo lsof -i :3306 - Display all TCP connections:
sudo lsof -i TCP
Advantages:
- Very detailed process information.
- Can show which user started the process.
- Available in most distributions (often pre-installed).
Drawbacks:
- Slower than
sswhen handling large amounts of data. - Requires administrator privileges (
sudo).
When to Use Which Tool? A Complete Selection Guide
The table below will help you quickly choose the right tool depending on your goal.
| Diagnostic Goal | Recommended Tool | Example Command |
|---|---|---|
| Quick check of local listening ports | ss |
ss -tulnp |
| Checking active TCP/UDP connections | ss or lsof |
ss -s or sudo lsof -i |
| Scanning ports on a remote host | nmap |
nmap -sT -p 1-1000 192.168.1.1 |
| Reading raw TCP connection data | /proc/net/tcp file |
cat /proc/net/tcp |
Older systems (lack of ss) |
netstat |
netstat -tulnp |
| Detailed info on processes using ports | lsof |
sudo lsof -i -P -n |
Linux Distributions and Tool Availability
The availability of port-checking tools depends on the Linux distribution and its age. Below is an overview of the most popular systems.
Ubuntu (Latest Versions)
In Ubuntu 22.04 LTS and newer:
- ss is installed by default (the
iproute2package). - netstat is not installed by default — to use it, install the
net-toolspackage:sudo apt install net-tools - nmap is not installed by default — installation:
sudo apt install nmap
centos / RHEL
In centos 7 and RHEL 7:
- ss and netstat are available (
iprouteandnet-toolspackages). - nmap requires installation:
sudo yum install nmap
In centos Stream 9 and newer:
- net-tools is not installed by default — using ss is recommended.
- nmap available via
dnf:sudo dnf install nmap
Arch Linux / Manjaro
In Arch Linux and derivatives:
- ss is installed by default (the
iproute2package). - netstat is not installed by default — installation:
sudo pacman -S net-tools - nmap available via:
sudo pacman -S nmap
Debian
In Debian 11 and newer:
- ss available by default.
- netstat requires installing the
net-toolspackage. - nmap installed via:
sudo apt install nmap
Permissions and Limitations — What to Watch Out For
When using port diagnostics tools, it is worth keeping in mind a few key issues regarding permissions and system limitations.
1. Administrator Privileges
All tools require administrator privileges (sudo) to display information about processes (e.g., PID, program name) using ports. Without sudo, you will only see port numbers and connection states.
Example:
# Bez sudo — tylko numery portów
ss -tuln
# Z sudo — także nazwy procesów
sudo ss -tulnp
2. Firewalls and Network Blocks
If a local firewall (e.g., ufw, firewalld, or iptables) blocks ports, tools like ss or netstat may not show them as open, even if the service is listening. Remember to check your firewall configuration:
- Ubuntu:
sudo ufw status - centos/RHEL:
sudo firewall-cmd --list-all - Check iptables rules:
sudo iptables -L -n
3. SELinux and AppArmor
Systems with security mechanisms enabled, such as SELinux (RHEL/centos) or AppArmor (Ubuntu/Debian), can restrict tools from accessing process information. This may result in errors when running ss -tulnp or lsof -i.
Solution:
- Check SELinux status:
getenforce - Check AppArmor profiles:
sudo aa-status - If necessary, adjust policies or run tools with appropriate privileges.
4. Package Dependencies
Some tools, especially nmap, may require additional packages (e.g., libpcap for network scanning). If errors occur during installation, check dependencies:
- Ubuntu/Debian:
sudo apt install -f - centos/RHEL:
sudo yum deplist nmap
Summary: Which Tool Should You Choose?
Choosing the right tool depends on your goal and environment:
- For quick, modern local diagnostics:
ss— the best choice across all new distributions. - For older systems or backward compatibility:
netstat(though migrating tossis recommended). - For scanning remote hosts or security audits:
nmap— indispensable in networks. - For detailed process information:
lsof— when you need to know which user or application is using a specific port. - For low-level debugging: The
/proc/net/tcpfile — when you need raw kernel data.
If you are just starting your journey with Linux, I recommend starting with ss and nmap. They are modern, efficient, and widely used in the industry. Meanwhile, netstat is only worth knowing for legacy systems or historical context.
Common Issues and How to Solve Them
While working with port checking tools, you may encounter a few common problems. Here is how to solve them.
1. The ss -tulnp Command Does Not Show Processes
Problem: The command returns port numbers, but not process names.
Cause: Lack of administrator privileges or missing process information in the kernel.
Solution:
sudo ss -tulnp
If it still doesn't work, check if the kernel exposes this information:
cat /proc/sys/net/core/somaxconn
2. nmap Returns "Host Seems Down" Even Though the Host is Active
Problem: Nmap cannot scan the host, even though it is visible on the network.
Cause: The firewall is blocking ICMP (ping) packets, or nmap tries to ping the host before scanning by default.
Solution: Use the -Pn flag to skip the ping test:
nmap -Pn -p 80 192.168.1.1
3. lsof Does Not Work or Returns Errors
Problem: The sudo lsof -i command does not work or returns "command not found".
Cause: The lsof package is not installed or the kernel does not provide the appropriate information.
Solution: Install the package:
sudo apt install lsof # Ubuntu/Debian
sudo yum install lsof # CentOS/RHEL
sudo pacman -S lsof # Arch
4. Tools Do Not Show Ports That Should Be Open
Problem: The service is running, but the tools do not detect the open port.
Possible causes:
- The service is listening only on the loopback interface (
127.0.0.1) rather than the global IP address. - The firewall is blocking the port.
- The service was not started with administrator privileges.
Solution:
- Check which interface the service is listening on:
ss -tulnp | grep '0.0.0.0:' - Check the firewall configuration.
- Run the service with appropriate privileges.
Security: Which Ports Should Be Open?
While port diagnostics are important, it is equally vital to know which ports should be open and which are better closed. Here are a few practical tips.
Ports That Should Usually Be Open:
- 22 (SSH) — for remote server management (make sure you use SSH keys, not passwords).
- 80 (HTTP) and 443 (HTTPS) — for websites.
- 3306 (MySQL) — if you need remote database access (best restricted to specific IPs).
- 25 (SMTP) — if you are hosting your own mail server.
Ports That Should Be Closed (or Secured):
- 21 (FTP) — insecure, better to use SFTP (port 22).
- 23 (Telnet) — completely unencrypted, dangerous.
- 3389 (RDP) — Remote Desktop, vulnerable to brute-force attacks (secure it additionally).
- Loosely configured ports above 1024 — frequently used by malware.
Always apply the principle of least privilege: open only those ports that are essential for your services to function.
Example Scenario: Troubleshooting Connection Issues
Imagine your web application isn't working, and you suspect the problem lies with the ports. How do you troubleshoot?
Step 1: Check If the Port Is Listening
sudo ss -tulnp | grep ':80'
If you see nothing, it means the web server (e.g., Apache or Nginx) is not listening on port 80.
Step 2: Check If the Service Is Running
systemctl status apache2 # lub nginx, a zależnie od systemu
Step 3: Check the Firewall
sudo ufw status # Ubuntu
sudo firewall-cmd --list-all # CentOS/RHEL
If the firewall is blocking port 80, add a rule:
sudo ufw allow 80/tcp
Step 4: Check If the Application Is Listening on the Correct Interface
Sometimes a service listens only on 127.0.0.1 rather than the global IP address. Check the configuration in the application file (e.g., /etc/nginx/sites-enabled/default or /etc/apache2/ports.conf).
Step 5: Scan the Port Remotely
To check if port 80 is visible from the outside, use nmap:
nmap -Pn -p 80 twoj-server-ip
If all steps show that the port is open and the application still doesn't work, the problem may lie within the application itself or its configuration.
Summary: Your Port Diagnostics Checklist
To make your work easier, I have prepared a short checklist that you can print and keep handy during port diagnostics.
- Quick local diagnostics:
sudo ss -tulnp— list open ports and processes.ss -s— connection statistics.
- Remote scanning:
nmap -sT -p 1-1000 twoj-server-ip— port scanning.nmap -sV -p 22,80,443 twoj-server-ip— scanning with service version detection.
- Checking processes:
sudo lsof -i -P -n— list open sockets.sudo lsof -i :80— which process is using port 80.
- Low-level data:
cat /proc/net/tcp— raw kernel TCP connection data.
- Firewall and security:
sudo ufw status— check firewall on Ubuntu.sudo firewall-cmd --list-all— check firewall on centos/RHEL.
Remember that port diagnostics are only part of a broader process of maintaining system security and stability. Always combine it with log reviews, performance monitoring, and regular software updates.
Further Reading and Tools to Expand Your Knowledge
If you want to deepen your knowledge of Linux networking, I recommend the following resources:
- UFW — Ubuntu Firewall — firewall documentation on Ubuntu.
- Configuring Firewalls — RHEL Documentation — firewall guide for RHEL/centos.
- Nmap Official Documentation — full
nmapdocumentation with examples. - Linux Networking Documentation — official kernel networking documentation.
- Rocky Linux — centos Successor — comparison of new RHEL-based distributions.
Sources
- https://www.tecmint.com/check-open-ports-linux-without-telnet/
- https://wiki.archlinux.org/title/Network_configuration#netstat
- https://packages.ubuntu.com/
- https://www.linux.org/threads/ss-vs-netstat.24746/
- https://nmap.org/book/
- https://man7.org/linux/man-pages/man8/ss.8.html
- https://man7.org/linux/man-pages/man8/netstat.8.html
- https://man7.org/linux/man-pages/man1/lsof.1.html
- https://packages.ubuntu.com/jammy/net-tools
- https://centos.pkgs.org/7/centos-x86_64/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm.html
Comments