Swap file creation
Note: For Btrfs, follow the procedure described in Btrfs#Swap file instead of the steps below.
Use mkswap(8) to create a swap file the size of your choosing (see Partitioning#Swap for advice). For example, creating a 4 GiB swap file:
mkswap -U clear --size 4G --file /swapfile
Activate the swap file:
swapon /swapfile
Finally, edit the fstab configuration to add an entry for the swap file:
/etc/fstab
/swapfile none swap defaults 0 0
For additional information, see fstab#Usage.
Note: The swap file must be specified by its location on the file system, not by its UUID or LABEL.
dm-crypt/Swap encryption
code
Page
Discussion
Read
View source
View history
Tools
< Dm-crypt
Depending on requirements, different methods may be used to encrypt the swap partition which are described in the following. A setup where the swap encryption is re-initialised on reboot (with a new encryption) provides higher data protection, because it avoids sensitive file fragments which may have been swapped out a long time ago without being overwritten. However, re-encrypting swap also forbids using a suspend-to-disk feature generally.
Without suspend-to-disk support
In systems where suspend-to-disk (hibernation) is not a desired feature, /etc/crypttab can be set up to decrypt the swap partition with a random password with plain dm-crypt at boot-time. The random password is discarded on shutdown, leaving behind only encrypted, inaccessible data in the swap device.
To enable this feature, simply uncomment the line beginning with swap in /etc/crypttab. Change the device parameter to the name of your swap device. For example, it will look something like this:
This will map /dev/sdX# to /dev/mapper/swap as a swap partition that can be added in /etc/fstab like a normal swap. If you had a non-encrypted swap partition before, do not forget to disable it - or re-use its fstab entry by changing the device to /dev/mapper/swap. The default options should be sufficient for most usage. For other options and an explanation of each column, see crypttab(5) as well as point cryptsetup FAQ 2.3.
Warning: All contents of the named device will be permanently deleted. It is dangerous to use the kernel's simple naming for a swap device, since their naming order (e.g. /dev/sda, /dev/sdb) changes upon each boot. Options are:
code
Assistant
User
My swap doesn't encrypted because its stored inside encrypted root partition
Assistant
If your swap file is located within an encrypted root partition, you won't need to deal with additional encryption for the swap itself; rather, you only need to ensure that your system knows how to resume from that swap file when hibernating. Here's a concise guide on setting that up:
1. Confirm Your Swap File
Ensure that your swap file (e.g., ) is created and active:
Use by-id and by-path paths. However, these are both are susceptible to hardware changes. See Persistent block device naming#by-id and by-path.
Use PARTLABEL.
Use an LVM logical volume's name.
Use the method described in #UUID and LABEL. Labels and UUIDS cannot be used directly because of the recreation and re-encryption of the swap device on every boot with mkswap, see cryptsetup FAQ.
Note: Swap partition setup can sometimes fail, see systemd issue 10179.
To use a by-id persistent device naming instead of kernel simple naming, first identify the swap device:
After a reboot to activate the encrypted swap, you will note that running swapon -s shows an arbitrary device mapper entry (e.g. /dev/dm-1) for it, while the lsblk command shows crypt in the FSTYPE column. Due to fresh encryption each boot, the UUID for /dev/mapper/swap will change every time.
Note: If the partition chosen for swap was previously a LUKS partition, crypttab will not overwrite the partition to create a swap partition. This is a safety measure to prevent data loss from accidental mis-identification of the swap partition in crypttab. In order to use such a partition the LUKS header must be overwritten once.
UUID and LABEL
It is dangerous to use crypttab swap with simple kernel device names like /dev/sdX# or even /dev/disk/by-id/ata-SERIAL-partX. A small change in your device names or partitioning layout and /etc/crypttab will see your valuable data formatted on the next boot. Same if you use PARTUUID and then decide to use that partition for something else without removing the crypttab entry first.
It is more reliable to identify the correct partition by giving it a genuine UUID or LABEL. By default that does not work because dm-crypt and mkswap would simply overwrite any content on that partition which would remove the UUID and LABEL too; however, it is possible to specify a swap offset. This allows you to create a very small, empty, bogus filesystem with no other purpose than providing a persistent UUID or LABEL for the swap encryption.
Create a filesystem with label of your choice:
mkfs.ext2 -L cryptswap /dev/sdX# 1M
The unusual parameter after the device name limits the filesystem size to 1 MiB, leaving room for encrypted swap behind it.
With this, /dev/sdX# now can easily be identified either by UUID or LABEL, regardless of how its device name or even partition number might change in the future. All that is left are the /etc/crypttab and /etc/fstab entries. For example, using different encryption options:
Note the offset: it is 2048 sectors of 512 bytes (it is not affected by the dm-crypt sector size), thus 1 MiB. This way the encrypted swap will not affect the filesystem LABEL/UUID, and data alignment works out as well.
/etc/fstab
/dev/mapper/swap none swap defaults 0 0
Using this setup, the cryptswap will only try to use the partition with the corresponding LABEL, regardless of what its device name may be. Should you decide to use the partition for something else, by formatting it the cryptswap LABEL would also be gone, so /etc/crypttab will not overwrite it on your next boot.
Disabling hibernation in desktop environments
Desktop environments may not automatically detect that a swap partition is randomly encrypted and cannot be used for suspend-to-disk.
Xfce can be configured to hide its Hibernate button by running this command:
The following three methods are alternatives for setting up an encrypted swap for suspend-to-disk. If you apply any of them, be aware that critical data swapped out by the system may potentially stay in the swap over a long period (i.e. until it is overwritten). To reduce this risk consider setting up a system job which re-encrypts swap, e.g. each time the system is going into a regular shut-down, along with the method of your choice.
LVM on LUKS
If the swap volume is in a volume group that gets activated in initramfs, simply follow the instructions in Power management/Suspend and hibernate#Hibernation.
Using a swap partition
Note: To use a partition which is currently used by the system, you have to disable it first with # swapoff /dev/device, and make sure to remove any line in /etc/crypttab pointing to this device.
Basic swap partition setup
Use LuksFormat to create the encrypted container for the swap partition:
cryptsetup luksFormat --label swap /dev/device
Open the container to /dev/mapper/swap:
cryptsetup open /dev/disk/by-label/swap swap
Create a swap filesystem inside the mapped partition:
mkswap /dev/mapper/swap
If not using Systemd#GPT partition automounting, add the mapped partition to /etc/fstab by adding the following line:
/dev/mapper/swap none swap defaults 0 0
Set up your system to resume from /dev/mapper/swap. For example, if you use GRUB with kernel hibernation support, add the kernel parameter resume=/dev/mapper/swap to GRUB by appending it to the GRUB_CMDLINE_LINUX_DEFAULT variable in /etc/default/grub. A kernel line with encrypted root and swap partitions can look like this:
I created swap file /swp on encrypted root drive. How to setup resume on it? Which kernel params I should pass in bootloader? for opening the swap file by typing password when resuming linux from hibernation
To set up a resume from a swap file on an encrypted root drive and ensure that your system can properly resume after hibernation, you need to follow these steps:
1. Create the Encrypted Swap File
You mentioned you've already created the swap file. Just ensure it has been set up properly using:
bash
sudo mkswap /swp
sudo swapon /swp
2. Add the Swap File to /etc/fstab
Make sure your swap file is listed in /etc/fstab so it is activated on boot. The entry should look like this:
code
/swp none swap sw 0 0
3. Set Up the Resume Configuration
You need to configure your system to use the swap file for hibernation. This involves the following:
Edit GRUB Configuration: You need to make sure GRUB knows that it should use the swap file for resuming. Open the GRUB configuration file:
bash
sudo nano /etc/default/grub
Find the line that starts with GRUB_CMDLINE_LINUX_DEFAULT and append the following parameters to it:
Regenerate the GRUB Configuration: After modifying the GRUB configuration file, you need to regenerate the GRUB configuration:
bash
sudo grub-mkconfig -o /boot/grub/grub.cfg
4. Update the Initramfs
If you are using an encrypted root file system, you will also need to ensure that the initial ramdisk image (initramfs) knows about the swap file.
Add the resume parameter in the initramfs configuration. If you are using systemd, you can create or edit:
bash
sudo nano /etc/initramfs-tools/conf.d/resume
In this file, add the following line:
code
RESUME=/swp
Make sure to update the initramfs image:
bash
sudo update-initramfs -u
5. Enable Password Prompt for Decryption on Resume
If your swap file needs to be decrypted to resume, you will generally want your system to prompt for a password. Make sure you have set up your cryptsetup and crypttab correctly if you are using LUKS.
6. Testing
Finally, it's important to test the setup to ensure it works as expected:
Hibernate the system.
Upon waking up, you should be prompted for your password if you set up encrypted swap properly.
Confirm that the system resumes correctly.
Conclusion
Your modifications should now configure the Linux kernel to use the encrypted swap file for system hibernation. Make sure to carefully follow each step to avoid any configuration mistakes. Always keep backups of important configuration files before modifications.
/swp
bash
sudo mkswap /swp
sudo swapon /swp
2. Add Entry in /etc/fstab
Make sure your swap file is listed in your /etc/fstab so that it is activated on boot:
plaintext
/swp none swap sw 0 0
Edit the file using:
bash
sudo nano /etc/fstab
3. Configure GRUB for Resume
You need to configure GRUB to use your swap file for resuming from hibernation. Open the GRUB configuration file:
bash
sudo nano /etc/default/grub
Find the line starting with GRUB_CMDLINE_LINUX_DEFAULT and append the resume parameter:
After modifying the GRUB configuration, regenerate the GRUB menu:
bash
sudo grub-mkconfig -o /boot/grub/grub.cfg
5. Configure Initramfs
Since you're using an encrypted root filesystem, you should inform the initramfs about the swap file for hibernation. Create or edit a resume configuration file:
bash
sudo nano /etc/initramfs-tools/conf.d/resume
Add the following line to specify the resume swap file:
plaintext
RESUME=/swp
After that, update the initramfs:
bash
sudo update-initramfs -u
6. Testing the Setup
Once you've configured everything, it's important to test the setup:
Hibernate your system:
bash
sudo systemctl hibernate
Upon waking your system, it should resume from the swap file if everything is configured correctly.
Summary
Since your swap file is stored within an encrypted partition, you only need to set the resume parameter to point to your swap file in both GRUB and initramfs configurations. By following these steps, you should be able to successfully set up resuming from hibernation using your swap file.