How to Encrypt a Linux File System with DM-Crypt - Linux.com (2024)

Editor’s Note: File system as two words refers to the layout of your directories, which includes things like the /dev directory that represent different hardware devices and don’t actually refer to files on your disk drive. And “filesystem” refers to the software that manages the files and directories.

How to Encrypt a Linux File System with DM-Crypt - Linux.com (1)One common question we get from Linux.com readers is about how to implement a file system encryption method for Linux. Before we dive into this, I want to make two points:

First, it’s hard to find decent information on the web about this. So I’m going to point you to a couple of really good resources I managed to find.

Second, it’s important to understand the technical details of the problem. And that’s what I’m going to address here, after which I’ll give you some pointers on how to accomplish the encryption, and then point you to the other resources.

People from time to time say they want to encrypt their data, but there’s a fundamental part missing in what they’re asking: What exactly do they want to encrypt? Do they want to encrypt their data from within a software package, and then store that data to the hard drive in a single file? For example, would they like LibreOffice to create an entire .odt word processing document, and encrypt it, and then write the encrypted results to the file system as a single file, as in the following figure? Or would they like Linux to take care of the encryption itself at the file system level?

In the case of Linux taking care of it, LibreOffice would have to do nothing other than read and write the files as it currently does. Linux would encrypt the file before actually writing it to the disk, and decrypt it after reading it back. This is the approach I’m taking here, but for this there are many other questions you need to ask. And to ask the right questions, you need to understand how block storage works. Let’s look at that first.

Block Level Storage

When an operating system works with a local drive, the operating system uses filesystem software to format the drive, and then read and write individual sectors. When you save a file, the filesystem software figures out the sectors to write to. When you read a file, the filesystem figures out which sectors the data is on, and then reads those sectors and reconstructs the file for you. To manage the files, the filesystem uses different types of indexes that it also stores on the disk. Different filesystems use different means to organize the data, and also include various security mechanisms; the end result is different file systems such as ext4 and NTFS.

Low-Level Details

How to Encrypt a Linux File System with DM-Crypt - Linux.com (2)Now that we’re clear on how block-level devices work, consider this: The operating system uses its filesystem software to write the sectors of data to the drive. The filesystem software determines where to write the sectors of data and how to organize them, including the creation of metadata that describes the filenames and how they’re organized, and so on. But in order for the filesystem software to perform the actual read and writes to the drive, there needs to be present a device driver that does the actual controlling of the device itself, as shown in the left side of the next figure. (The drivers are represented in your file system hierarchy within the /dev directory.)

Right at this point–the spot from the filesystem software to the device driver–there’s a choice to be made in encryption: Do you want the filesystem software to do the encrypting before writing the data? Or, how about we effectively wedge a piece of software in between the filesystem software and the device driver? This way, the filesystem will operate as it normally does, but when it tries to access the device, its calls instead will be handled by encryption software, as shown on the right side of the following figure. This how we’re going to do it for this article. But first let’s talk about a few more issues.

(Incidentally, if you want to see how device drivers exist in the /dev directory of your Linux system, check out this article. It covers programming, but if you aren’t a programmer, click to page 2, and scroll down to the section labeled Hello, World! Using /dev/hello_worldand read the first paragraph for a glorious explanation.)

If you want to encrypt an entire partition, you might consider encrypting the entire drive. However, there’s one little problem there. If the computer boots from the drive, the drive needs a small portion dedicated to the bootup code. This bootup code is machine code that the computer reads in and executes to boot the computer. If the entire hard drive is encrypted, including this data, the computer would need some way to decrypt the data. But the computer doesn’t have a file system loaded yet, so it can’t read a program that decrypts it. See where this is going? The decryption code would need to be in the BIOS itself. But most computers don’t have such code. And that means the boot record really can’t be encrypted (although peoplehave discussedvarious waysaround this problemsuch as putting the bootup on a removable USB drive, as well as how to solve other technical issues).

Remote Drives

If your drive is remote, there are ways you can access the data; this is important in understanding what type of encryption is available to you. The two ways are:

  • Block-level storagejust like with local drives, whereby your filesystem software can read and write directly to the sectors on the remote disk

  • File-level storagewhereby your operating system sends files to a remote server, which has its own operating system and filesystem software; this remote server in turns writes the files to its disk.

With file-level storage, you don’t have many choices regarding encryption. If you want to encrypt the data, you need to encrypt it in your application before sending it on to the remote server for storage.

But with block-level remote storage you do have options. For example, if you’re using a cloud hosting service whereby you can attach different volumes to a server you allocated, you’re usually using block-level storage. The volumes aren’t necessarily attached physically to your hosted server; yet, the server can access them as if they are, and format the volume and read and write individual sectors just as if the drive is mounted locally. What this means is with block-level remote storage you can perform encryption at the file-system level just as you might on your local computer and local drive.

The software

Now we know what we want to accomplish; the question is, how do you do it? It turns out Linux has a software package built in that uses the method I explained earlier of wedging software in between the filesystem software and the device drivers. The software is called dm-crypt. And dm-crypt encrypts the data and writes it onto the storage device (by way of the device driver) using a storage format called LUKS.

LUKS (Linux Unified Key Setup) is the format used on the drive itself, and is essentially used in place of a file system such as ext4. The dm-crypt system sits between the filesystem software; the filesystem software reads and writes ext4, and the ext4 data gets pushed through dm-crypt which then stores the data in LUKS format on the drive. Thus, you can effectively have a file system such as ext4 or NTFS sitting “on top of” the encrypted LUKS format.

Note that dm-crypt is the name of the subsystem, and that you use various tools to work with it. There is no single command called dm-crypt. There are some programs you can use to manage dm-crypt:

Note that dm-crypt is the name of the subsystem, and that you use various tools to work with it. There is no single command called dm-crypt. There are some programs you can use to manage dm-crypt:

Other Features

One cool thing about the dm-crypt system is that it doesn’t have to work directly with a disk driver. Instead, it can save all the data into a single file instead of using LUKS and a whole disk partition. What that means is you can have dm-crypt create a single file within which you could create an entire file system. Then you can mount that single file as a separate drive, and then access it from any software just like you would any other drive.

Cloud Drives

Because some cloud providers (such as Amazon Web Services) give you full roo access to the block devices connected to your servers, you can make use of dm-crypt; you can format a block device with the LUKS format, and then prepare it for your dm-crypt system; then you can format it all with an ext4 file system. The end result is a fully encrypted drive living in the cloud, one that you manage yourself. Want to try it?Here’s a tutorial on doing it using the cryptsetup program.

Some other cloud providers don’t give you direct access to the block device as AWS does. For example, Digital Ocean does not give you direct access; but you can still create a file and set up dm-crypt to use that file, and then create what they call a “container” within the file that represents the file system. In fact, the process of this is the same way you would accomplish creating an encrypted container file on your own local machine. Andhere’s Digital Ocean’s tutorialon creating a dm-crypt LUKS container file. Notice in this tutorial that just like with the block device, you create an entire file system (such as ext4) but in this case that file system lives inside the container file.

Local Drives

And that brings us to how to accomplish all this locally. The tutorial above for creating an encrypted drive on Amazon is the same steps for creating it locally on one of your own hard drives. Buthere’s another tutorialthat gives step-by-step instructions for doing it locally on your own hard drive, also using cryptsetup.

If you want to create a local container drive that contains an entire encrypted file system, just follow the steps in the Digital Ocean tutorial above.

Or if you want to use the other program, cryptmount, to encrypt an entire partition or create a container file, followthis tutorial; the author, Carla Schroder, knows her stuff and provides some good steps.

Conclusion

That’s about it. The important thing about knowing how to encrypt is to fully understand first what you’re really trying to accomplish – have an application encrypt and decrypt its data, have the operating system handle the encryption; and whether to encrypt an entire partition or just individual files; and whether to create a container to hold the encrypted files. Then you can follow the steps on the tutorials I linked to here and get the job done right.

How to Encrypt a Linux File System with DM-Crypt - Linux.com (2024)

FAQs

How to encrypt a file system in Linux? ›

Use the appropriate commands (gpg --encrypt or right-click and select Encrypt) to encrypt the files in linux, and check for the ". gpg" or ". pgp" extension on the encrypted files. When decrypting files, provide the correct passphrase or key to successfully access the original content.

What is dm-crypt in Linux? ›

Dm-crypt is a Linux kernel-level encryption mechanism that allows users to mount an encrypted file system. Mounting a file system is the process in which a file system is attached to a directory (mount point), which makes it available to the operating system.

What is the best way to encrypt Linux? ›

10 Best Linux File and Disk Encryption Tools (2024)
  1. Tomb. Tomb is a free and open-source tool for easily encrypting and backing up files on GNU/Linux systems. ...
  2. Cryptmount. ...
  3. CryFS. ...
  4. GnuPG. ...
  5. VeraCrypt. ...
  6. EncFS. ...
  7. 7-zip. ...
  8. dm-crypt.
Feb 28, 2024

What Linux encryption tool can be used to enable Linux file system encryption? ›

dm-crypt is the standard device-mapper encryption functionality provided by the Linux kernel. It can be used directly by those who like to have full control over all aspects of partition and key management.

How do you encrypt a file system? ›

Right-click (or press and hold) a file or folder and select Properties. Select the Advanced button and select the Encrypt contents to secure data check box.

How does DM crypt work? ›

The dm-crypt device mapper target resides entirely in kernel space, and is only concerned with encryption of the block device – it does not interpret any data itself. It relies on user space front-ends to create and activate encrypted volumes, and manage authentication.

What is the difference between LUKS and dm-crypt? ›

LUKS features like key management with multiple passphrases/key-files or re-encrypting a device in-place are unavailable with plain mode. Plain dm-crypt encryption can be more resilient to damage than LUKS, because it does not rely on an encryption master-key which can be a single-point of failure if damaged.

When using the dm-crypt command, which type of encryption is used by default? ›

The default cipher for LUKS is nowadays aes-xts-plain64 , i.e. AES as cipher and XTS as mode of operation.

Is TrueCrypt still available? ›

TrueCrypt is a discontinued source-available freeware utility used for on-the-fly encryption (OTFE).

Can Linux be encrypted? ›

Linux devices can be encrypted in one of two ways: Full-disk encryption: Encrypting the block device before it is mounted on the system. File-based encryption: Encrypting only a folder or file using native filesystem features.

How to encrypt files and folders in Linux? ›

One way to encrypt a directory using a symmetric key in Linux is by using the "tar" and "gpg" utilities together. This will prompt you to enter and verify a passphrase, which will be used as the symmetric key.

What is the command mostly commonly used to encrypt files in Linux? ›

gpg. One of the standard and most well know tools for encrypting files on Linux is gpg.

How to tell if a file is encrypted in Linux? ›

If you want to check if a file is encrypted, use the getFileInfo() method that returns the file type and flag that indicates if the file is encrypted.

Does Linux have full disk encryption? ›

In Linux environment Linux Unified Key Setup (LUKS) is used for encrypting entire block devices, hard drive, SSDs and even removable storage drives. Full hard drive encryption is possible only during the installation of the Linux operating system. In this case it will encrypt both the swap space and system partitions.

Is LUKS encryption at rest? ›

Disk-encryption solutions such as LUKS protect the data only when your system is off.

How do I zip and encrypt a file in Linux? ›

You can accomplish this with gpg .
  1. gpg -c filename.zip.
  2. Enter passphrase to encrypt with.
  3. You should now have a file filename.zip.gpg.
  4. Move or delete the original file.
  5. Decrypt your new file with gpg filename.zip.gpg + entering your passphrase.
  6. It should now create a non- .gpg version of the file for you to access normally.
Oct 16, 2020

How to encrypt a file in Unix? ›

Encrypt/Decrypt Files in Linux using Ccrypt
  1. -e, –encrypt :Encrypt. This is the default mode. ...
  2. -d, –decrypt: Decrypt. ...
  3. -c, –cat: Decrypt one or more files to standard output. ...
  4. -x, –keychange: Change the key of encrypted data. ...
  5. -u, –unixcrypt: Simulate the old unix crypt command.
May 15, 2019

How to encrypt root filesystem on Linux? ›

How To Encrypt Root Filesystem on Linux
  1. Creating Basic Disk Layout.
  2. Creating LUKS & LVM partitions on disk. ...
  3. Transfer Entire Filesystem to Encrypted Disk.
  4. Install and Configure GRUB Bootloader.
  5. Re-install GRUB Stage 1 & 2. ...
  6. Modify crypttab and fstab files.
  7. Re-configure initramfs image.
  8. Boot on Encrypted Device.
Jan 3, 2021

Can you encrypt Linux? ›

Linux devices can be encrypted in one of two ways: Full-disk encryption: Encrypting the block device before it is mounted on the system. File-based encryption: Encrypting only a folder or file using native filesystem features.

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5896

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.