How to get TX/RX bytes without ifconfig?
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?
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
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
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
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.