Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
Networking

Check open and listening ports on Linux with ss and lsof

Find which ports a Linux server listens on with ss and lsof, trace each one to a process, read the firewall, and confirm from outside what is reachable.

CloudSat teamNovember 18, 20266 min read
Abstract illustration of a row of glowing sockets on a server, some lit and some dark, with light paths leading outward
On this pageListening is not the same as reachable

"Which ports are open on this server?" has two answers, and you need both. The first is what the operating system listens on: processes that have bound a socket and wait for connections. The second is what the network can actually reach, after every firewall between the internet and that socket has had its say. A database bound to all interfaces behind a strict firewall is safe today and exposed the day someone edits a rule. This guide shows how to read each layer with standard tools and how to close what you find.

Listening is not the same as reachable

Socket binds to Firewall Reachable from the internet?
127.0.0.1 or ::1 Any No, local processes only
0.0.0.0 or [::] Allows the port Yes
0.0.0.0 or [::] Drops the port No, until the rule changes
A private address only Any Only from that network

The safest service is one that is not listening at all. The next safest is one bound to localhost. Firewall rules are the third line, not the first.

List listening sockets with ss

ss ships with iproute2 on every current distribution and replaces the older netstat. This is the command to remember:

sudo ss -tulpn
Flag Meaning
-t TCP sockets
-u UDP sockets
-l Listening sockets only
-p Show the owning process (needs root to see other users' processes)
-n Numeric ports and addresses, no name lookups

A typical web server prints something like this:

Netid State  Recv-Q Send-Q  Local Address:Port  Peer Address:Port Process
udp   UNCONN 0      0       127.0.0.53%lo:53         0.0.0.0:*    users:(("systemd-resolve",pid=612,fd=14))
tcp   LISTEN 0      4096          0.0.0.0:22         0.0.0.0:*    users:(("sshd",pid=1021,fd=3),("systemd",pid=1,fd=88))
tcp   LISTEN 0      200         127.0.0.1:5432       0.0.0.0:*    users:(("postgres",pid=1188,fd=6))
tcp   LISTEN 0      511           0.0.0.0:80         0.0.0.0:*    users:(("nginx",pid=1302,fd=6))
tcp   LISTEN 0      511              [::]:80            [::]:*    users:(("nginx",pid=1302,fd=7))

Read the Local Address column first. 0.0.0.0 and [::] mean every IPv4 or IPv6 interface. 127.0.0.1:5432 is PostgreSQL accepting local connections only, which is what you want on a single-server setup. 127.0.0.53%lo:53 is the systemd-resolved stub resolver, bound to the loopback interface and harmless.

Two details trip people up. On Ubuntu 24.04 and later, SSH is socket-activated, so systemd appears next to sshd as an owner of port 22. And a UDP socket shows the state UNCONN rather than LISTEN, because UDP has no connections.

To narrow the list, give ss a filter expression:

sudo ss -ltnp '( sport = :80 or sport = :443 )'
sudo ss -lunp 'sport = :51820'

Trace a port back to its service

ss -p gives a process name and PID. To get more context, lsof shows the command line and user, and systemctl maps a PID to the unit that started it:

sudo lsof -nP -iTCP:5432 -sTCP:LISTEN
sudo fuser -v 80/tcp
systemctl status 1188

systemctl status with a PID prints the owning unit, so you know whether to stop postgresql.service, a timer, or a container. Ports held by systemd itself appear in systemctl list-sockets, which is where you find socket-activated services that have no process running yet.

Containers publish ports in their own way

A port published with docker run -p 8080:80 shows up as docker-proxy in ss, or not at all if the userland proxy is disabled. List what Docker exposes directly:

docker ps --format 'table {{.Names}}\t{{.Ports}}'

Docker writes its own packet-filter rules for published ports, and those rules are evaluated before UFW's, so a ufw deny 8080 does not protect a published container port. When only a local reverse proxy needs to reach the container, publish it on loopback with -p 127.0.0.1:8080:80.

Read the firewall rules

Next, check what the host firewall allows. Use the tool your distribution manages:

sudo ufw status verbose          # Ubuntu, Debian with UFW
sudo firewall-cmd --list-all     # RHEL, AlmaLinux, Rocky Linux, Fedora
sudo nft list ruleset            # the raw nftables rules behind both

A provider-level firewall is invisible from inside the server. On a CloudSat server, the cloud firewall filters inbound traffic before it reaches the VM, so read its rules in the console as well. A port must be allowed in both places to be reachable.

Confirm from the outside

The final check runs from a different machine that you control, against your own server's public address. nc tests one TCP port at a time:

nc -vz 203.0.113.10 443
nc -vz 203.0.113.10 5432

For a quick sweep of your own server, nmap reports each port as open, closed or filtered:

nmap -Pn -p 22,80,443,3306,5432,6379 203.0.113.10

Scan only addresses you own or have written permission to test. Scanning other people's systems can break the law and most hosting terms.

Result What it means
open A process listens and every firewall lets the packet through
closed The host answered with a reset: firewalls pass the port, nothing listens
filtered No answer: a firewall dropped the probe

Repeat the test over IPv6 (nc -6 -vz 2001:db8::10 443). A service bound to [::] can be reachable over IPv6 even when your IPv4 rules are tight, especially with hand-written iptables rules that never covered ip6tables.

Ports you will meet often

Port Service Usually should listen on
22/tcp SSH Public, ideally restricted by source
80, 443/tcp HTTP, HTTPS Public
53/udp+tcp DNS Loopback, unless you run a resolver for others
3306/tcp MySQL, MariaDB Loopback or a private tunnel
5432/tcp PostgreSQL Loopback or a private tunnel
6379/tcp Redis, Valkey Loopback only
27017/tcp MongoDB Loopback or a private tunnel
111/tcp+udp rpcbind Nowhere, unless you serve NFS

Close what you do not need

Stop and disable a service you never use, including its socket unit when it has one:

sudo systemctl disable --now rpcbind.service rpcbind.socket

For services you need locally, bind them to loopback in their own configuration: listen_addresses = 'localhost' in postgresql.conf, bind-address = 127.0.0.1 for MySQL and MariaDB, bind 127.0.0.1 -::1 for Redis and Valkey. Restart the service and run sudo ss -tulpn again to confirm the address changed. Re-run the outside test after every firewall edit, because a rule that looks right in a config file can still be shadowed by an earlier one.

Make it a routine check

New listeners appear quietly: a package pulls in a daemon, a container image exposes an admin port, a developer starts a debug server and forgets it. Record a baseline once the server is in the state you want, then compare against it on a schedule:

sudo ss -Htuln | awk '{print $1, $5}' | sort -u | sudo tee /root/ports.baseline
sudo ss -Htuln | awk '{print $1, $5}' | sort -u | diff /root/ports.baseline -

The -H flag drops the header line, and the awk step keeps only the protocol and local address, so the comparison ignores PIDs that change on every restart. Run the second line from a daily cron job or systemd timer and send any output to your alerting channel. An empty result means nothing new is listening.

FAQ

Should I still use netstat?

It works if the net-tools package is installed, but many minimal images no longer include it. ss reads the same kernel data faster and supports filters, so new scripts should use it.

Why does ss show a port that my firewall blocks?

Because ss reports sockets, not reachability. The process listens regardless of firewall rules. Confirm exposure with an outside test, and prefer binding internal services to localhost so a future rule change cannot expose them.

What is listening on 127.0.0.53?

The systemd-resolved stub resolver. Local programs send DNS queries there and it forwards them to your configured upstream servers. It never accepts traffic from other machines.

How do I list listening ports on a Windows server?

In PowerShell, run Get-NetTCPConnection -State Listen and add | Sort-Object LocalPort for a readable list. netstat -ano also works and shows the PID, which you can match in Task Manager or with Get-Process -Id.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.