How to monitor network connections on Linux with iftop

iftop is a very useful command that can help us troubleshoot network issues and show statistics about download speed and network connections

to install it on ubuntu

sudo apt-get -y install iftop

Get a list of network connections and info for download / upload

sudo iftop -n -P -t -s1 -L1000
interface: wlp108s0
IP address is: 192.168.1.168
IPv6 address is: 2a02:587:e3e:d700:cc8a:4e6f:447d:4f4a
MAC address is: 34:e1:2d:55:ea:ec
Listening on wlp108s0
# Host name (port/service if enabled) last 2s last 10s last 40s cumulative
--------------------------------------------------------------------------------------------
1 [2a02:587:e3e:d700:cc8a:4e6f:447d::45536 => 76,4Kb 76,4Kb 76,4Kb 19,1KB
[2606:4700:30::681b:bb88]:http <= 4,31Mb 4,31Mb 4,31Mb 1,08MB
--------------------------------------------------------------------------------------------
Total send rate: 76,4Kb 76,4Kb 76,4Kb
Total receive rate: 4,31Mb 4,31Mb 4,31Mb
Total send and receive rate: 4,39Mb 4,39Mb 4,39Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total): 76,4Kb 4,31Mb 4,39Mb
Cumulative (sent/received/total): 19,1KB 1,08MB 1,10MB
============================================================================================

Input parameter explaination:

-n: do no resolve ip addresses-P: display ports-t: plain text output-s<n> n the number of seconds to sample traffic-L<n> n the max number of lines to display

Output explaination per entry:

1st line of hostname/port: an ip/port from our server'=>' The speed we upload for the last 2s, 10s, 40s and cumulative2nd line of hostname/port: an ip/port from the remote server'<=' The speed we download for the last 2s,10s,40s and cumulative

As you nottice appart from each connection entry we can get cumulative statistics about totals send — recieve rate, peak rate and cumulative sent/receive for the sampling period

--------------------------------------------------------------------------------------------
Total send rate: 76,4Kb 76,4Kb 76,4Kb
Total receive rate: 4,31Mb 4,31Mb 4,31Mb
Total send and receive rate: 4,39Mb 4,39Mb 4,39Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total): 76,4Kb 4,31Mb 4,39Mb
Cumulative (sent/received/total): 19,1KB 1,08MB 1,10MB
============================================================================================

This tool is usable when you want to troubleshoot something on the fly, what about historical data? how we can use this tool to log them? see this article

https://medium.com/@kpatronas/iftop-how-to-log-network-connections-for-troubleshooting-d6b6115f3486

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store