Difference between revisions of "Dm-crypt/Encrypting a non-root file system"

From ArchWiki
Jump to navigation Jump to search
m (spanish interlanguage link)
m (→‎Without losetup: Highlighting important information about minimum size requirement)
 
(22 intermediate revisions by 10 users not shown)
Line 1: Line 1:
 
{{Lowercase title}}
 
{{Lowercase title}}
 
[[Category:Disk encryption]]
 
[[Category:Disk encryption]]
[[es:Dm-crypt/Encrypting a non-root file system]]
+
[[es:Dm-crypt (Español)/Encrypting a non-root file system]]
 
[[ja:Dm-crypt/root 以外のファイルシステムの暗号化]]
 
[[ja:Dm-crypt/root 以外のファイルシステムの暗号化]]
 
[[pl:Dm-crypt/Encrypting a non-root file system]]
 
[[pl:Dm-crypt/Encrypting a non-root file system]]
 +
[[pt:Dm-crypt (Português)/Encrypting a non-root file system]]
 +
[[zh-hant:Dm-crypt/Encrypting a non-root file system]]
 +
[[zh-hans:Dm-crypt/Encrypting a non-root file system]]
 
The following are examples of encrypting a secondary, i.e. non-root, filesystem with dm-crypt.   
 
The following are examples of encrypting a secondary, i.e. non-root, filesystem with dm-crypt.   
 
== Overview ==  
 
== Overview ==  
Line 16: Line 19:
  
 
First make sure the partition is empty (has no file system attached to it). Delete the partition and create an empty one if it has a file system. Then prepare the partition by securely erasing it, see [[Dm-crypt/Drive preparation#Secure erasure of the hard disk drive]].  
 
First make sure the partition is empty (has no file system attached to it). Delete the partition and create an empty one if it has a file system. Then prepare the partition by securely erasing it, see [[Dm-crypt/Drive preparation#Secure erasure of the hard disk drive]].  
 +
 +
Create the partition which will contain the encrypted container.
  
 
Then setup the LUKS header with:
 
Then setup the LUKS header with:
  # cryptsetup ''options'' luksFormat --type luks2 ''device''
+
  # cryptsetup ''options'' luksFormat ''device''
  
 
Replace {{ic|''device''}} with the previously created partition. See [[Dm-crypt/Device encryption#Encryption options for LUKS mode]] for details like the available {{ic|''options''}}.
 
Replace {{ic|''device''}} with the previously created partition. See [[Dm-crypt/Device encryption#Encryption options for LUKS mode]] for details like the available {{ic|''options''}}.
Line 69: Line 74:
 
Using losetup directly can be avoided completely by doing the following [https://wiki.gentoo.org/wiki/Custom_Initramfs#Encrypted_keyfile]:
 
Using losetup directly can be avoided completely by doing the following [https://wiki.gentoo.org/wiki/Custom_Initramfs#Encrypted_keyfile]:
  
{{bc|1=
+
$ dd if=/dev/urandom of=bigsecret.img bs=100M count=1 iflag=fullblock
# dd if=/dev/urandom of=key.img bs=20M count=1
+
$ cryptsetup luksFormat bigsecret.img
# cryptsetup --align-payload=1 luksFormat key.img
+
 
}}
+
Make sure to not omit the {{ic|1=iflag=fullblock}} option, otherwise ''dd'' might return a partial read. See [[dd#Partial read]] for details.
  
Before running {{ic|cryptsetup}}, look at the [[Dm-crypt/Device_encryption#Encryption_options_for_LUKS_mode|Encryption options for LUKS mode]] and [[Disk_encryption#Ciphers_and_modes_of_operation|Ciphers and modes of operation]] first to select your additional desired settings.
+
Before running {{ic|cryptsetup}}, look at the [[dm-crypt/Device encryption#Encryption options for LUKS mode|encryption options for LUKS mode]] and [[Disk encryption#Ciphers and modes of operation|ciphers and modes of operation]] first to select your additional desired settings.
  
 
The instructions for opening the device and making the [[file system]] are the same as [[#Partition]].
 
The instructions for opening the device and making the [[file system]] are the same as [[#Partition]].
  
Having too small of a file will get you a {{ic|Requested offset is beyond real size of device /dev/loop0}} error, but as a rough reference creating a 4 MiB file will encrypt it successfully. [http://archive.is/VOh2p]
+
{{Note|Creating a file smaller than the LUKS2 header (16 MiB) will give a {{ic|Requested offset is beyond real size of device bigsecret.img}} error when trying to open the device.}}
 
 
If creating a larger file, {{ic|dd}} from {{ic|/dev/urandom}} will stop after 32 MiB, requiring the {{ic|1=iflag=fullblock}} option to complete the full write. [https://unix.stackexchange.com/questions/178949/why-does-dd-from-dev-urandom-stops-early]
 
  
 
Manual mounting and unmounting procedure is equivalent to [[#Manual mounting and unmounting]].
 
Manual mounting and unmounting procedure is equivalent to [[#Manual mounting and unmounting]].
Line 88: Line 91:
 
A loop device enables to map a blockdevice to a file with the standard util-linux tool {{ic|losetup}}. The file can then contain a filesystem, which can be used quite like any other filesystem. A lot of users know [[TrueCrypt]] as a tool to create encrypted containers. Just about the same functionality can be achieved with a loopback filesystem encrypted with LUKS and is shown in the following example.  
 
A loop device enables to map a blockdevice to a file with the standard util-linux tool {{ic|losetup}}. The file can then contain a filesystem, which can be used quite like any other filesystem. A lot of users know [[TrueCrypt]] as a tool to create encrypted containers. Just about the same functionality can be achieved with a loopback filesystem encrypted with LUKS and is shown in the following example.  
  
First, start by creating an encrypted container, using an appropriate [[random number generator]]:  
+
First, start by creating an encrypted container with [[dd]], using an appropriate [[random number generator]]:  
  
  # dd if=/dev/urandom of=/bigsecret bs=1M count=10
+
  $ dd if=/dev/urandom of=bigsecret.img bs=100M count=1 iflag=fullblock
  
This will create the file {{ic|bigsecret}} with a size of 10 megabytes.  
+
This will create the file {{ic|bigsecret.img}} with a size of 100 mebibytes.  
  
{{Note|To avoid having to [[Dm-crypt/Device_encryption#Loopback filesystem|resize]] the container later on, make sure to make it larger than the total size of the files to be encrypted, in order to at least also host the associated metadata needed by the internal file system. If you are going to use LUKS mode, its metadata header requires one to two megabytes alone.}}
+
{{Note|To avoid having to [[dm-crypt/Device encryption#Loopback filesystem|resize]] the container later on, make sure to make it larger than the total size of the files to be encrypted, in order to at least also host the associated metadata needed by the internal file system. If you are going to use LUKS mode, its metadata header alone requires up to 16 mebibytes.}}
  
 
Next create the device node {{ic|/dev/loop0}}, so that we can mount/use our container:
 
Next create the device node {{ic|/dev/loop0}}, so that we can mount/use our container:
  
  # losetup /dev/loop0 /bigsecret
+
  # losetup /dev/loop0 bigsecret.img
  
{{Note|If it gives you the error {{ic|/dev/loop0: No such file or directory}}, you need to first load the kernel module with {{ic|modprobe loop}}. These days (Kernel 3.2) loop devices are created on demand. Ask for a new loop device with {{ic|# losetup -f}}.}}
+
{{Note|If it gives you the error {{ic|/dev/loop0: No such file or directory}}, you need to first load the kernel module with {{ic|modprobe loop}} as root. These days (Kernel 3.2) loop devices are created on demand. Ask for a new loop device with {{ic|losetup -f}} as root.}}
  
 
From now on the procedure is the same as for [[#Partition]], except for the fact that the container is already randomised and will not need another secure erasure.
 
From now on the procedure is the same as for [[#Partition]], except for the fact that the container is already randomised and will not need another secure erasure.
Line 107: Line 110:
  
 
==== Manual mounting and unmounting ====
 
==== Manual mounting and unmounting ====
 +
 
To unmount the container:
 
To unmount the container:
  
Line 115: Line 119:
 
To mount the container again:
 
To mount the container again:
  
  # losetup /dev/loop0 /bigsecret
+
  # losetup /dev/loop0 bigsecret.img
 
  # cryptsetup open /dev/loop0 secret
 
  # cryptsetup open /dev/loop0 secret
 
  # mount -t ext4 /dev/mapper/secret /mnt/secret
 
  # mount -t ext4 /dev/mapper/secret /mnt/secret

Latest revision as of 23:21, 27 December 2019

The following are examples of encrypting a secondary, i.e. non-root, filesystem with dm-crypt.

Overview

Encrypting a secondary filesystem usually protects only sensitive data, while leaving the operating system and program files unencrypted. This is useful for encrypting an external medium, such as a USB drive, so that it can be moved to different computers securely. One might also choose to encrypt sets of data separately according to who has access to it.

Because dm-crypt is a block-level encryption layer, it only encrypts full devices, full partitions and loop devices. To encrypt individual files requires a filesystem-level encryption layer, such as eCryptfs or EncFS. See Disk encryption for general information about securing private data.

Partition

This example covers the encryption of the /home partition, but it can be applied to any other comparable non-root partition containing user data.

Tip: You can either have a single user's /home directory on a partition, or create a common partition for all user's /home directories.

First make sure the partition is empty (has no file system attached to it). Delete the partition and create an empty one if it has a file system. Then prepare the partition by securely erasing it, see Dm-crypt/Drive preparation#Secure erasure of the hard disk drive.

Create the partition which will contain the encrypted container.

Then setup the LUKS header with:

# cryptsetup options luksFormat device

Replace device with the previously created partition. See Dm-crypt/Device encryption#Encryption options for LUKS mode for details like the available options.

To gain access to the encrypted partition, unlock it with the device mapper, using:

# cryptsetup open device name

After unlocking the partition, it will be available at /dev/mapper/name. Now create a file system of your choice with:

# mkfs.fstype /dev/mapper/name

Mount the file system to /home, or if it should be accessible to only one user to /home/username, see #Manual mounting and unmounting.

Tip: Unmount and mount once to verify that the mapping is working as intended.

Manual mounting and unmounting

To mount the partition:

# cryptsetup open device name
# mount -t fstype /dev/mapper/name /mnt/home

To unmount it:

# umount /mnt/home
# cryptsetup close name
Tip: GVFS can also mount encrypted partitions. One can use a file manager with gvfs support (e.g. Thunar) to mount the partition, and a password dialog will pop-up. For other desktops, zulucryptAUR also provides a GUI.

Automated unlocking and mounting

There are three different solutions for automating the process of unlocking the partition and mounting its filesystem.

At boot time

Using the /etc/crypttab configuration file, unlocking happens at boot time by systemd's automatic parsing. This is the recommended solution if you want to use one common partition for all user's home partitions or automatically mount another encrypted block device.

See Dm-crypt/System configuration#crypttab for references and Dm-crypt/System configuration#Mounting at boot time for an example set up.

On user login

Using pam_exec it is possible to unlock (cryptsetup open) the partition on user login: this is the recommended solution if you want to have a single user's home directory on a partition. See dm-crypt/Mounting at login.

Unlocking on user login is also possible with pam_mount.

Loop device

There are two methods for using a loop device as an encrypted container, one using losetup directly and one without.

Without losetup

Using losetup directly can be avoided completely by doing the following [1]:

$ dd if=/dev/urandom of=bigsecret.img bs=100M count=1 iflag=fullblock
$ cryptsetup luksFormat bigsecret.img

Make sure to not omit the iflag=fullblock option, otherwise dd might return a partial read. See dd#Partial read for details.

Before running cryptsetup, look at the encryption options for LUKS mode and ciphers and modes of operation first to select your additional desired settings.

The instructions for opening the device and making the file system are the same as #Partition.

Note: Creating a file smaller than the LUKS2 header (16 MiB) will give a Requested offset is beyond real size of device bigsecret.img error when trying to open the device.

Manual mounting and unmounting procedure is equivalent to #Manual mounting and unmounting.

Using losetup

A loop device enables to map a blockdevice to a file with the standard util-linux tool losetup. The file can then contain a filesystem, which can be used quite like any other filesystem. A lot of users know TrueCrypt as a tool to create encrypted containers. Just about the same functionality can be achieved with a loopback filesystem encrypted with LUKS and is shown in the following example.

First, start by creating an encrypted container with dd, using an appropriate random number generator:

$ dd if=/dev/urandom of=bigsecret.img bs=100M count=1 iflag=fullblock

This will create the file bigsecret.img with a size of 100 mebibytes.

Note: To avoid having to resize the container later on, make sure to make it larger than the total size of the files to be encrypted, in order to at least also host the associated metadata needed by the internal file system. If you are going to use LUKS mode, its metadata header alone requires up to 16 mebibytes.

Next create the device node /dev/loop0, so that we can mount/use our container:

# losetup /dev/loop0 bigsecret.img
Note: If it gives you the error /dev/loop0: No such file or directory, you need to first load the kernel module with modprobe loop as root. These days (Kernel 3.2) loop devices are created on demand. Ask for a new loop device with losetup -f as root.

From now on the procedure is the same as for #Partition, except for the fact that the container is already randomised and will not need another secure erasure.

Tip: Containers with dm-crypt can be very flexible. Have a look at the features and documentation of Tomb. It provides a dm-crypt script wrapper for fast and flexible handling.

Manual mounting and unmounting

To unmount the container:

# umount /mnt/secret
# cryptsetup close secret
# losetup -d /dev/loop0

To mount the container again:

# losetup /dev/loop0 bigsecret.img
# cryptsetup open /dev/loop0 secret
# mount -t ext4 /dev/mapper/secret /mnt/secret