1. MagicESP
  2. Ubuntu Server 18.04
  3. Monitor Network Connections

Monitor Network Connections

netstat can be used to monitor network connections.

Listen Ports and Programs

sudo netstat -plnt

the output can be like this

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1076/mysqld
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1086/nginx -g daemo
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1367/sshd
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      878/postgres
tcp6       0      0 :::80                   :::*                    LISTEN      1086/nginx -g daemo
tcp6       0      0 :::22                   :::*                    LISTEN      1367/sshd

How many connections in different tcp states

netstat -an | awk '/^tcp/ {A[$(NF)]++} END {for (I in A) {printf "%5d %s\n", A[I], I}}'

the output can be like this

  413 FIN_WAIT2
    6 CLOSING
    9 LISTEN
   46 SYN_RECV
 2011 TIME_WAIT
 1811 ESTABLISHED
   13 LAST_ACK
  130 FIN_WAIT1