Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we’ll discuss the alternative ways to read the TX/RX information. TX shows the volume of data in bytes transmitted by an interface while RX information indicated the volume of data received by an interface in bytes. Usually, we use the ifconfig command since its output contains the TX/RX information. At present, ifconfig is deprecated and it’s no longer supported. At the same time, Linux OS has already provided us with the equivalent commands we can use to obtain the same information. As we often say, one of the defining features of Linux is that “everything is a file”. Here, we’ll look at some special files we can obtain this information from.

2. The ip Command

Despite ifconfig being available, we can’t rely on it any longer. It won’t be available in future versions. On the contrary, it’s still in use. The ip command is the perfect alternative to the ifconfig command. We use the ip command to display and configure the network parameters for our computer. Besides having the capability to find out which interfaces are configured on the system, we can also use ip to check the status of the other interfaces:
$ ip a              
2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 64:66:00:7v:a6:8c brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.7/24 brd 192.168.0.255 scope global dynamic noprefixroute wlan0
       valid_lft 165135sec preferred_lft 165135sec
    inet6 fe80::1142:db0d:1d29:f10a/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
As we can see, this output doesn’t give us the TX and RX information. To get this information, we use the -s flag and the link object. To begin with, let’s see the TX and RX information on all interfaces in our computer:
$ ip -s link               
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    RX:  bytes packets errors dropped  missed   mcast           
          7920     104      0       0       0       0 
    TX:  bytes packets errors dropped carrier collsns           
          7920     104      0       0       0       0 
2: wlan0: <BROADCAST,MULTICAST, UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 62:57:20:7g:a6:7c brd ff:ff:ff:ff:ff:ff
    RX:  bytes packets errors dropped  missed   mcast           
     152808590  126789      0       0       0       0 
    TX:  bytes packets errors dropped carrier collsns           
      10153431   68193      0       0       0       0 
3: usb0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN mode DEFAULT group default qlen 1000
    link/ether 76:4d:7e:25:0b:ed brd ff:ff:ff:ff:ff:ff
    RX:  bytes packets errors dropped  missed   mcast           
       5468410    5900      0       0       0       0 
    TX:  bytes packets errors dropped carrier collsns           
        873035    4914      0       0       0       0 
Next, let’s display this information for a single interface:
$ ip -s link show dev wlan0
2: wlan0: <BROADCAST,MULTICAST, UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 62:57:20:7g:a6:7c brd ff:ff:ff:ff:ff:ff
    RX:  bytes packets errors dropped  missed   mcast           
     152808540  126788      0       0       0       0 
    TX:  bytes packets errors dropped carrier collsns           
      10153349   68192      0       0       0       0
We can use the -h option to make our output more human-readable:
$ ip -s -h link show dev wlan0
2: wlan0: <BROADCAST,MULTICAST, UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether 62:57:20:7g:a6:7c brd ff:ff:ff:ff:ff:ff
    RX:  bytes packets errors dropped  missed   mcast           
          153M    127k      0       0       0       0 
    TX:  bytes packets errors dropped carrier collsns           
         10.2M   68.6k      0       0       0       0 

3. /proc/net/dev File

The /proc directory is also known as the virtual file system. If we list the contents of the virtual file system using ls, the files within are listed. Even though these files don’t exist on the disk, the OS creates them on the spot upon our request or when a process makes the request. If we look inside the /proc directory, we realize we have named and numbered directories. The named directories may be similar to some of the commands we usually run. On the other hand, the numbered directories are the PIDs of the running processes. /proc/net directory provides an in-depth look at various networking parameters and statistics on the system. Each directory has a virtual file within it. The file describes the aspect of the system’s network configuration. Inside the /proc/net directory, some of the virtual files we’ll find are arp, dev, netstat, raw, route, etc. The /proc/net/dev file lists the various network devices configured on the system. Additionally, it shows complete transmission (TX) and reception (RX) statistics on each interface. This file displays the number of bytes each interface has sent and received, the number of inbound and outbound packets, the number of errors seen, the number of packets dropped, etc. We can use cat to view these virtual files:
$ 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:    7440      96    0    0    0     0          0         0     7440      96    0    0    0     0       0          0
  eth0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
 wlan0: 4122842    4984    0    0    0     0          0         0   781419    3322    0    0    0     0       0          0
We must note that on most devices /proc/net/dev is read from the hardware counters. Likewise, other stats get updates from the network stack in the device structures.

4. /sys/class/net Directory

Apart from the virtual file system, we can also look at the /sys/class/net directory. This directory has a directory for each network interface available in our system:
$ cd /sys/class/net/ && ls
lo  usb0  wlan0
In each of these directories, we have a /statistics directory that contains all the network information. Let’s get into the wlan0 directory and view its contents:
$ cd /sys/class/net/wlan0 && ls 
addr_assign_type    dev_port           name_assign_type      queues
address             dormant            napi_defer_hard_irqs  speed
addr_len            duplex             netdev_group          statistics
broadcast           flags              operstate             subsystem
carrier             gro_flush_timeout  phy80211              testing
Next, let’s view the contents of the /statistics directory:
$ cd wlan0/statistics && ls
collisions  rx_compressed  rx_errors        rx_length_errors  rx_over_errors     tx_bytes           tx_dropped      tx_heartbeat_errors
multicast   rx_crc_errors  rx_fifo_errors   rx_missed_errors  rx_packets         tx_carrier_errors  tx_errors       tx_packets
rx_bytes    rx_dropped     rx_frame_errors  rx_nohandler      tx_aborted_errors  tx_compressed      tx_fifo_errors  tx_window_errors
Now, let’s view the TX/RX information. To display the files, we use cat:
$ cat rx_bytes             
153262397
$ cat tx_bytes
10389858

5. Using the ethtool Command

The ethtool is a command used to query or control network driver and hardware settings. We can use this command to get the statistics of a particular interface. To query a specific network interface and get the network stats, we use the -S flag. Let’s look at its syntax:
$ ethtool -S interface_name
For example, let’s run the ethtool command on our terminal:
$ ethtool -S wlan0 | grep '[r,t]x'
     rx_packets: 405242
     rx_bytes: 192996587
     rx_duplicates: 93
     rx_fragments: 268845
     rx_dropped: 575
     tx_packets: 69741
     tx_bytes: 10409745
     tx_filtered: 0
     tx_retry_failed: 0
     tx_retries: 4432
     txrate: 135000000
     rxrate: 1000000
     ch_time_rx: 18446744073709551615
     ch_time_tx: 18446744073709551615
We’re using grep to filter and get results for TX and RX.

6. Using the netstat Command

In older systems, we can still use the netstat command. Even though it’s outdated, it’s available in current versions of Linux. The future versions might not have it. We use the netstat command to print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. Let’s use the netstat command to see the TX and RX information:
$ netstat -i            
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
lo       65536      114      0      0 0           114      0      0      0 LRU
usb0      1500     2895      0      0 0          2683      0      0      0 BMRU
wlan0     1500   129149      0      0 0         69615      0      0      0 BMRU
Next, we use it to display the TX and RX statistics for each protocol:
$ netstat -s      
Ip:
    Forwarding: 2
    344042 total packets received
    1 with invalid headers
    4 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    338915 incoming packets delivered
    217068 requests sent out
    58 outgoing packets dropped
    223 dropped because of missing route
Icmp:
    156 ICMP messages received
    0 input ICMP message failed
    ICMP input histogram:
        destination unreachable: 156
    181 ICMP messages sent
    0 ICMP messages failed
Although netstat -i is still usable, ip -s link command is the better option. The netstat command may be dropped in the coming versions.

7. Conclusion

In this article, we looked at alternatives to using the ifconfig command to get the TX/RX information. We discussed the ip, ethtool, and netstat commands. Besides, we also saw the virtual file system where we explored some of the contents of the /proc/net directory and the /proc/net/dev file. Lastly, we looked at the /sys/class/net directory, which is similar to the virtual file system.
Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

guest
0 Comments
Inline Feedbacks
View all comments