Back to library
Defensive Beginner Free

Audit all listening services and count active established connections

Audit all listening services and count active established connections

Linux CLI / Linux Hardening

Log in to save

Command

ss -tlnp | grep LISTEN && netstat -an | grep ESTABLISHED | wc -l

Syntax

ss -tlnp | grep LISTEN

Use case

Server hardening audit to identify and disable unnecessary network services that expand the attack surface.

Explanation

Network service auditing: • ss -tlnp → Show TCP listening sockets with process names • t → TCP only • l → Listening sockets • n → Numeric (no hostname resolution) • p → Show process name/PID • netstat -an → All connections, numeric • ESTABLISHED | wc -l → Count active sessions Use this to identify unexpected services that shouldn't be running.

Example output

State    Recv-Q  Send-Q  Local Address:Port
LISTEN   0       128     0.0.0.0:22         (sshd, pid=1234)
LISTEN   0       511     0.0.0.0:80         (nginx, pid=5678)
LISTEN   0       128     127.0.0.1:3306     (mysqld, pid=9012)
Active ESTABLISHED connections: 4

More examples

Primary example

ss -tlnp | grep LISTEN && netstat -an | grep ESTABLISHED | wc -l

Server hardening audit to identify and disable unnecessary network services that expand the attack surface.

linux ss netstat hardening audit network

Related

Defensive Intermediate Free

Hardened IPTables firewall baseline — default deny with explicit allow rules

Hardened IPTables firewall baseline — default deny with explicit allow rules

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables firewall hardening network security
IPTables / Linux Hardening Open