How to Encrypt a Disk on Ubuntu with LUKS
Encrypt a data disk or a container file on Ubuntu with LUKS and cryptsetup, unlock it after a restart, back up the header, and know what encryption does on a VPS.
3 min readBy Soumya
Reviewed by the AmazingRDP technical team

Need a server where you control the disks? A Linux VPS gives you root access to set up encrypted storage. See Linux VPS from $6.99
Disk encryption turns everything on a disk into unreadable data until the right passphrase is entered. On Ubuntu the standard tool is LUKS, used through the command cryptsetup. It protects data at rest: a stolen laptop, a disk sent for repair, a backup or disk image that ends up in the wrong hands. It does not protect a system that is running and unlocked.
Three ways to use it
| Way | Good for | Note |
|---|---|---|
| Whole system, chosen in the installer | Laptops and desktops | The passphrase is typed at every start |
| A separate data disk or partition | Servers and VPS | The system starts normally; you unlock the data when needed |
| A container file | Any machine, no spare disk needed | A single file that holds an encrypted volume |
The first way is a tick box in Ubuntu's installer ("Encrypt the new Ubuntu installation", or the LVM-with-LUKS option on Ubuntu Server) and cannot be added to a running system afterwards. On a remote server it is awkward: someone has to type the passphrase on the console after every restart. That is why servers normally use the second or third way, described here.
Encrypt a data disk or partition
This erases the partition. Find the right one first; a new second disk usually appears as sdb or vdb.
sudo apt install cryptsetup
lsblk -f
Create the encrypted volume. You type YES in capitals and choose a passphrase:
sudo cryptsetup luksFormat /dev/sdb1
Open it under a name of your choice, put a file system on it, and mount it:
sudo cryptsetup open /dev/sdb1 securedata
sudo mkfs.ext4 /dev/mapper/securedata
sudo mkdir -p /mnt/secure
sudo mount /dev/mapper/securedata /mnt/secure
Files written to /mnt/secure are now stored encrypted. To lock it again:
sudo umount /mnt/secure
sudo cryptsetup close securedata
After a restart
The volume is locked after every restart. Unlock it by hand with the open and mount lines above, or make that shorter with two entries. In /etc/crypttab (get the UUID from sudo blkid /dev/sdb1):
securedata UUID=1234abcd-12ab-34cd-56ef-1234567890ab none luks,noauto
and in /etc/fstab:
/dev/mapper/securedata /mnt/secure ext4 noauto 0 0
Then one pair of commands unlocks and mounts:
sudo cryptdisks_start securedata
sudo mount /mnt/secure
noauto matters on a server: without it the system stops at start-up and waits for the passphrase on a console you may not be watching. See mount and automount drives in Linux for more on fstab.
You will find guides that store a key file on the server so that the volume opens by itself. That is convenient, and it removes most of the protection: whoever gets the disk gets the key with it.
An encrypted container file
No spare disk? A file can hold the encrypted volume:
fallocate -l 5G ~/vault.img
sudo cryptsetup luksFormat ~/vault.img
sudo cryptsetup open ~/vault.img vault
sudo mkfs.ext4 /dev/mapper/vault
sudo mkdir -p /mnt/vault
sudo mount /dev/mapper/vault /mnt/vault
Closing it is the same as above. The file can be copied to another machine or to backup storage and stays encrypted there.
Look after the header and the passphrase
sudo cryptsetup luksDump /dev/sdb1
sudo cryptsetup luksAddKey /dev/sdb1
sudo cryptsetup luksHeaderBackup /dev/sdb1 --header-backup-file sdb1-header.img
The first line shows the volume's details and how many passphrases it has. The second adds another passphrase, for a colleague or as a spare. The third saves the header: the small area at the start of the volume that holds the keys. If it is damaged, the data is gone, passphrase or not, so keep the backup file somewhere else.
There is no recovery. A forgotten passphrase means the data is lost; nobody, including your provider, can open the volume.
What encryption does and does not do on a VPS
- It does keep your data unreadable in disk images, snapshots and backups, and on the physical disk after your server is deleted.
- It does not hide data from someone who gets into the running system while the volume is open. Passwords, updates and a firewall remain the first line.
- A virtual machine runs on hardware the provider controls. Encryption inside the VM raises the effort needed to read your data and is not an absolute barrier against the host.
- Encrypt what needs it (customer data, keys, backups) and keep a tested backup of it.
Our Linux VPS plans come with root access for a setup like this. Related: Linux data recovery tools and setting file permissions with setfacl.
Questions
How do I encrypt a disk on Ubuntu?
Install cryptsetup, run cryptsetup luksFormat on the partition, open it with cryptsetup open, then create a file system and mount it.
Can I encrypt Ubuntu after installation?
The system disk only by reinstalling with the encryption option. A data disk or a container file can be encrypted at any time.
What happens if I forget my LUKS passphrase?
The data cannot be recovered. Add a second passphrase and keep it somewhere safe.
Does disk encryption slow down a server?
On current processors the difference is small for most work, because they have built-in support for the encryption used.