How to get current network interface bandwidth statistics on Linux / UNIX?

Tools such as MRTG provide bandwidth / network bandwidth graphs for current network usage on specific interfaces, such as eth0. How can I return this information on the command line on Linux / UNIX?

Preferably, this would be without installing anything other than what is available as standard on the system.

+81
linux networking bandwidth
Feb 27 '09 at 20:39
source share
15 answers

You can parse ifconfig output

+17
Feb 27 '09 at 20:45
source share

iftop does for network usage what top(1) does for CPU usage - http://www.ex-parrot.com/~pdw/iftop/

I don't know how a β€œstandard” iftop is, but I was able to install it using yum install iftop in Fedora.

+144
Mar 23 '09 at 17:29
source share

Got sir? Probably yes if you are using RHEL / CentOS.

No need for private binaries, hacky scripts, libpcap, etc. Win.

 $ sar -n DEV 1 3 Linux 2.6.18-194.el5 (localhost.localdomain) 10/27/2010 02:40:56 PM IFACE rxpck/s txpck/s rxbyt/s txbyt/s rxcmp/s txcmp/s rxmcst/s 02:40:57 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:57 PM eth0 10700.00 1705.05 15860765.66 124250.51 0.00 0.00 0.00 02:40:57 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:57 PM IFACE rxpck/s txpck/s rxbyt/s txbyt/s rxcmp/s txcmp/s rxmcst/s 02:40:58 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:58 PM eth0 8051.00 1438.00 11849206.00 105356.00 0.00 0.00 0.00 02:40:58 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:58 PM IFACE rxpck/s txpck/s rxbyt/s txbyt/s rxcmp/s txcmp/s rxmcst/s 02:40:59 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:59 PM eth0 6093.00 1135.00 8970988.00 82942.00 0.00 0.00 0.00 02:40:59 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: IFACE rxpck/s txpck/s rxbyt/s txbyt/s rxcmp/s txcmp/s rxmcst/s Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: eth0 8273.24 1425.08 12214833.44 104115.72 0.00 0.00 0.00 Average: eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
+96
Oct 27 '10 at 19:04
source share

I wrote this dumb script a long time ago, it depends only on Perl and Linux & ge; 2.6:

 #!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); use Time::HiRes qw(gettimeofday usleep); my $dev = @ARGV ? shift : 'eth0'; my $dir = "/sys/class/net/$dev/statistics"; my %stats = do { opendir +(my $dh), $dir; local @_ = readdir $dh; closedir $dh; map +($_, []), grep !/^\.\.?$/, @_; }; if (-t STDOUT) { while (1) { print "\033[H\033[J", run(); my ($time, $us) = gettimeofday(); my ($sec, $min, $hour) = localtime $time; { local $| = 1; printf '%-31.31s: %02d:%02d:%02d.%06d%8s%8s%8s%8s', $dev, $hour, $min, $sec, $us, qw(1s 5s 15s 60s) } usleep($us ? 1000000 - $us : 1000000); } } else {print run()} sub run { map { chomp (my ($stat) = slurp("$dir/$_")); my $line = sprintf '%-31.31s:%16.16s', $_, $stat; $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[0]) / 1) if @{$stats{$_}} > 0; $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[4]) / 5) if @{$stats{$_}} > 4; $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[14]) / 15) if @{$stats{$_}} > 14; $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[59]) / 60) if @{$stats{$_}} > 59; unshift @{$stats{$_}}, $stat; pop @{$stats{$_}} if @{$stats{$_}} > 60; "$line\n"; } sort keys %stats; } sub slurp { local @ARGV = @_; local @_ = <>; @_; } 

It just reads from /sys/class/net/$dev/statistics every second and prints the current digits and average rate of change:

 $ ./net_stats.pl eth0 rx_bytes : 74457040115259 4369093 4797875 4206554 364088 rx_packets : 91215713193 23120 23502 23234 17616 ... tx_bytes : 90798990376725 8117924 7047762 7472650 319330 tx_packets : 93139479736 23401 22953 23216 23171 ... eth0 : 15:22:09.002216 1s 5s 15s 60s ^ current reading ^-------- averages ---------^ 
+39
Feb 27 '09 at 10:40
source share

You can parse / proc / net / dev .

+23
Feb 27 '09 at 20:54
source share
  • dstat - combines vmstat, iostat, ifstat, netstat information and much more
  • iftop - An amazing network bandwidth utility to analyze what is really going on at your level
  • netio - measures network bandwidth over TCP / IP
  • inq is a CLI troubleshooting utility that displays repository information, usually Symmetrix. By default, INQ returns the device name, Symmetrix ID, Symmetrix LUN, and capacity.
  • send_arp - Sends an arp broadcast to the specified network device (eth0 by default), reporting the old and new IP address mapping to the MAC address.
  • EtherApe is a graphical network monitor for Unix modeled after etherman. Thanks to the channel level, IP and TCP mode, it graphically displays network activity.
  • iptraf is an IP traffic monitor that displays information about IP traffic passing through your network.

More details: http://felipeferreira.net/?p=1194

+21
Nov 22 '13 at 16:30
source share

nload is a great tool for real-time bandwidth monitoring and can be easily installed on Ubuntu or Debian using sudo apt-get install nload.

 Device eth0 [10.10.10.5] (1/2): ===================================================================================== Incoming: . ...| # ####| .. |#| ... #####. .. Curr: 2.07 MBit/s ###.### #### #######|. . ## | Avg: 1.41 MBit/s ########|#########################. ### Min: 1.12 kBit/s ........ ################################### .### Max: 4.49 MBit/s .##########. |###################################|##### Ttl: 1.94 GByte Outgoing: ########## ########### ########################### ########## ########### ########################### ##########. ########### .########################### ########### ########### ############################# ########### ###########..############################# ############ ########################################## ############ ########################################## ############ ########################################## Curr: 63.88 MBit/s ############ ########################################## Avg: 32.04 MBit/s ############ ########################################## Min: 0.00 Bit/s ############ ########################################## Max: 93.23 MBit/s ############## ########################################## Ttl: 2.49 GByte 

Another great tool is iftop , apt-get'able is also easy:

  191Mb 381Mb 572Mb 763Mb 954Mb └────────────┴──────────┴─────────────────────┴───────────┴────────────────────── box4.local => box-2.local 91.0Mb 27.0Mb 15.1Mb <= 1.59Mb 761kb 452kb box4.local => box.local 560b 26.8kb 27.7kb <= 880b 31.3kb 32.1kb box4.local => userify.com 0b 11.4kb 8.01kb <= 1.17kb 2.39kb 1.75kb box4.local => b.resolvers.Level3.net 0b 58b 168b <= 0b 83b 288b box4.local => stackoverflow.com 0b 42b 21b <= 0b 42b 21b box4.local => 224.0.0.251 0b 0b 179b <= 0b 0b 0b 224.0.0.251 => box-2.local 0b 0b 0b <= 0b 0b 36b 224.0.0.251 => box.local 0b 0b 0b <= 0b 0b 35b ───────────────────────────────────────────────────────────────────────────────── TX: cum: 37.9MB peak: 91.0Mb rates: 91.0Mb 27.1Mb 15.2Mb RX: 1.19MB 1.89Mb 1.59Mb 795kb 486kb TOTAL: 39.1MB 92.6Mb 92.6Mb 27.9Mb 15.6Mb 

Do not forget about the classic and powerful utilities sar and netstat on the old * nix!

+17
Apr 09 '14 at 22:32
source share

Besides iftop and iptraf, also check:

  • bwm-ng (Next Generation Bandwidth Monitoring)

and / or

  • cbm (Color bandwidth meter)

ref: http://www.powercram.com/2010/01/bandwidth-monitoring-tools-for-ubuntu.html

+15
May 7 '10 at 12:00
source share

I got another quick'n'dirty bash script for this:

 #!/bin/bash IF=$1 if [ -z "$IF" ]; then IF=`ls -1 /sys/class/net/ | head -1` fi RXPREV=-1 TXPREV=-1 echo "Listening $IF..." while [ 1 == 1 ] ; do RX=`cat /sys/class/net/${IF}/statistics/rx_bytes` TX=`cat /sys/class/net/${IF}/statistics/tx_bytes` if [ $RXPREV -ne -1 ] ; then let BWRX=$RX-$RXPREV let BWTX=$TX-$TXPREV echo "Received: $BWRX B/s Sent: $BWTX B/s" fi RXPREV=$RX TXPREV=$TX sleep 1 done 

Given that sleep 1 will last exactly one second, which is incorrect, but good enough to estimate the approximate bandwidth.

Thanks @ephemient for /sys/class/net/<interface> ! :)

+14
Jun 14 '13 at 6:45
source share

I like iptraf , but you probably have to install it and it looks like it is no longer supported.

+4
Feb 27 '09 at 20:44
source share

If you want to just get the value, you can use a simple shell oneliner like this:

 S=10; F=/sys/class/net/eth0/statistics/rx_bytes; X=`cat $F`; sleep $S; Y=`cat $F`; BPS="$(((YX)/S))"; echo $BPS 

It will show you the average β€œreceived bytes per second” for 10 seconds (you can change the period by changing the parameter S=10 , and you can measure the transmitted BPS instead of the received BPS, using tx_bytes instead of rx_bytes ). Remember to change eth0 to the network device that you want to monitor.

Of course, you are not limited to displaying average speed (as mentioned in other answers, there are other tools that will show you a much more pleasant result), but this solution can easily be scripted for other purposes.

For example, the following shell script (divided into several lines for readability) will execute the offlineimap process only if the average transfer speed of 5 minutes falls below 10 kBP (presumably when some other process with a bandwidth ends):

 #!/bin/sh S=300; F=/sys/class/net/eth0/statistics/tx_bytes BPS=999999 while [ $BPS -gt 10000 ] do X=`cat $F`; sleep $S; Y=`cat $F`; BPS="$(((YX)/S))"; echo BPS is currently $BPS done offlineimap 

Note that /sys/class/... is specific to Linux (this is normal since the sender chose the linux tag) and needs a non-archaic kernel. The shell code itself is compatible with / bin / sh (therefore, not only bash, but also dashes and other / bin / sh implementations), and / bin / sh is what is always installed.

+3
Sep 20 '14 at 13:10
source share

I think this is not bad. Must be installed though. Provides you with more information than you need. Netstat will provide you package rates, but will not be with bandwidth either. netstat -s

+2
Apr 01 '09 at 2:59
source share

You can use iperf to evaluate network performance (maximum possible bandwidth). See the following links for more details:

http://en.wikipedia.org/wiki/Iperf

https://iperf.fr/

https://code.google.com/p/iperf/

+1
Jul 28 '14 at 20:02
source share

I could not get the ifconfig script parsing to work on AMI to get it working by measuring the received traffic averaged over 10 seconds

 date && rxstart=`ifconfig eth0 | grep bytes | awk '{print $2}' | cut -d : -f 2` && sleep 10 && rxend=`ifconfig eth0 | grep bytes | awk '{print $2}' | cut -d : -f 2` && difference=`expr $rxend - $rxstart` && echo "Received `expr $difference / 10` bytes per sec" 

Sorry, this is so cheap and nasty, but it worked!

0
Nov 25 '14 at 2:39
source share
 ifconfig -a ip -d link ls -l /sys/class/net/ (physical and virtual devices) route -n 

If you want output (ifconfig -a) in json format, you can use this (python)

0
Dec 08 '14 at 1:31 on
source share



All Articles