Let’s start by explaining what write-back caching is and how it works to better understand the Linux write cache. Write caching is a feature available on most hard drives that allows them to collect all data into the drive’s cache memory, before being permanently written to disk. Once a certain amount of data is collected in the hard drive’s cache memory, the whole data chunk is transferred and stored with a single writing event.
The result of this writing method is a reduction in write events, thereby improving a hard drive’s data transfer and yielding a faster write speed.
In this tutorial, you’ll learn how write caching works on a Linux system. You’ll also see how to check whether caching is enabled on one of your disks, and how to toggle the setting on or off.
In this tutorial you will learn:
- How to install hdparm on major Linux distros
- How to use hdparm to view HDD chache settings
- How to use hdparm to enable or disable HDD write caching
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Install hdparm on major Linux distros
In order to check your hard drive’s caching setting, and to enable or disable drive caching via the command line, you’ll need to install the
hdparmsoftware package. This software is available in official repositories on nearly all Linux distributions.
On some systems, hdparm may already be installed by default. But if not, you can use the appropriate command below to install the program with your system’s package manager.
$ sudo apt install hdparm
$ sudo dnf install hdparm
$ sudo pacman -S hdparm
After it’s installed, check out the examples below to configure hard drive caching in Linux.
Linux write cache settings
The write-back cache is enabled by default on most hard drives. This technology is especially important for SSDs (Solid Sate Drives) which are based on flash technology and have a limited number of write/erase cycles. By transferring data first to volatile cache memory, and writing it all in single batch, the write-back caching extends the life expectancy of most solid state drives.
Not all systems belong to the same “turn-on write-back caching” recommendation group, as write-back caching carries a risk of data loss in certain events, such as power failure, etc. In the event of power failure, data residing in the hard drive’s cache does not get a chance to be stored, and will be lost. This fact is especially important for database servers and similar systems.
Adjust hard drive cache settings with hdparm
hdparmexamples below to see how you can adjust the chaching settings of the HDDs on your system.
The only thing you need to know before getting started is the path to the drive’s device file that you wish to enable or disable caching for. In the examples below, we will be using a hard drive at path
/dev/sda. You can see a list of all your connected storage devices and their corresponding device paths with the following command:
# fdisk -l
- Use the following
-Woption to check whether a drive has write caching turned on or off.
$ sudo hdparm -W /dev/sda /dev/sda: write-caching = 1 (on)
- To disable write caching on the drive, use the
$ sudo hdparm -W 0 /dev/sda /dev/sda: setting drive write-caching to 0 (off)
- To enable write caching on the drive (recommended for most situations), use the
$ sudo hdparm -W 1 /dev/sda /dev/sda: setting drive write-caching to 1 (on)
Linux write cache is a feature that will prolong the life of a hard drive and provide faster write results. The main reason some users may find the need to turn write caching off is in the case of a database server or similar system that is constantly doing large writes to disk. As seen in this guide, it’s an easy setting to toggle on or off with the use of the
hdparm utility on Linux.