On this page
- Which tool should you use, ss or netstat?
- How do you list listening ports with their programs?
- How do you tell public services from local ones?
- How do you check which program uses a given port?
- How do you list active connections?
- How do you count connections per client address?
- What are the netstat equivalents?
- What should you do when a port is not listening?
- Questions
Which tool should you use, ss or netstat?
Use ss. It ships with iproute2 on every current image, reads socket data straight from the kernel through netlink and stays fast even with tens of thousands of connections. netstat belongs to the older net-tools package, which is no longer installed by default on Ubuntu, Debian, Rocky Linux or AlmaLinux. It still works and many guides mention it, so this page shows both, but scripts and daily checks are better written with ss. Confirm it is present:
ss -VIf you prefer to keep netstat around for habit, install it with sudo apt install net-tools on Ubuntu and Debian, or sudo dnf install net-tools on Rocky and Alma.
How do you list listening ports with their programs?
Combine the flags -t (TCP), -u (UDP), -l (listening only), -p (process) and -n (numeric ports, no name lookups). Run it with sudo, otherwise the process column stays empty for services owned by other users:
sudo ss -tulpnA typical result on a fresh web server looks like this:
Netid State Local Address:Port Peer Address:Port Process
udp UNCONN 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=512,fd=13))
tcp LISTEN 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=845,fd=3))
tcp LISTEN [::]:22 [::]:* users:(("sshd",pid=845,fd=4))
tcp LISTEN 127.0.0.1:5432 0.0.0.0:* users:(("postgres",pid=990,fd=6))
tcp LISTEN *:443 *:* users:(("caddy",pid=1102,fd=7))Here SSH answers on both IPv4 and IPv6, PostgreSQL only on localhost, and Caddy on every address for both families. The local DNS stub on 127.0.0.53 is part of systemd-resolved on Ubuntu and is not reachable from outside.
How do you tell public services from local ones?
Look at the address before the port. It decides who can connect, independently of the firewall:
0.0.0.0means all IPv4 addresses, so the port is public on IPv4.[::]or*means all IPv6 addresses, and often IPv4 too when the program uses a dual stack socket.127.0.0.1and[::1]mean loopback only; nothing outside the server can reach it.- A specific address such as
198.51.100.24means only that one address.
Databases, caches and admin panels should normally show a loopback address. If Redis, MySQL or a dashboard appears on 0.0.0.0, either change its bind setting or make sure your firewall blocks the port, and preferably both. The firewall rules tutorial and the ufw guide show how to close everything except the ports you mean to publish. To verify from outside, scan your own server with nmap.
How do you check which program uses a given port?
Add a filter expression after the flags. ss understands source and destination ports with a short syntax:
sudo ss -tlnp 'sport = :80'
sudo ss -tlnp '( sport = :80 or sport = :443 )'
sudo ss -ulnp 'sport = :51820'This is the quickest way to solve "address already in use" errors: the output names the process and its PID that already holds the port. Stop that service with sudo systemctl stop NAME or change one of the two programs to another port. As an alternative, sudo lsof -i :80 answers the same question if lsof is installed.
How do you list active connections?
Drop -l and add a state filter. The commands below show established TCP connections, then only those to or from the HTTPS port:
ss -tn state established
ss -tn state established '( sport = :443 or dport = :443 )'
ss -tan | awk 'NR>1 {print $1}' | sort | uniq -cThe last line counts sockets per state, which helps spot problems: thousands in TIME-WAIT are normal on a busy web server, while a growing pile of SYN-RECV points at a flood of half open connections and many CLOSE-WAIT entries usually mean an application forgets to close its sockets. For a short overall summary use ss -s.
How do you count connections per client address?
Print the peer address column without a header, strip the port, and let sort and uniq do the counting:
ss -Htn4 state established | awk '{sub(/:[0-9]+$/, "", $4); print $4}' | sort | uniq -c | sort -rn | head
ss -Htn6 state established | awk '{sub(/:[0-9]+$/, "", $4); print $4}' | sort | uniq -c | sort -rn | headWith a state filter, ss omits the State column, so the peer address and port sit in the fourth field. One address holding hundreds of connections might be a crawler, a misbehaving client or an attack; combine this with your web server logs before blocking anything. Tools like fail2ban can automate blocks based on log patterns.
What are the netstat equivalents?
Most netstat habits translate directly, because ss kept the same letters on purpose:
sudo netstat -tulpn # ss -tulpn
netstat -tan # ss -tan
netstat -s # nstat -az (or ss -s for a short summary)
netstat -rn # ip route
netstat -i # ip -s linkThe output layout differs slightly: netstat prints the state at the end of each line and shows the program as PID/name, while ss puts the state first and the process in a users:(...) field. When you read older documentation, the flags carry over; only the routing and interface views moved to the ip command.
What should you do when a port is not listening?
When a client cannot connect, work from the inside out. First confirm the service runs and owns the port:
systemctl status nginx --no-pager
sudo ss -tlnp 'sport = :443'
sudo journalctl -u nginx -n 30 --no-pagerIf nothing listens, read the service log for the reason (a config error or a port already taken). If it listens only on 127.0.0.1, change its bind address. If it listens publicly but outsiders still cannot connect, the firewall is the next suspect; check sudo ufw status or sudo firewall-cmd --list-all. Finally test from another machine with netcat to see whether the port answers over the network.
Questions about Listening ports with ss and netstat
Why is the process column empty?
You ran ss without sudo. A normal user can only see process details for its own sockets. Repeat the command with sudo.
What does UNCONN mean for UDP?
UDP has no connections, so a UDP socket waiting for packets shows UNCONN instead of LISTEN. It is the normal state for DNS, WireGuard and similar services.
Does a listening port mean it is open to the internet?
Not necessarily. The firewall still decides whether packets reach it. A public bind address plus an allow rule makes it reachable; a loopback bind address never is.
How can I watch connections change live?
Wrap the command in watch, for example watch -n 2 "ss -s". For bandwidth per connection or per process, see the iftop and nethogs guide.
Is there an ss equivalent on FreeBSD?
FreeBSD uses sockstat -4 -6 -l to list listening sockets with their owners, and its own netstat remains part of the base system.
More in Networking and diagnostics
- FreeRADIUS with PostgreSQL
- IPv6 on your server
- The ip command
- DNS lookups with dig
- Test ports and transfer data with netcat
- Trace network paths with mtr
- Capture packets with tcpdump
- Live bandwidth with iftop and nethogs
- HTTP requests with curl
- Dynamic DNS updates from a server
- Latency graphs with SmokePing
- Traffic analysis with ntopng
- Check which ports are reachable