How to List All Network Connections and Open Ports in Debian 12 Bookworm

Learn how to list all network connections and open ports in Debian 12 Bookworm.

Debian 12 “Bookworm” is a stable and secure Linux distribution widely used for servers and desktops. Understanding how to list all network connections and open ports is crucial for system administration, security auditing, and troubleshooting. This guide will provide step-by-step instructions on how to check active network connections and open ports using various command-line utilities.

Why List Network Connections and Open Ports?

Monitoring network connections and open ports helps system administrators to:

  • Detect unauthorized access and potential security threats.
  • Monitor active services to ensure proper functionality.
  • Diagnose network-related issues and troubleshoot connectivity problems.
  • Optimize firewall rules by identifying open and unused ports.

Methods to List Network Connections and Open Ports

There are several commands available in Debian 12 to list network connections and open ports:

The ss (socket statistics) command is a modern replacement for netstat, offering faster and more detailed output. It is pre-installed in Debian 12.

List All Listening Ports

ss -tuln

Explanation:

  • -t : Show TCP connections.
  • -u : Show UDP connections.
  • -l : Show only listening ports.
  • -n : Show numerical addresses instead of resolving hostnames.

List All Active Connections

ss -tanp
  • -p : Display the process using the connection.

Display Detailed Statistics

ss -s

This provides a summary of open sockets by protocol type.

2. Using netstat (Legacy Command)

Although netstat has been deprecated in favor of ss, it is still available in the net-tools package.

Install net-tools (if not installed)

sudo apt update && sudo apt install net-tools

Show All Listening Ports

netstat -tulnp

3. Using lsof (List Open Files)

The lsof command can be used to list open network connections along with their associated processes.

Install lsof

sudo apt install lsof

List Open Network Connections

lsof -i

Show Specific Port Usage

lsof -i :80

This checks for processes using port 80 (HTTP).

4. Using nmap for Port Scanning

The nmap tool is useful for scanning open ports and auditing network security.

Install nmap

sudo apt install nmap

Scan Open Ports on Local Machine

nmap -p- localhost

This scans all 65535 ports on localhost.

5. Using proc Filesystem

Linux provides network details in the /proc filesystem.

View Active TCP Connections

cat /proc/net/tcp

View Active UDP Connections

cat /proc/net/udp

6. Using iptables (If Used as Firewall)

If iptables is managing your firewall, you can list open ports:

sudo iptables -L -n -v

For nftables, use:

sudo nft list ruleset

Conclusion

Understanding how to list network connections and open ports is essential for maintaining the security and functionality of a Debian 12 system. The ss command is the most efficient choice, but other tools like netstat, lsof, and nmap offer additional insights. Regularly monitoring open ports helps mitigate security risks and optimize system performance.