Viewing TCP connections with netstat

netstat -tpn shows TCP connections on your current machine. Start a TCP server with nc -l -p 12345, then connect to it with nc 12345. After this, we can see the connection:

$ sudo netstat -tpn
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0         ESTABLISHED 5441/nc
tcp        0      0         ESTABLISHED 5445/nc

Notice our one TCP connection is listed twice! We can see why by running strace on netstat: it traverses every process in /proc.

Get updates on Twitter

More by Jim

Tagged . All content copyright James Fisher 2018. This post is not associated with my employer. Found an error? Edit this page.