89

Since ifconfig is apparently being deprecated in major Linux distributions, I thought I'd learn something about the ip tool that's supposed to be used instead of ifconfig.

And here I ran into a problem: when run on its own, ifconfig shows the number of bytes received/transmitted on each interface besides other info. I couldn't find a way to get this from ip. Is there no such function in this tool? What other built-in tools could I use for getting those stats?

1

5 Answers 5

74

Another option is to use the /proc filesystem. The /proc/net/dev file contains statistics about the configured network interfaces. Each line is dedicated to one network interface and it contains statistics for receive and transmit. The statistics include metrics such total number of received/transmittted bytes, packets, drops, errors and so on.

cat /proc/net/dev Inter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed lo: 29846937 129576 0 0 0 0 0 0 29846937 129576 0 0 0 0 0 0 wlan0: 9467393340 8027251 0 0 0 0 0 0 2559312961 5896509 0 0 0 0 0 0 

Or you can try the netstat command which can display all network interfaces and related statistics:

netstat -i Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg lo 65536 0 130435 0 0 0 130435 0 0 0 LRU wlan0 1492 0 8028018 0 0 0 5897361 0 0 0 BMRU 
6
  • /proc/net/dev has the cleanest format of all of the solutions IMO, accepting. Commented Aug 25, 2013 at 15:23
  • 1
    Stats showed by cat /proc/net/dev are for how long time ? Commented Jul 18, 2014 at 10:35
  • How do you get tx/rx from /proc/net/dev ? Commented Jun 8, 2015 at 21:31
  • 1
    Try to parse 3. and 11. field of the output, something like awk '/:/ { print($1,$3, $11) }' < /proc/net/dev Commented Jun 9, 2015 at 13:04
  • netstat is deprecated along with ifconfig. On Linux, not sure about other *IX varietals. Commented Apr 30, 2021 at 18:47
86

The ip command which is part of the the iproute2 package is the new tool. The link subcommand is for managing the devices/interfaces.

If you can get the stats of an interface using ip -s link

root:~# ip -s link 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 RX: bytes packets errors dropped overrun mcast 50679705 529967 0 0 0 0 TX: bytes packets errors dropped carrier collsns 50679705 529967 0 0 0 0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:1d:7d:aa:e3:4e brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 187663757 308710386 0 0 0 0 TX: bytes packets errors dropped carrier collsns 4051284587 532435117 0 0 0 0 
3
  • 10
    While the proc answer is clean and lightweight, this answer is probably the closest to a "replacement" for the ifconfig tool because it was explicitly designed with replacement of ifconfig in mind. Commented Aug 29, 2013 at 20:40
  • 9
    ip -s link show dev eth0 to show only traffic on eth0 Commented May 9, 2018 at 11:33
  • 13
    To get this in human readable form (reporting in mb/gb/tb rather than bytes), use the -h flag. For instance, ip -h -s link. Commented Apr 25, 2019 at 10:45
63

You can get all necessary info via proc

# cat /sys/class/net/eth0/statistics/rx_bytes # cat /sys/class/net/eth0/statistics/rx_packets # cat /sys/class/net/eth0/statistics/tx_packets # cat /sys/class/net/eth0/statistics/tx_bytes 

Also you can use iptables and parse output.

For received packets

# iptables -L INPUT -n -v 

for transmitted packets

# iptables -L OUTPUT -n -v 

If server is a gateway, then you should also parse FORWARD chain

2
  • This answer is the most simple, I was able to build a script with this fairly easily. Also I use mmwatch (github.com/cloudflare/cloudflare-blog/blob/master/…) to turn the values in to rates. However, do not use iptables now, it is deprecated. Commented Aug 9, 2022 at 18:04
  • to have a single command to use when checking on servers I use grep . /sys/class/net/$(ip route show|awk '/default/{print $5}'|head -1)/statistics/*|grep -v ':0$' or - if you know the name of the nic - grep . /sys/class/net/wlp0s20f3/statistics/*|grep -v ':0$' Commented Nov 9, 2022 at 19:18
1

You can read the file /sys/class/net/wlp3s0/statistics/rx_bytes and get the rx_byes directly without calling another command, vnstat is also good. Linux stores all information in files as I know, so better to find those files and get information. Finding the relevant file is the challenge.

1
  • 2
    Welcome to the community! Well done, that's correct, but note that some old kernels don't provide these files. Wish you luck, and success! ;) Commented Mar 25, 2017 at 17:56
0

You can also use ethtool:

ethtool -S eth0 

The benefit to ethtool, is you can see each rx / tx queue and how they are balanced.

I found watch is a great tool to update the screen using all these tools. However most of these tools display traffic as bytes or packets per second. I find it difficult to convert bytes to gigs or pps to millions of packets persecond on the fly. Just a tip, check out Cloudflare's mmwatch tool. It does the same as watch, but will do the calculations automatically and display the output to the screen like watch. (Example: So you will see 1.2m/s for 1.2 million packets per-second)

https://github.com/cloudflare/cloudflare-blog/blob/master/2017-06-29-ssdp/mmwatch

Also, ethtool -S eth0 gives A lot of output, I grep the following way to show receive queues and discards also with mmwatch:

mmwatch "ethtool -S eth0 | grep 'rx-\|discard'" 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.