How to use iostat command in Linux explained with examples

When using a computer and a system it is useful to know how it is managing the use of resources. This will allow us to know which is the performance of the same one and thus to solve problems if there were them. There are too many tools for this purpose in Linux but if we can use them in the terminal then it is a bonus. That is why there is the Linux iostat command that specializes in the input and output load of the system. We will also help you with some examples so that you can understand the command.

So, The iostat command is used to monitor the activity of the system’s devices, partitions, and networked systems (NFS). It can also be used to compare activity between disks.

This command generates reports that can be used to change system configuration to better balance the input/output load between physical disks. The first report generated by the iostat command provides statistics concerning the time since the system was booted. Each subsequent report covers the time since the previous report

In short, iostat is one of those GNU/Linux utilities that every sysadmin should know and understand perfectly. But also, any user concerned about his computer can know and learn.

Getting the iostat command

Usually, the iostat command is not built into almost any Linux distribution. But this command is included in the sysstat package that we have to install in our Linux distribution.

So, if you use Debian, Ubuntu, or any derivative you have to run these commands:

sudo apt update
sudo apt install sysstat

For distributions like RHEL, CentOS or Fedora or some derivative:

sudo dnf install sysstat

And in Arch Linux and derivatives the command would be the following:

sudo pacman -S sysstat

Once this package is installed, the iostat command will be available.

Using the iostat command on Linux

The iostat command has many different options for it to be executed. However, the basic syntax of the command is as follows:

iostat [ options ] [ <interval> [ <count> ] ] ]

Although it is also possible to run it without any parameters and it will give us the following output on the screen:

Linux 5.4.0-65-generic (atechtown) 	03/20/2021 	_x86_64_	(1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           4.49    0.00    1.75    0.32    0.00   93.45

Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
sda              15.47       281.02      1453.92         0.00     147424     762728          0

This first iostat report is easy to interpret, but let’s take it in parts.

The first section is dedicated to the system but with quite basic information that serves as a preamble to the report as such. The information shown is the Kernel version, hostname, system date, system type, and the number of processor cores.

In the second section of the report we find information about the CPU of the computer and the following values:

  • %user: Percentage of CPU usage at the normal user level.
  • %nice: Show the percentage of CPU usage at the user level with priorities.
  • %system: Show the percentage of CPU utilization that occurred while executing at the system level (kernel).
  • %iowait: This value shows the percentage of time that the CPUs were idle and the system received I/O requests.
  • %steal: Show the percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor.
  • %idle: Show the percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.

The third section of the report is what we are looking for which is a report on the I/O devices that we have.

  • Device: Refers to the device from which the data originates.
  • tps: Indicate the number of transfers per second that were issued to the device. A transfer is an I/O request to the device. Multiple logical requests can be combined into a single I/O request to the device. A transfer is of indeterminate size
  • kb_read/s Indicate the amount of data read from the device expressed in kilobytes per second.
  • kB_wrtn/s: Indicate the amount of data written to the device expressed in kilobytes per second.
  • kB_read: The total number of kilobytes read.
  • kB_wrtn: The total number of kilobytes written.

Example of use

Obtain more comprehensive statistics

The most common use of iostat is with the x option which gives us a more detailed report.

iostat -x
Using the iostat command
Using the iostat command

This way we will have more columns that show even more information about it.

Get CPU statistics only

There are times when it is convenient to omit other reports and just keep the CPU report, for that there is the -c option.

iostat -c
Only CPU mode
Only CPU mode

Device statistics only

On the other hand, it is also possible to show only the device statistics.

iostat -d
Only Device mode on iostat
Only Device mode on iostat

Using interval information

One of the most interesting capabilities of the iostat command is that we can get a certain amount of reports in a certain time range.

In this example, I will request three reports from the device with an interval of two seconds.

iostat -d 2 3
Interval mode
Interval mode

You can even use the -m option to change the unit of measure to Mb.

iostat -md 2 3
Changing the output format
Changing the output format

This way you can get a more readable report.

Likewise, you can request an interval report for the cpu with the -c option.

iostat -mc 2 3
Using the iostat options
Using the iostat options

Obtain a block output

Finally, it is possible to obtain a more ordered block output by using the -p option.

iostat -p
Block output
Block output

So you can manage it in a better way.

Conclusion

Iostat is a fairly simple command to use but it can be quite useful if we want to know how our disk is being used. Especially useful in environments where there is known to be a lot of incoming and outgoing traffic it can give us important metrics on how the disk is performing. In this post, we have shown you with some examples the simplicity and greatness of the command.

More info: The iostat documentation and more tutorial about Linux Commands

Share This: