How to check established TCP connections

Display established TCP connections inside terminal.

Use the following command to check established TCP connections using Debian based distribution:

$ ss  -o state established -t -p
Recv-Q           Send-Q                         Local Address:Port                          Peer Address:Port
0                0                                                        users:(("redis-server",pid=18324,fd=9)) timer:(keepalive,3min56sec,0)
0                0                                             users:(("chrome",pid=1131,fd=108)) timer:(keepalive,33sec,0)
0                0                                              users:(("chrome",pid=1131,fd=149)) timer:(keepalive,512ms,0)
0                0                                             users:(("firefox",pid=4015,fd=59)) timer:(keepalive,1.352ms,0)
0                0                                              users:(("chrome",pid=1131,fd=144)) timer:(keepalive,39sec,0)
0                0                                                        users:(("python",pid=1744,fd=3))
0                0                                               users:(("chrome",pid=1131,fd=284)) timer:(keepalive,7.560ms,0)
0                0                                              users:(("firefox",pid=4015,fd=50)) timer:(keepalive,6min22sec,0)

If you want nice looking output then use longer version with printf, awk and sed:

$ printf "%-15.15s %5s %20s %7.7s\n" "Process" "PID" "Destination IP" "Port" && \
  sudo ss  -o state established -t -p  | \
    sed -e 1d -e 's/^[0-9]*[ ]*[0-9]*[ ]*[0-9.]*:[a-z0-9]*[ ]*\([0-9.]*\):\([a-z0-9]*\).*users:(("\(.*\)",pid=\([0-9]*\),.*/\3\t\4\t\1\t\2/' | \
    awk '{printf "%-15.15s %5s %20s %7.7s\n",$1,$2,$3,$4}'

Example output:

Process           PID       Destination IP    Port
redis-server    18324     55548
chrome           1131   https
chrome           1131    5228
firefox          4015   https
chrome           1131   https
python           1744      6379
chrome           1131   https
firefox          4015   https

Simple as that.

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.