Create a LUKS encrypted EXT4 filesystem in a file under GNU/Linux

NOTE: This article assumes that you have the necessary software installed from your GNU/Linux distribution.

  • Do a "su -", or do everything here as root.
  • Create a file for the filesystem and then secure its permissions. This will create 2 GB file on the present working directory:

    dd of=encrypted bs=1G count=0 seek=2 # You may substitute an absolute path for "encrypted".

    chmod 600 encrypted # You may substitute an absolute path for "encrypted".

  • Do the following on the present working directory in the step above:

    mkdir /encrypted

    losetup /dev/loop0 encrypted # You may substitute an absolute path to the file "encrypted".

    # Note: The following 2 commands will ask you for input.

    cryptsetup -y luksFormat /dev/loop0 # A password for the encrypted filesystem will be asked here.

    cryptsetup luksOpen /dev/loop0 encrypteddev # This will show "/dev/mapper/encrypteddev".

    mkfs.ext4 /dev/mapper/encrypteddev # This will make a EXT4 filesystem on your encrypted file.

    mount /dev/mapper/encrypteddev /encrypted

  • To unmount your encrypted filesystem:

    umount /encrypted

    cryptsetup luksClose encrypteddev # This will remove "/dev/mapper/encrypteddev".

    losetup -d /dev/loop0

  • To mount your encrypted filesystem again (your present working directory should be where the "encrypted" file is located):

    losetup /dev/loop0 encrypted # You may substitute an absolute path to the file "encrypted".

    # Note: The following command will ask you for input.

    cryptsetup luksOpen /dev/loop0 encrypteddev # This will show "/dev/mapper/encrypteddev".

    mount /dev/mapper/encrypteddev /encrypted # You may substitute an absolute path to the file "encrypted"