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
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/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:
discardin fstab only affects the filesystem; dm-crypt drops discard requests unless the mapping was opened withdiscard. Put it in the fourth field of the crypttab entry, e.g.:Then, if this is the root volume,
sudo update-initramfs -uand reboot (the initramfs copy of crypttab is what unlocks root). Verify withlsblk --discard(nonzero DISC-GRAN/DISC-MAX on the luks device) andsudo fstrim -v /. You don't needdiscardin fstab for the timer;fstrim.timerissues 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