Dm-crypt
Encrypt removable data disk
ASSUMPTION
Disk is /dev/disk/by-label/backup. The whole disk will be used. We'll call the encrypted partition encBackup.
Encrypt data partition
Wipe the disk.
sudo cryptsetup open --type plain --key-file /dev/urandom /dev/disk/by-label/backup encBackup
# OR: sudo cryptsetup open --type plain --cipher aes-xts-plain64 --key-file /dev/urandom /dev/disk/by-label/backup encBackup
sudo dd if=/dev/zero of=/dev/mapper/encBackup status=progress bs=1M
sudo cryptsetup close encBackupCreate the encrypted partition. For stronger password, use one generated by pass.
Then open volume and create the file system.
pass bkphdd | sudo cryptsetup --key-file - luksFormat /dev/disk/by-label/backup
# OR: sudo cryptsetup --verify-passphrase luksFormat /dev/disk/by-label/backup
pass bkphdd | sudo cryptsetup --key-file - open /dev/disk/by-label/backup encBackup
# OR: sudo cryptsetup open /dev/disk/by-label/backup encBackup
sudo mkfs.ext4 /dev/mapper/encBackupMount and unmount
Mount
pass bkphdd | sudo cryptsetup --key-file - open /dev/disk/by-label/backup encBackup
# OR: sudo cryptsetup open /dev/disk/by-label/backup encBackup
sudo mount -o nodev,nosuid,noexec /dev/mapper/encBackup /media/hdd1Unmount
sudo umount /media/hdd1 && sudo sync
sudo cryptsetup close encBackupEncrypt swap
Steps without using encrypted root partition (some steps differ).
Useful links:
- Data at rest encryption
- Swap encryption
- Suspend and hibernate
- Root partition wrongly assumed to be encrypted
ASSUMPTION
Swap is on disk /dev/nvme0n1 and partition /dev/nvme0n1p2. We'll call the encrypted partition encSwap.
Encrypt swap partition
First disable swap if in use and disable automount.
sudo swapoff /dev/nvme0n1p2
sudo parted /dev/nvme0n1
(parted) print
(parted) set 2 no_automount onWipe the partition (not the disk).
sudo cryptsetup open --type plain --key-file /dev/urandom /dev/nvme0n1p2 encSwap
sudo dd if=/dev/zero of=/dev/mapper/encSwap status=progress bs=1M
sudo cryptsetup close encSwapCreate the encrypted partition, open volume and create the filesystem.
sudo cryptsetup luksFormat /dev/nvme0n1p2
sudo cryptsetup config --label=swap /dev/nvme0n1p2
sudo cryptsetup open /dev/disk/by-label/swap encSwap
sudo mkswap /dev/mapper/encSwapGRUB configuration
Now we configure the boot options on GRUB.
Update the configuration /etc/default/grub by:
- Adding
cryptdevice=/dev/disk/by-label/swap:encSwap - Update
resumetoresume=/dev/mapper/encSwap
GRUB_CMDLINE_LINUX_DEFAULT="audit=0 loglevel=3 quiet cryptdevice=/dev/disk/by-label/swap:encSwap resume=/dev/mapper/encSwap"TIP
For more details on the options used here, check mkinitcpio -H encrypt.
Regenerate the grub.cfg:
sudo grub-mkconfig -o /boot/grub/grub.cfgInitrd configuration
We use initrd to open swap. Update HOOKS in /etc/mkinitcpio.conf by adding encrypt somewhere before resume and filesystems:
HOOKS=(base udev setvtrgb autodetect modconf keyboard keymap block encrypt resume filesystems fsck)Regenerate the initramfs.
sudo mkinitcpio -p linuxFstab configuration
Finally configure fstab to mount the swap on boot. Add the following line to /etc/fstab:
/dev/mapper/encSwap swap swap defaults 0 0Using keyfile in USB drive
This step is optional.
Add the module vfat to /etc/mkinitcpio.conf:
MODULES=(vfat)And add cryptkey=device:fstype:path kernel option to /etc/default/grub:
cryptkey=/dev/disk/by-label/USB_LOADER:vfat:/swap_keyfileApply the changes.
sudo mkinitcpio -p linux
sudo grub-mkconfig -o /boot/grub/grub.cfgNow create the swap_keyfile in your USB drive with the password on it.
WARNING
Be careful to not put a new line at the end of the file! Text editors usually put it automatically, so use echo -n or a password generator command.
Encrypt home
ASSUMPTION
Home is on disk /dev/nvme0n1 and partition /dev/nvme0n1p4. We'll call the encrypted partition encHome.
WARNING
When creating the home partition, do not set the flag linux-home, or else it will conflict with the configuration in /etc/crypttab. (only password unlock will be available, even without configuration in /etc/crypttab)
Encrypt home partition
Wipe the partition as described in the sections above.
Create the encrypted partition, open volume and create the filesystem.
cryptsetup luksFormat /dev/nvme0n1p4
cryptsetup config --label=home /dev/nvme0n1p4
cryptsetup open /dev/disk/by-label/home encHome
mkfs.ext4 /dev/mapper/encHomeCrypttab configuration
Add the following line to /etc/crypttab:
# <name> <device> <password> <options>
encHome /dev/disk/by-label/home /home_keyfile:/dev/disk/by-label/USB_LOADERNow create the home_keyfile in your USB drive with the password on it.
TIP
Syntax for password is path:device, the inverse of the setting cryptkey without fstype used to encrypt swap.
WARNING
Be careful to not put a new line at the end of the file! Text editors usually put it automatically, so use echo -n or a password generator command.