Skip to main content

Complete Guide to setting up LUKS on LVM encryption in Arch Linux (Minimal System)

· Updated: Jan 15, 2025 · Arch Linux, LUKS, LVM, Encryption

Setting Up LUKS Encryption

Welcome to a detailed guide on setting up LUKS (Linux Unified Key Setup) encryption (LVM) as part of your Arch Linux installation process. LUKS encryption provides an additional layer of security for your data.

Enabling Time Synchronization

Before delving into encryption, it’s essential to ensure accurate timekeeping on your system. Enabling Network Time Protocol (NTP) synchronization will synchronize your system’s clock with remote servers.

timedatectl set-ntp true

Partition Setup

Before installing Arch Linux, you need to partition your disk. You can use tools like cfdisk, fdisk, or gdisk for this purpose. In this guide, we will use gdisk.

The parition layout will be using LVM, so there only contains 2 partitions.

First off, you need to know which storage devices you want to partition and that name of the device. You can list the available storage devices using the following command:

lsblk
  1. Initialize and Create Partitions: Utilize the gdisk tool to initialize your disk and create the necessary partitions:
gdisk /dev/sda

While inside the gdisk interface, execute the following actions:

Your disk should now have the following partitions:

SizeTypeCode
1GEFI Systemef00
100GLinux LVM8e00

Format Boot Partition

Our partition layout is ready, now we need to format the partitions. We will start by formatting the EFI boot partition. formatted as FAT32. This filesystem format aligns with UEFI booting requirements.

mkfs.fat -F32 /dev/sda1

Encrypt Your Partition (LUKS)

Configure encryption using LUKS (Linux Unified Key Setup) and create logical volumes to efficiently manage your filesystem.

cryptsetup luksFormat --type luks2 --cipher aes-xts-plain64 --hash sha256 --iter-time 10000 --key-size 256 --pbkdf argon2id --use-urandom --verify-passphrase /dev/sda2

YES

This command initiates the LUKS encryption process with specific parameters:

Follow the prompts and input your chosen passphrase when prompted. Remember that this passphrase will be required to unlock and access the encrypted partition.

Open the Encrypted Partition

LUKS partition is encrypted, for open it:

cryptsetup open /dev/sda2 crypt

This command opens the encrypted partition and maps it to the crypt device. and have a new mapper named as crypt.

Create Physical and Logical Volumes

Next, you’ll create physical and logical volumes to manage your filesystem efficiently. These volumes will serve as the foundation for your Arch Linux installation.

Create a physical volume:

pvcreate /dev/mapper/crypt

Create a volume group named vol:

vgcreate vol /dev/mapper/crypt

Create logical volumes for swap, root, and home directories:

lvcreate -L 12G vol -n swap
lvcreate -l 50%FREE vol -n root
lvcreate -l 100%FREE vol -n home

These commands create logical volumes for the swap, root, and home directories, allocating the desired percentage of space for each volume. For the

Format and Mount Partitions

In this step, we will format and mount the partitions necessary for the Arch Linux installation. Properly configuring these partitions is crucial for ensuring a stable and functional system. We’ll cover formatting the root and home volumes, as well as creating and enabling swap space. Let’s delve into the details:

Root and Home

We’ll need to format the root and home volumes with btrfs filesystem. To format the root and home volumes with the Btrfs filesystem, execute the following commands:

mkfs.btrfs /dev/vol/root && mkfs.btrfs /dev/vol/home

Swap

Swap space is an integral part of your system’s memory management. It provides additional virtual memory when physical RAM is fully utilized. Creating and enabling swap space ensures that your system can handle memory-intensive tasks without performance degradation.

To create and enable swap space on the designated swap logical volume, use these commands:

mkswap /dev/vol/swap && swapon /dev/vol/swap

Mount Root and Home Partitions

Time to mount the root and home partitions to the /mnt directory. Execute the following commands:

mount /dev/vol/root /mnt
mount /dev/vol/home --mkdir /mnt/home

The EFI partition will not be mounted here, we’ll mount it after chrooting into the new system.

Install Essential Packages

This is the time to install essential packages that form the core of your Arch Linux system. These packages provide foundational tools and utilities that enable system management, software development, and hardware compatibility.

pacstrap -i /mnt base base-devel linux linux-firmware linux-headers lvm2 vim networkmanager sudo

Automate Mounts with the fstab File

To ensure that your filesystems are automatically mounted during system boot, you need to generate the /etc/fstab file. This file contains information about your partitions and their mount points, enabling the system to mount them correctly.

genfstab -U /mnt >> /mnt/etc/fstab

Chroot into the New System

To enter this new system environment, type the following command:

arch-chroot /mnt

Initialize the Pacman Keyring

Better to refresh the pacman keyring, execute the following commands:

pacman-key --init && pacman-key --populate archlinux

Enable Network Services

Enable the NetworkManager service to manage network connections.

systemctl enable NetworkManager

Set the System Locale

Configuring the system locale is an essential task to ensure proper language support and effective localization within your Arch Linux environment. The system locale defines the language, character encoding, and other regional settings that your system will use.

Execute the following command to open the locale.gen file in the vim text editor:

vim /etc/locale.gen
  1. Inside the text editor, navigate to the line that corresponds to your desired locale. For instance, to enable the English (United States) locale, find the line containing en_US and remove the # symbol at the beginning of the line.

  2. Save the file and exit the text editor.

  3. Generate the selected locale by running the command:

locale-gen

This command generates the necessary locale files based on your configuration.

  1. Set the system’s default locale by entering the following command:
echo LANG=en_US.UTF-8 > /etc/locale.conf

Set User Passwords

User account management is a crucial aspect of system security. Follow these steps to establish secure passwords for both the root user and a new user:

  1. Set the root password by entering the following command and following the prompts:
passwd
  1. Create a new user account using the useradd command. Replace username with the desired username:
useradd -m username
  1. Set the password for the newly created user by running the following command and following the prompts:
passwd username

Basic group allocation

To ensure that your user account has the necessary permissions to perform system tasks, allocate the user to essential groups.

usermod -aG wheel,storage,power username

Configure sudoers file

sudo aren’t allowed by default, you need to enable it by editing the sudoers file.

EDITOR=vim visudo
  1. Uncomment the line %wheel ALL=(ALL) ALL by removing the # symbol at the beginning of the line.

Timestamp Timeout

To avoid the entering password delay every time, you can set the timestamp timeout to 0.

Defaults timestamp_timeout=0

Set Hostname

Assigning a hostname to your Arch Linux system is essential for network identification. As example, we will set the hostname to arch.

echo arch > /etc/hostname

Set Hosts File

To associate the hostname with the loopback address, modify the /etc/hosts file, adding the following line:

127.0.0.1 localhost
::1       localhost
127.0.0.1 arch.localdomain  localhost

Set Timezone

Configuring the correct timezone ensures accurate timekeeping on your Arch Linux system. to do this, create a symbolic link to the appropriate timezone file and synchronize the hardware clock with the system time. As example, we will set the timezone to Asia/Taipei.

ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime

Synchronize the hardware clock with the system time:

hwclock --systohc

Configure mkinitcpio

You need to configure mkinitcpio to include necessary modules for LVM2 and encryption support. This step ensures your encrypted partitions can be properly accessed during the boot process. Edit the /etc/mkinitcpio.conf file:

  1. Locate the HOOKS line and add lvm2 and encrypt to the list of hooks. Your modified line should look like this:

    HOOKS=(base udev autodetect modconf kms keyboard keymap consolefont block lvm2 encrypt filesystems fsck)
  2. Save the file and exit the text editor.

  3. Regenerate the initramfs with the updated configuration:

    mkinitcpio -p linux

Format and Mount EFI Partition

It’s time to format the EFI partition and mount it to the /boot/efi directory. Execute the following commands:

mkfs.fat -F32 /dev/sda1

and mount it:

mount /dev/sda1 --mkdir /boot/efi

Install and Configure Bootctl

Now, we’ll configure the systemd-boot bootloader to manage the boot process for your Arch Linux system.

  1. Install bootctl to the /boot/efi directory:

    bootctl --path=/boot/efi install
  2. Open the loader.conf file for editing using the vim text editor:

    vim /boot/loader/loader.conf
  3. Inside the text editor, add the following lines to set the default boot options:

    default arch
    timeout 10
    editor 0
  4. Save the file and exit the text editor.

  5. Create a boot entry for your Arch Linux installation in the bootloader configuration. This ensures that you can easily select Arch Linux during boot.

    vim /boot/loader/entries/arch.conf
  6. Inside the text editor, add the following lines to specify the Linux kernel and initramfs files:

    title Arch linux
    linux /vmlinuz-linux
    initrd /initramfs-linux.img
  7. Save the file and exit the text editor.

Add Encryption Options

To ensure that your encrypted partition is properly decrypted during the boot process, you need to add encryption options to the bootloader configuration. This step is crucial for seamless decryption and access to your encrypted root volume.

  1. Add the UUID of the encrypted partition to the bootloader configuration. First, obtain the UUID of the encrypted partition using the blkid command:

    blkid /dev/sda2 >> /boot/loader/entries/arch.conf
  2. Now, reopen the arch.conf file for further editing:

    vim /boot/loader/entries/arch.conf
  3. Refine the following lines within the file to precisely outline the encryption options, while replacing <UUID> with the actual UUID obtained in the previous step:

    options cryptdevice=UUID=<UUID>:cryptlvm root=/dev/vol/root quiet rw
  4. Save the file and exit the text editor.

Finalize Boot Configuration

And this is the sample of the arch.conf file, you should similar to this, if you done correctly.

title Arch linux
linux /vmlinuz-linux
initrd /initramfs-linux.img

options cryptdevice=UUID=<UUID>:cryptlvm root=/dev/vol/root quiet rw

Finish Installation

After completing these steps, Now exit your current user then umount your arch system. You can enjoy your new Arch Linux system with LUKS encryption. (But no GUI XD)

exit
umount -R /mnt

you can proceed to reboot your system.

reboot

References