How to Check Active Web Server Connections in Linux with ss

Quick Linux Tip #69:

Question: How many active connections does my web server have right now?

Try: sudo ss -tn state established '( sport = :80 )' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn

Info: ss -tn lists TCP connections using numeric addresses and ports. Filtering for ESTABLISHED connections on local port 80 isolates active HTTP connections, while awk, cut, sort, and uniq count connections by remote IP address.

Examples:

  • $ sudo ss -tn state established '( sport = :443 )' | wc -l  # Count established HTTPS connections
  • $ sudo netstat -an | grep :80 | grep ESTABLISHED | wc -l  # Count HTTP connections with legacy netstat
  • $ sudo lsof -iTCP:80 -sTCP:ESTABLISHED  # View processes using established HTTP sockets

Note: ss is generally faster and more capable than the legacy netstat. An unusually high number of connections from one IP can indicate aggressive scraping, a misbehaving client, or a possible denial-of-service attack. For IPv6 addresses, avoid simple cut -d: -f1 parsing because IPv6 addresses themselves contain colons.




LinuxTeck.com
linuxteck@ubuntu:~$ sudo ss -tn state established '( sport = :80 )' | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn
     45 203.0.113.100
     23 198.51.100.50
     18 192.0.2.200
     12 203.0.113.155
      8 198.51.100.99
      5 192.0.2.75
      3 203.0.113.44
      2 198.51.100.201
      1 192.0.2.234

linuxteck@ubuntu:~$ sudo ss -tn state established '( sport = :80 )' | wc -l
117
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #68: How to Extract PDF Pages from the Linux Command Line NEXT ARTICLE Quick Linux Tip #70: Run Daily Tasks with Linux systemd Timers
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.