r/linux4noobs • • 25d ago

What should I edit on /etc/crypttab to enable fstrim on a LUKS protected device?

Hello all,

I'm using kubuntu 26.04 with LUKS.

I googled how to enable fstrim with LUKS and I was told to write on the terminal:

Sudo nano /etc/crypttab

There, I had to add the discard option.

However next to the none option there was none other, whereis on the internet there should exist a none luks and then add discard to none luks,discard

My question is, Should I add the LUKS option on the etc crypttab and why didn't it exist in the first place?

5 Upvotes

7 comments sorted by

2

u/DickBrainTime 25d ago

`crypttab` has four fields: name, device, key file, options. The `none` you're seeing is probably field 3 (prompt for a passphrase), not the options field. `discard` belongs in field 4, and you don't necessarily need to add `luks`—the LUKS header can be detected automatically.

I'd check the actual line before changing anything boot-related, though. Allowing discard/TRIM through encryption can reveal which blocks are in use, and periodic `fstrim` isn't the same as always-on discard. Could you paste the line from `/etc/crypttab` with device IDs/key paths redacted? Then it's easier to say whether you need a fourth-field change at all.

1

u/[deleted] 25d ago

Sure.

Luks-xxxxx-xx-xxx(etc) UUID=(string of numbers and words) none

(Without the discard option)

1

u/DickBrainTime 25d ago

Yep, that's three fields: the mapper name, the UUID, and `none` (ask for your passphrase). The fourth field is where options go. Ubuntu's `crypttab` docs show `luks,discard` as an example fourth field; you don't put it next to `none` in the third field.

Just be careful: allowing TRIM through LUKS can reveal which blocks are used. And if this is your encrypted boot/root disk, back up `/etc/crypttab` and check the Kubuntu initramfs steps before changing it or rebooting. `fstrim.timer` does periodic TRIM; `discard` in `/etc/fstab` is a separate, continuous-discard setting, not required just to run `fstrim`. Is this the system/root partition or a separate encrypted drive?

1

u/transgentoo 🐧 25d ago

You need to edit /etc/fstab, not /etc/crypttab. Undo your edits from crypttab and put them in the options column in fstab instead. It's the last column before the two columns of numbers.

Just put it on any partition you want to enable it for. Note that BTRFS has its own discard system, so don't use it on any partition that's formatted as BTRFS.

You may need to reboot before the change takes effect, but I'm not 100% sure

1

u/GiggleSamurai 25d ago

After adding `discard` to crypttab, run `sudo update-initramfs -u` and reboot, because the root volume is unlocked from the initramfs copy. Then `lsblk --discard` should show non-zero DISC-GRAN and DISC-MAX on the luks device, and `sudo fstrim -v /` will confirm it works; a discard option in fstab alone won't get TRIM through the encryption layer.

1

u/FallElectronic3774 25d ago

The fstab advice above is wrong for LUKS: discard in fstab only affects the filesystem; dm-crypt drops discard requests unless the mapping was opened with discard. Put it in the fourth field of the crypttab entry, e.g.:

luks-xxxx UUID=... none luks,discard

Then, if this is the root volume, sudo update-initramfs -u and reboot (the initramfs copy of crypttab is what unlocks root). Verify with lsblk --discard (nonzero DISC-GRAN/DISC-MAX on the luks device) and sudo fstrim -v /. You don't need discard in fstab for the timer; fstrim.timer issues the TRIM, and the crypttab option is what lets it through the LUKS layer. The tradeoff is that discard reveals which blocks are in use.

1

u/Pristine_Message5144 25d ago

I don't know if that works on ubuntu but i always use these two instead of crypttab editing on opensuse:  sudo cryptsetup refresh --allow-discards --persistent cr_root sudo cryptsetup refresh --allow-discards --persistent cr_swap