Ubuntu - How to encrypt swap partition

Create partition for swap (/dev/sdaX in this example). Prepare and enable it using mkswap and swapon commands. If you already use swap partition then omit these steps.

$ sudo mkswap /dev/sdaX
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=325d9718-8532-460d-afec-74e6aee9ae5f
$ sudo swapon /dev/sdaX

Execute ecryptfs-setup-swap script (it is part of ecryptfs-utils package):

$ sudo ecryptfs-setup-swap 

WARNING:
An encrypted swap is required to help ensure that encrypted files are not leaked
to disk in an unencrypted format.

HOWEVER, THE SWAP ENCRYPTION CONFIGURATION PRODUCED BY THIS PROGRAM 
WILL BREAK HIBERNATE/RESUME ON THIS SYSTEM!

NOTE: Your suspend/resume capabilities will not be affected.

Do you want to proceed with encrypting your swap? [y/N]: y

INFO: Setting up swap: [/dev/sdaX]
 * Stopping remaining crypto disks...                                    
 * cryptswap1 (stopped)...                                                [ OK ]
 * Starting remaining crypto disks...                                    
 * cryptswap1 (starting)..
 * cryptswap1 (started)...                                                [ OK ]
INFO: Successfully setup encrypted swap!

Changes are automatically applied to crypttab and fstab configuration files.

$ cat /etc/crypttab | grep /dev/sdaX
cryptswap1 /dev/sdaX /dev/urandom swap,cipher=aes-cbc-essiv:sha256
$ cat /etc/fstab | grep cryptswap1
/dev/mapper/cryptswap1 none swap sw 0 0
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu