zimazui

freedom & security


Disk encryption with LUKS

Let’s encrypt your disk in Linux with LUKS

Source: How To Linux Hard Disk Encryption With LUKS cryptsetup encrypt command

Configure LUKS partition

First we generate the private key:

$ cryptsetup -y -v --type luks2 luksFormat /dev/sdc

Now the volume can be decrypted and initialized with the following command:

$ cryptsetup luksOpen /dev/sdc backup1

The mapping name in /dev/mapper can be seeing with the command:

$ ls -l /dev/mapper/backup1

Now the following command allows to check the mapping status:

$ cryptsetup -v status backup1

And the LUKS headers can be dumped using the following command:

$ cryptsetup luksDump /dev/sdc

Format Linux partition

First, zeros need to be written to the encrypted device with the following command:

$ dd if=/dev/zero of=/dev/mapper/backup1

The progress can be monitored using the pv command as follows:

$ pv -tpreb /dev/zero | dd of=/dev/mapper/backup1 bs=128M

It is also possible to pass the status=progress option to the dd command:

$ dd if=/dev/zero of=/dev/mapper/backup1 status=progress

Next, the ext4 filesystem can be created:

$ mkfs.ext4 /dev/mapper/backup1

And now we can finally mount our new filesystem:

$ mount /dev/mapper/backup1 /mnt/backup1

Unmount and close (secure) encrypted partition

The partition can be unmounted and the encrypted device can be closed securing our data using the following commands:

$ umount /mnt/backup1
$ cryptsetup luksClose backup1

Re-mount encrypted volume

The encrypted volume can be mounted again using the following commands:

$ crpytsetup luksOpen /dev/sdc mybackup
$ mount /dev/mapper/mybackup /mnt/backup1
$ df -H
$ mount

Conclusion

Encrypting and managing your disks in Linux with LUKS it is not so complicated. Enjoy! :D