How to get TX/RX bytes without ifconfig?
Solution 1
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
Solution 2
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
Solution 3
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
Solution 4
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.
Related videos on Youtube
justinas
Updated on September 18, 2022Comments
-
justinas almost 2 years
Since
ifconfig
is apparently being deprecated in major Linux distributions, I thought I'd learn something about theip
tool that's supposed to be used instead ofifconfig
.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 fromip
. Is there no such function in this tool? What other built-in tools could I use for getting those stats?-
SunSparc almost 11 yearsYou can also check out
netstat
. linux.die.net/man/8/netstat
-
-
justinas almost 11 years
/proc/net/dev
has the cleanest format of all of the solutions IMO, accepting. -
Moshe Katz almost 11 yearsWhile the
proc
answer is clean and lightweight, this answer is probably the closest to a "replacement" for theifconfig
tool because it was explicitly designed with replacement ofifconfig
in mind. -
lalebarde almost 10 yearsStats showed by
cat /proc/net/dev
are for how long time ? -
Piotr Kula about 9 yearsHow do you get tx/rx from
/proc/net/dev
? -
dsmsk80 about 9 yearsTry to parse 3. and 11. field of the output, something like awk '/:/ { print($1,$3, $11) }' < /proc/net/dev
-
Admin over 7 yearsWelcome to the community! Well done, that's correct, but note that some old kernels don't provide these files. Wish you luck, and success! ;)
-
pim about 6 years
ip -s link show dev eth0
to show only traffic on eth0 -
Joseph Redfern about 5 yearsTo get this in human readable form (reporting in mb/gb/tb rather than bytes), use the
-h
flag. For instance,ip -h -s link
. -
Mike S about 3 yearsnetstat is deprecated along with ifconfig. On Linux, not sure about other *IX varietals.
-
Admin about 2 years
netstat -i
cuts off interface names after the 8th char, not very usable if your system has longer interface names (which mine does).