Solid state drives in Linux: Enabling TRIM for SSDs

466 readers like this.
Three giant robots and a person

After installing my first solid state drive (SSD) in a computer that was running Linux, I have begun to explore how to take care of them. Solid state drives are different than traditional magnetic drives in the way that they operate, and they require different care from the software side in order to function optimally.

On traditional magnetic drives, deleted files are not completely removed from the disk at the time of deletion. This is why you can recover deleted files. Essentially, the filesystem just references the location of a file on the disk, and when a file is deleted, that reference is erased, allowing you to write new data over old data in these blank spaces. However, with SSDs, new data can only be written on completely new or erased cells of the drive. Because the space must be cleared prior to a write, if enough free space is not already available at the time a file is being written, it must be erased first. This can negatively affect performance.

If the operating system were to erase unused space before writing new data, at a time when the device is not simultaneously trying to write, file saving performance could be improved. Enter TRIM. A TRIM command essentially allows your operating system to tell the drive which areas of data are no longer being used so that it come wipe them, speeding up the drive for future writes, and providing users of SSDs with a more optimum experience.

In Linux, fstrim provides this functionality, readying the drive for new data to be written and extends the life of the drive over the long term. Since trimming SSDs is not automatic on the Linux distributions that I have used, it is imperative that it be scheduled or the performance of the SSD will degrade over time.

In order to run fstrim on a drive, the drive itself, as well as the file system sitting on top of it, must support TRIM. Enabling TRIM can be done during the filesystem mounting process. For example, in order to mount the device /dev/sda2 to /mnt with TRIM enabled, you would run:

mount -t ext4 -o discard /dev/sda2 /mnt

TOnce enabled, the TRIM process itself is rather simple. Trimming your SSD can also be accomplished manually on the command line or in a cron job. As a super user (using su or sudo), run fstrim / -v to accomplish manual trimming, or set up a cron job to run this command for you on a regular basis when your computer is not in use. And for a complete list of fstrim, options refer to its man page.

Hardware support varies depending on the type of drive interface used whether PCI, ATA, SCSI or SD/MMC. It's also worth consulting with your Linux vendor to learn more about how your particular distribution may support TRIM.

For example, Red Hat offers the following the SSD disk guidelines. "Performance degrades as the number of used blocks approaches the disk capacity. The degree of performance impact varies greatly by vendor. However, all devices experience some degradation. To address the degradation issue, the host system (for example, the Linux kernel) may use discard requests to inform the storage that a given range of blocks is no longer in use."

The Debian wiki offers some basic cautions for SSD use: Use a Linux kernel 3.2 or newer, use the latest firmware for the SSD, use the EXT4 file system, and "have enough DRAM required to operate without swap space under normal workloads."

User profile image.
Educator, entrepreneur, open source advocate, life long learner, Python teacher. M.A. in Educational Psychology, M.S. Ed. in Educational Leadership, Linux system administrator.


Great information. I use JFS on SSD on both my desktop and laptop, with TRIM features and, I think, noatime set. Works quite well.

The discard flag causes the filesystem to issue TRIM commands on each sector as they are deleted. There are varying schools of thought on whether this is a good idea, but it can have an impact on performance. The filesystem does not need to be mounted with -o discard to be able to run fstrim. In fact, some distros have a cron job that runs fstrim -av weekly in lieu of mounting with -o discard.

Ubuntu activates the trim command as a default as soon as it detects your using an ssd hard drive.


You can check the changelog for Ubuntu 16.04, regarding de util-linux package. It installs the fstrim command and script to run automatically. Quoting:
"util-linux (2.27.1-6ubuntu1) xenial; urgency=medium * Merge from Debian unstable. Remaining changes: - Regularly trim SSDs automatically (core-1311-ssd-trimming): + Add debian/fstrim-all.cron: Call fstrim --all. Installed as /etc/cron.weekly/fstrim."



In reply to by Don Watkins

Thanks for sharing and adding to the learning.

In reply to by cromualdo (not verified)

Systemd also offers fstrim.timer.
As an example, here is how to enable it for Debian:
sudo cp /usr/share/doc/util-linux/examples/fstrim.service /etc/systemd/system
sudo cp /usr/share/doc/util-linux/examples/fstrim.timer /etc/systemd/system
sudo systemctl enable fstrim.timer

Thanks for sharing. All this helps to get a better idea of how to take care of solid state drives.

In reply to by Ferdinand Thommes (not verified)

Thanks for this awesome explanation and article.

This article is misleading. I know someone already commented on this, but you should really fix it in your main article. It's not
step 1: mount with discards
step 2: now run fstrim
these are actually two different approaches altogether. In fact, running fstrim after the filesystem has been mounted with discards is completely needless (other than the first time).

This article is about 5 years late to the party, and even after all that time, is inaccurate at best.

I'm not a fan of SSD drives. I don't know what it is about them, I just prefer the spinning disks that I know and love, they operate as I need them to, and they don't give problems until their EOL. And by that time I've already got another one to replace it. Not to mention I'm not able to purchase 2/3/4TB hard drive for very little money:……

--more than enough space for all the scripts I'll write as a fledgling programmer! I guess when SSD's become the norm I'll eventually have to come to grips with it and buy them, but until then?...I'll stock up on spinning disks and enjoy the seek times! LoL!

I installed linux on my single-SSD laptop, do I have to enable TRIM, will the system automatically enable it? thx.

Very good article!

I use a Samsung EVO 850 SSD with several different distributions and did not know that issue.

With proper authorization and author's credits I would like to prepare a text for the readers of my website.

Thank you!

good comments

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.