hdparm Command in Linux

Introduction

In this guide, you will discover how to leverage the Linux hdparm command to interact and fine-tune various settings of your hard disk drives (HDDs) and solid-state drives (SSDs). This tutorial covers understanding the purpose and functionality of hdparm, benchmarking disk performance, and optimizing disk performance using this powerful utility for systemadmin tasks. You will delve into features like disk performance benchmarking, disk configuration optimization, and disk information retrieval. The examples shown aim to help you effectively utilize hdparm in your Linux environment.

Understand the Purpose and Functionality of hdparm

In this section, you will learn about the purpose and functionality of the hdparm command in Linux. The hdparm command is a robust utility enabling systemadmin to interact with and configure diverse aspects of your hard disk drives (HDDs) and solid-state drives (SSDs).

The hdparm command offers the following key functionalities:

  1. Disk Performance Measurement: You can use hdparm to benchmark the performance of your disk, including read/write speeds, cache settings, and other important parameters.
  2. Disk Configuration Optimization: hdparm allows you to adjust various disk parameters to optimize performance, such as enabling advanced power management features, setting the read-ahead cache size, and more.
  3. Disk Information Retrieval: The hdparm command can provide detailed information about your disk, including the model, firmware version, and other technical specifications which is useful for systemadmin.

Let's begin by using hdparm to retrieve information about your disk as root.

sudo hdparm -i /dev/sda

Example output:

/dev/sda:
 Model=VBOX HARDDISK, FwRev=1.0, SerialNo=
 Config={ HardSect NotMFM Motor(_) Removeable DTR(500Mbs) SpinUp(0.600s) }
 RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=0
 BuffType=unknown, BuffSize=0kB, MaxMultSect=0
 (maybe): CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=33554432
 IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
 PIO modes:  pio0 pio1 pio2 pio3 pio4
 DMA modes:  mdma0 mdma1 mdma2
 UDMA modes: udma0 udma1 udma2 udma3 udma4 *udma5
 AdvancedPM=no WriteCache=enabled
 Drive conforms to: unknown:  ATA/ATAPI-1 ATA/ATAPI-2 ATA/ATAPI-3 ATA/ATAPI-4 ATA/ATAPI-5 ATA/ATAPI-6 ATA/ATAPI-7

This command retrieves detailed information about the /dev/sda disk, including the model, firmware version, serial number, and supported disk modes. Understanding this information is essential for any systemadmin.

Measure Disk Performance with hdparm

In this section, you will learn how to use the hdparm command to benchmark the performance of your disk in Linux. This information can be useful for identifying performance bottlenecks and optimizing your system's disk configuration for systemadmin tasks.

Let's start by measuring the disk's read performance using the -t option:

sudo hdparm -t /dev/sda

Example output:

/dev/sda:
 Timing buffered disk reads: 236 MB in  3.01 seconds = 78.41 MB/sec

This command measures the disk's buffered read performance, which provides an estimate of the disk's maximum sustained read speed.

Next, let's measure the disk's cached read performance using the -T option:

sudo hdparm -T /dev/sda

Example output:

/dev/sda:
 Timing cached reads:   13624 MB in  2.00 seconds = 6812.00 MB/sec

The cached read performance measures the speed of reading data from the disk's cache, which is typically much faster than reading directly from the disk.

Finally, let's measure the disk's sequential read and write performance using the -t and -T options together:

sudo hdparm -tT /dev/sda

Example output:

/dev/sda:
 Timing buffered disk reads: 236 MB in  3.01 seconds = 78.41 MB/sec
 Timing cached reads:   13624 MB in  2.00 seconds = 6812.00 MB/sec

This command provides both the buffered disk read and cached read performance metrics, giving you a more comprehensive view of your disk's performance.

Optimize Disk Performance using hdparm

In this final section, you will learn how to use the hdparm command to optimize the performance of your disk in a Linux system as a systemadmin.

First, let's enable the read-ahead cache, which can significantly improve read performance:

sudo hdparm -r1 /dev/sda

Example output:

/dev/sda:
 setting readonly to 1 (on)

This command sets the read-ahead cache size to 1, which is the maximum value. You can experiment with different values to find the optimal setting for your system.

Next, let's enable the advanced power management (APM) feature, which can help reduce power consumption and potentially improve performance for HDD and SSD:

sudo hdparm -B 254 /dev/sda

Example output:

/dev/sda:
 setting Advanced Power Management level to 0xfe (254) (maximum performance)

This command sets the APM level to 254, which is the maximum performance setting. You can experiment with lower values to find the right balance between performance and power savings.

Finally, let's enable the write-caching feature, which can improve write performance:

sudo hdparm -W1 /dev/sda

Example output:

/dev/sda:
 setting drive write-caching to 1 (on)

This command enables the write-caching feature, which can provide a significant boost to write performance.

After making these changes, let's verify the disk performance again using the steps from the previous section.

Summary

In this guide, you first learned about the purpose and functionality of the hdparm command in Linux, making it a useful tool for any systemadmin. The hdparm command is a powerful utility that allows you to interact with and configure various parameters of your hard disk drives (HDDs) and solid-state drives (SSDs). It provides key functionalities such as disk performance measurement, disk configuration optimization, and disk information retrieval. You then explored how to use hdparm to measure the performance of your disk, including read/write speeds, cache settings, and other parameters. Finally, you learned how to optimize disk performance using hdparm by adjusting various disk parameters, such as enabling advanced power management features and setting the read-ahead cache size.

400+ Linux Commands