An encrypted disk can only be read with your passphrase. If the disk ever leaves your server, for example when it is replaced, nobody can read your data on it. This page encrypts an additional, empty data disk with LUKS, the standard encryption of Linux. The commands were tested on Ubuntu 26.04 LTS with a 4 TB data disk; run them as root.

⚠ This erases the disk

Encrypting a disk this way deletes everything on it. Use an empty disk, or copy its data elsewhere first. If you lose the passphrase, the data cannot be recovered, not even by our support.


1. Find the Empty Disk

lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS

An empty disk has no partitions, no FSTYPE and no MOUNTPOINTS, for example sdc. Make sure it is not the system disk or part of a RAID. Below, replace /dev/sdc with your disk. More about disks: How to Use Additional Disks on a Linux Server.

2. Encrypt It

cryptsetup luksFormat /dev/sdc

cryptsetup warns that the data will be overwritten; type YES in capital letters. Then enter a long passphrase twice and keep it in your password manager.

The disk gets LUKS version 2 with aes-xts-plain64 and a 512-bit key; cryptsetup luksDump /dev/sdc shows it.

3. Open It and Create a File System

cryptsetup open /dev/sdc data
mkfs.ext4 -L data /dev/mapper/data
mkdir /data
mount /dev/mapper/data /data

cryptsetup open asks for the passphrase and makes the opened disk available as /dev/mapper/data. You only create the file system once. Now /data works like any other folder; lsblk shows the disk as crypto_LUKS with data of the type crypt below it.

A wrong passphrase gives No key available with this passphrase.

4. Close It and Open It Again

umount /data
cryptsetup close data

After closing, the data cannot be read. To use it again, open and mount it:

cryptsetup open /dev/sdc data
mount /dev/mapper/data /data

Your files are there again.

5. After a Restart: Two Ways

Open it by hand (more secure). After each restart of the server, log in over SSH and run the two commands from step 4. Until you do, the data cannot be read, also by programs on the server: start programs that use /data only after that.

Open it automatically. A key file on the system disk opens the data disk at every start. This protects the data disk when it leaves the server on its own, but not against someone who can log in to the running server. Create the key, add it to the disk (the command asks for your passphrase) and tell the system about it:

mkdir -m 700 /etc/cryptsetup-keys.d
dd if=/dev/urandom of=/etc/cryptsetup-keys.d/data.key bs=512 count=1
chmod 400 /etc/cryptsetup-keys.d/data.key
cryptsetup luksAddKey /dev/sdc /etc/cryptsetup-keys.d/data.key
blkid -s UUID -o value /dev/sdc

Add one line to /etc/crypttab, with the UUID from the last command, and one to /etc/fstab:

# /etc/crypttab
data UUID=your-uuid /etc/cryptsetup-keys.d/data.key luks,nofail

# /etc/fstab
/dev/mapper/data /data ext4 defaults,nofail 0 2

nofail lets the server start even if the disk cannot be opened. Test it without restarting: close the disk as in step 4, then run systemctl daemon-reload, systemctl start systemd-cryptsetup@data.service and mount -a. The disk is then opened with the key file and mounted on /data. Your passphrase keeps working as well.

6. Back Up the LUKS Header

The first part of the disk, the header, holds the keys. If it gets damaged, the passphrase does not help any more. Save a copy and keep it off the server, together with your passphrase:

cryptsetup luksHeaderBackup /dev/sdc --header-backup-file /root/sdc-luks-header.img

The copy has 16 MB. Anyone with this copy and a passphrase that was valid at that time can open the disk, so keep it as safe as the passphrase.

Speed

Processors with AES support encrypt fast: cryptsetup benchmark -c aes-xts-plain64 -s 512 shows the speed, for example about 1.5 GB/s, more than a hard disk can read or write. grep -c -w aes /proc/cpuinfo prints a number greater than 0 if the processor has it.


Related Guides

View all Linux guides

Was this answer helpful? 0 Users Found This Useful (0 Votes)