WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Linux · Expert · question 69 of 100

How do you set up and manage disk encryption in Linux, such as LUKS or dm-crypt?

📕 Buy this interview preparation book: 100 Linux questions & answers — PDF + EPUB for $5

Disk encryption is an essential aspect of system security, and Linux offers several options for implementing it. Two of the most popular disk encryption methods are LUKS (Linux Unified Key Setup) and dm-crypt.

LUKS is a disk encryption specification that provides a standard format for encrypting entire partitions or disks. It uses a key derivation function to generate a master key from a passphrase, which is then used to encrypt the data on the disk. LUKS supports multiple key slots, allowing for backup or recovery keys to be added. The encrypted data is accessed by opening the LUKS device and mapping it to a plain device using the dm-crypt module.

Here are the steps to set up LUKS-based disk encryption in Linux:

Install the required packages:

    sudo apt-get install cryptsetup

Partition the disk or create a LVM logical volume for encryption.

Create a LUKS partition:

    sudo cryptsetup luksFormat /dev/sda1

This command creates a LUKS partition on /dev/sda1. You will be prompted to enter a passphrase.

Open the LUKS partition:

    sudo cryptsetup luksOpen /dev/sda1 my_encrypted_partition

This command maps the encrypted partition to a device named "my_encrypted_partition". You will be prompted to enter the passphrase again.

Create a file system on the mapped device:

    sudo mkfs.ext4 /dev/mapper/my_encrypted_partition

Mount the mapped device:

    sudo mkdir /mnt/my_encrypted_partition
    sudo mount /dev/mapper/my_encrypted_partition /mnt/my_encrypted_partition

Unmount and close the mapped device:

    sudo umount /mnt/my_encrypted_partition
    sudo cryptsetup luksClose my_encrypted_partition

dm-crypt is a Linux kernel-level disk encryption system that provides transparent encryption of block devices. It supports multiple encryption modes, including AES, Twofish, and Serpent, and can be used to encrypt entire disks, partitions, or individual files.

Here are the steps to set up dm-crypt-based disk encryption in Linux:

Install the required packages:

    sudo apt-get install cryptsetup

Partition the disk or create a LVM logical volume for encryption.

Create a dm-crypt partition:

    sudo cryptsetup --cipher aes-xts-plain64 --key-size 256 --hash sha256 --iter-time 2000 --use-random luksFormat /dev/sda1

This command creates a dm-crypt partition on /dev/sda1 using the AES-XTS encryption algorithm with a 256-bit key, the SHA-256 hashing algorithm, and a key derivation time of 2 seconds.

Open the dm-crypt partition:

    sudo cryptsetup luksOpen /dev/sda1 my_encrypted_partition

This command maps the encrypted partition to a device named "my_encrypted_partition". You will be prompted to enter a passphrase.

Create a file system on the mapped device:

    sudo mkfs.ext4 /dev/mapper/my_encrypted_partition

Mount the mapped device:

    sudo mkdir /mnt/my_encrypted_partition
    sudo mount /dev/mapper/my_encrypted_partition /mnt/my_encrypted_partition

Unmount and close the mapped device:

    sudo umount /mnt/my_encrypted_partition
    sudo cryptsetup luksClose my_encrypted_partition

In both cases, the encrypted data is decrypted on-the-fly when it is read from the disk and encrypted again when it is written back

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Linux interview — then scores it.
📞 Practice Linux — free 15 min
📕 Buy this interview preparation book: 100 Linux questions & answers — PDF + EPUB for $5

All 100 Linux questions · All topics