Jump to content

Featured Replies

Posted

You are reading Part 42 of the 57-part series: Harden and Secure Linux Servers. [Level 5]

This series covers progressive security measures, from fundamental hardening techniques to enterprise-grade defense strategies. Each article delves into a specific security practice, explaining its importance and providing step-by-step guidance for implementation.

To explore more security best practices, visit the main guide for a full breakdown of all levels and recommendations.

Encrypting disk partitions ensures data remains protected even if the system is compromised or stolen. Disk encryption:

Prevents unauthorized access to sensitive files.
Protects against data theft from stolen drives or physical attacks.
Ensures compliance with security regulations (e.g., PCI-DSS, GDPR, HIPAA).

By using LUKS (Linux Unified Key Setup), you can securely encrypt disk partitions without affecting system performance.

How to Encrypt Disk Partitions Using LUKS

1. Install LUKS and Cryptsetup

LUKS is the default encryption method for Linux partitions. Ensure cryptsetup is installed:

For Debian/Ubuntu:

sudo apt update && sudo apt install cryptsetup -y

For CentOS/RHEL:

sudo yum install cryptsetup -y
2. Identify the Partition to Encrypt

List all available disk partitions:

lsblk

Example output:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  500G  0 disk  
├─sda1   8:1    0  100M  0 part /boot
└─sda2   8:2    0  499G  0 part /
sdb      8:16   0  1TB   0 disk  
└─sdb1   8:17   0  1TB   0 part  /mnt/data

In this example, /dev/sdb1 is an unmounted partition we will encrypt.

⚠️ Warning: Encrypting a partition will erase all existing data! Backup important files first.

3. Encrypt the Partition with LUKS

Run the following command to encrypt the partition:

sudo cryptsetup luksFormat /dev/sdb1

You'll be prompted to create a passphrase.
🔹 Use a strong passphrase and store it securely.

4. Open and Format the Encrypted Partition

To use the encrypted partition, it must be unlocked and formatted.

  1. Unlock the encrypted partition:

    sudo cryptsetup luksOpen /dev/sdb1 encrypted_partition
    

    (This maps /dev/sdb1 to /dev/mapper/encrypted_partition.)

  2. Format the unlocked partition with ext4:

    sudo mkfs.ext4 /dev/mapper/encrypted_partition
    
  3. Create a mount point:

    sudo mkdir /mnt/secure
    
  4. Mount the encrypted partition:

    sudo mount /dev/mapper/encrypted_partition /mnt/secure
    
5. Automatically Mount the Encrypted Partition at Boot

If you want the encrypted partition to mount automatically at startup, do the following:

  1. Store the LUKS passphrase in a secure key file (optional):

    sudo dd if=/dev/urandom of=/root/luks-keyfile bs=1024 count=4
    sudo chmod 600 /root/luks-keyfile
    sudo cryptsetup luksAddKey /dev/sdb1 /root/luks-keyfile
    
  2. Add the partition to /etc/crypttab:

    sudo nano /etc/crypttab
    

    Add:

    encrypted_partition  /dev/sdb1  /root/luks-keyfile  luks
    
  3. Modify /etc/fstab to mount at startup:

    sudo nano /etc/fstab
    

    Add:

    /dev/mapper/encrypted_partition  /mnt/secure  ext4  defaults  0  2
    
  4. Update initramfs to ensure LUKS unlocks at boot:

    sudo update-initramfs -u
    
6. Lock and Unlock the Encrypted Partition

When not in use, the encrypted partition should be unmounted and locked to prevent access.

To Lock the Partition:
sudo umount /mnt/secure
sudo cryptsetup luksClose encrypted_partition

(This securely locks the encrypted partition.)

To Unlock and Mount Again:
sudo cryptsetup luksOpen /dev/sdb1 encrypted_partition
sudo mount /dev/mapper/encrypted_partition /mnt/secure
7. Backup and Restore the LUKS Header

The LUKS header is critical for decryption. If corrupted, data will be permanently lost.

Backup the LUKS Header
sudo cryptsetup luksHeaderBackup /dev/sdb1 --header-backup-file /root/luks-header-backup.img
Restore the LUKS Header (if necessary)
sudo cryptsetup luksHeaderRestore /dev/sdb1 --header-backup-file /root/luks-header-backup.img

(Store the header backup on a separate secure device, not on the encrypted drive.)

Best Practices for Secure Disk Encryption

Use a strong passphrase and store it securely.
Backup encryption keys and the LUKS header to prevent permanent data loss.
Disable automatic unlocking for highly sensitive data.
Regularly check disk integrity using fsck and cryptsetup luksDump.
Use encrypted swap space to prevent data leakage (sudo cryptsetup luksFormat /dev/sdX for swap partitions).

By encrypting disk partitions, you ensure that sensitive data remains protected, even in cases of hardware theft, unauthorized access, or forensic analysis.

  • Views 51
  • Created
  • Last Reply

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Important Information

Terms of Use Privacy Policy Guidelines We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.