While there are reasons not to encrypt swap, I would also argue that there are reasons a person might want to. Tightening security (physical or digital) always restricts and slows access. That's not a reason to avoid a given security measure. As pointed out in another answer, when RAM is full, it is desirable that swap work as quickly as possible.
That being said, if the content that may reside in swap is sensitive enough there may be cases in which the performance trade-off may be justified. By default, the ubuntu installer (I checked 18.04 and 16.04) creates the swap partition in the LUKS-encrypted, LVM2 partition when selecting FDE during installation. Hence, swap is encrypted.
If for some reason, your swap partition is not encrypted, you can still encrypt it. If you're using LVM, and the LVM partition is already LUKS encrypted, you could simply resize the file systems within LVM to make space for swap and then create a logical volume for swap. The resizing of the filesystems may need to be done from a bootable USB. Update your /etc/fstab
file and you're done.
If you want to encrypt an existing non-encrypted swap partition, that is also possible, but takes a little more work. I'll list out the general steps.
- While booted into your system,
swapoff
your swap partition.
- Use
cryptsetup luksFormat ...
to encrypt the former swap partition.
- Generate a key file to use for decryption and add it as a decryption key.
- Decrypt the LUKS partition you just set up.
- run
mkswap
on the decrypted partition.
- Optionally remove the password you used when you set up LUKS encryption.
- Save your key file as
/root/keyfile
- Update your
/etc/crypttab
file to include an entry that decrypts your swap partition using the key file, /root/keyfile
.
- update your
/etc/fstab
file to mount the encrypted swap (you need to get the UUID of the decrypted swap partition.)
- Run
sudo update-initramfs -k all -c
to recreate your boot files
- Finally, run
sudo update-grub
Now swap is encrypted. When you boot, you'll put in the password to decrypt /
. With root decrypted, /root/keyfile
can be used by the system to automatically decrypt your swap partition.
There is a great write-up here about encrypting multiple partitions with a single passphrase. Since you're only trying to encrypt a swap partition, you can do it while booted into your installed system.
/boot
partition. If you're running an EFI system, there should be exactly 2 partitions that are not encrypted, the/boot
and/boot/efi
partitions. – b_laoshi Oct 16 '18 at 00:50