As Linux distributions increasingly deprecate ifconfig
in favor of iproute2
utilities, many sysadmins and developers find themselves needing alternative ways to access crucial network statistics. The traditional ifconfig
output conveniently displayed transmitted (TX) and received (RX) bytes, but this information isn't as readily available with the ip
command alone.
Here are several robust methods to retrieve interface statistics in modern Linux systems:
Method 1: Using ip -s
The ip
command can show statistics with the -s
flag:
ip -s link show dev eth0
This outputs detailed statistics including RX/TX bytes, packets, errors, and more. For cleaner output showing only bytes:
ip -s -h link show dev eth0 | grep -A1 "RX\|TX"
Method 2: /sys Filesystem
The kernel exposes network statistics in /sys/class/net/
:
cat /sys/class/net/eth0/statistics/rx_bytes
cat /sys/class/net/eth0/statistics/tx_bytes
This method is particularly useful for scripting and monitoring tools.
Method 3: ss Utility
While primarily for socket statistics, ss
can provide interface-level data:
ss -i
Method 4: netstat (Still Available on Many Systems)
Though also deprecated on some systems, netstat
remains available:
netstat -i
For monitoring scripts, here's a Python example using the sysfs method:
import time
def get_net_stats(interface):
with open(f'/sys/class/net/{interface}/statistics/rx_bytes') as f:
rx = int(f.read())
with open(f'/sys/class/net/{interface}/statistics/tx_bytes') as f:
tx = int(f.read())
return rx, tx
prev_rx, prev_tx = get_net_stats('eth0')
time.sleep(1)
curr_rx, curr_tx = get_net_stats('eth0')
print(f"RX rate: {(curr_rx - prev_rx)/1024:.2f} KB/s")
print(f"TX rate: {(curr_tx - prev_tx)/1024:.2f} KB/s")
JSON Output with jq
For machine processing, combine ip
with jq
:
ip -j -s link show dev eth0 | jq '.[].stats64'
The /sys
method is fastest for scripts as it doesn't spawn external processes. For human-readable output, ip -s
provides the best balance between information and readability.
As Linux distributions move away from legacy networking tools, many sysadmins find themselves needing alternatives to ifconfig
for monitoring network statistics. The ip
command from iproute2 has become the recommended replacement, but its output format differs significantly.
The most straightforward replacement is:
ip -s link show dev eth0
This outputs detailed statistics including:
- RX: bytes, packets, errors, dropped, overrun, mcast
- TX: bytes, packets, errors, dropped, carrier, collsns
For scripting purposes, the kernel exposes interface statistics in:
/proc/net/dev
Sample parsing with awk:
awk '/eth0:/ {print "RX:", $2, "bytes TX:", $10, "bytes"}' /proc/net/dev
Consider these modern alternatives:
- ss: Socket statistics with
ss -s
- nstat: Network stack statistics
- ethtool: Detailed NIC statistics with
ethtool -S eth0
Here's a practical bash script to track interface usage:
#!/bin/bash
INTERFACE="eth0"
RX1=$(cat /proc/net/dev | grep $INTERFACE | awk '{print $2}')
TX1=$(cat /proc/net/dev | grep $INTERFACE | awk '{print $10}')
sleep 1
RX2=$(cat /proc/net/dev | grep $INTERFACE | awk '{print $2}')
TX2=$(cat /proc/net/dev | grep $INTERFACE | awk '{print $10}')
echo "RX: $((($RX2 - $RX1)/1024)) KB/s"
echo "TX: $((($TX2 - $TX1)/1024)) KB/s"
For enterprise monitoring needs:
- Prometheus + node_exporter
- Telegraf + InfluxDB
- Zabbix or Nagios for alerts