Easy encrypted removable USB storage for Linux users - Page 1
Forum section: electronics / general

author: Nominal Animal
author_rank: Super Contributor
timestamp: May 16, 2025, 05:50:30 pm
content: Current Linux distributions will prompt for a passphrase when any encrypted media it recognizes is entered. I like to use LUKS2 encryption (256-bit aes-xts-plain64 with sha256 hashing), and select "forget the passphrase immediately" when mounting, so that the passphrase I used itself is not retained in memory anywhere (and the necessary secret stuff will be in kernel keychain). If a removable media contains more than one partition, they're all mounted (if possible). For me, the main use for an encrypted removable media is to backup my web account information. This way, it is usually not accessible (mounted) at all. Only when I create a new account or change a password, do I plug in the media and update the details, then detach it again.If you havegnome-disk-utilityinstalled, you can trivially create such a removable media, by removing all partitions on it, then creating a partition, and selecting type "Internal disk for use with Linux systems only (ext4)" with "Password protect volume (LUKS)". The trick is that when you then try to open the folder in your file manager, it will get mounted, but the file manager will tell you you don't have sufficient rights to browse it. This is okay; just run (once, after formatting it)sudo chown $(id -un):$(id -gn) /media/$(id -un)/LABEL/.to change the ownership to the currently running user. You can also remove the emergency space reservation for root by runningsudo rmdir /media/$(id -un)/LABEL/lost+found/Again, these only need to be done once only. The encrypted partition should useargon2idkey exchange, 2×256-bit aes-xts-plain64 cipher, andsha256hash.However, doing it all on the command line, with full control over every bit, is quite easy too. I shall explain that here. Note that this will absolutely wipe the contents on that media, so be very careful, and check twice before proceeding!Plug in the storage device, and find out the device name. If it gets automounted, unmount all partitions on it. I like to uselsblk -o NAME,SIZE,TYPE,VENDOR,MODEL,MOUNTPOINTSto list the disks and partitions currently known to the Linux kernel; if the mountpoint isn't empty, that isn't the one.For USB-connected media, this is typically/dev/sdX, whereXis a lowercase letter starting witha.Partition the device. Linux will mount all partitions, although I do recommend only encrypting at most one per physical device.It does not really matter what program you use:fdisk,sfdisk,gparted,gnome-disks, or something else.Both GPT and MBR (dos) partition tables will work equally well.My favourite solution is to do two partitions: the first one an unencrypted FAT or exFAT partition, and the second one an encrypted ext4 partition. Note that ext4 encodes owner and group with numerical values, so if you intend to transport files between multiple machines, you need to take that into account. Any filesystem supported by your Linux machine will work, though.Make sure none of the partitions are mounted; use the abovelsblkcommand andumount, if they are.If you have unencrypted partitions on it, you can format them now. Whatever filesystem you use, do remember to set theLABEL, because that will determine the name it'll show up when mounted.Prepare the partition encryption format, usingcryptsetup luksFormat.For a reasonably powerful machine, I would recommendLUKS2withargon2idkey derivation function,aes-xts-plain64cipher, 512 bits, andsha512hash for key setup. (You can run "cryptsetup benchmark" to see the encryption/decryption speeds using various hashes and ciphers.)I use long passphrases, and prefer the password processing to be faster than the default two seconds, so prefer to set it to 500 milliseconds using a single thread. TheLABELandSUBSYSTEMare optional, and not used by the standard tooling, but unlike the filesystem label, can be checked without decrypting the disk, so I use them to keep track long-term what I intended the encrypted media to be used for. Thus:cryptsetup luksFormat --type luks2 --hash sha512 --cipher aes-xts-plain64 -s 512 --pbkdf argon2id --iter-time 500 --pbkdf-parallel 1 --verify-passphrase --labelLABEL--subsystemSUBSYSTEM/dev/sdXnFollow the prompts. You'll need to typeYES, then the passphrase twice.At this point, you can check the encryption settings, and redo them if you're not satisfied:sudo cryptsetup luksDump /dev/sdXnOpen the encrypted partition, so we can format it:sudo cryptsetup open /dev/sdXntempnamewhere the decrypted device name will be/dev/mapper/tempname. It is only used this once, so doesn't matter what you use.If you find you're unhappy with how long the passphrase processing is taking etc., you can runsudo cryptsetup close /dev/mapper/tempnameand redo from step 4 (luksFormat) above onwards.Format the decrypted partition (/dev/mapper/tempname).As with unencrypted partitions, remember to set the volume label, as that determines the name you see in UI; typically, the media will be mounted at/media/username/volumelabel/with the volume label shown in graphical UI also.I like to use ext4 myself, for examplesudo mkfs.ext4 -t ext4 -e remount-ro -E discard,root_owner=$(id -u):$(id -n) -Lvolumelabel/dev/mapper/tempnamewhere theroot_owneroption copies the owner and group ID numbers from the current user.Close the encrypted device,sudo cryptsetup close /dev/mapper/tempnameunmount any unencrypted partitions on the same removable media, and optionally, if you're a bit paranoid like me, run something likesudo sh -c 'sync; echo 3 >/proc/sys/vm/drop_caches ; sync'to ensure absolutely everything is written to storage, and then remove the media.Done! On next insertion, you should get a passphrase prompt. If it shows you the options, it is safest to immediately forget the passphrase, as the actual runtime decryption keys will be stored in-kernel, and the passphrase you used will not be stored anywhere.When auto-mounted this way, the decrypted device name is typically/dev/mapper/luks-UUID-of-the-filesystem.If you use your desktop environment media ejection button, it will essentially do bothumount /media/username/volumelabelcryptsetup close /dev/mapper/luks-UUID-of-the-above-filesystembefore telling you it is safe to remove the media. Because we have two layers here, it is pretty important to do that, instead of just yanking out the media when you think you're done.I prefer LUKS2 for Linux-only encrypted media, because everything is then handled in-kernel, not in userspace. And on this AMD Ryzen 7 7730U laptop, single-core decryption/encryption using the above settings reaches almost 3 GB/s, so it won't slow down my I/O either.I posted this because I reworked the full disk encryption on my laptop. There, I usepbkdf2key derivation for/bootbecause Grub cannot do argon2 yet –– this means even my/bootis encrypted, and Grub prompts me for the passphrase even before I can select what to boot! –– and Argon2id for other filesystems (LVM2 on top of LUKS2 encrypted partition). Again, the idea is to make the system unusable for any casual thief or snooping person; I don't actually have anything more sensitive than personal emails and such.