reencrypt_linux_post_install
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| reencrypt_linux_post_install [2025/05/02 14:34] – [Associate the boot partition] usera | reencrypt_linux_post_install [2026/06/19 09:25] (current) – usera | ||
|---|---|---|---|
| Line 13: | Line 13: | ||
| You should make (encrypted !) backups of your system all the time. However, you should **ABSOLUTELY** do it before any operation on your disks or partitions such as the one presented here: there is a significant risk to destroy ALL your data. | You should make (encrypted !) backups of your system all the time. However, you should **ABSOLUTELY** do it before any operation on your disks or partitions such as the one presented here: there is a significant risk to destroy ALL your data. | ||
| - | I suggest a backup using two different open source softwares. The first one is [[https:// | + | Please follow the procedures listed |
| - | For this, you will need: | ||
| - | - An external hard drive to store the backup. I suggest using an external SSD with a capacity of roughly 3 times your whole drive capacity. For example, if you have a 500GB drive, you will probably be fine with a 2TB drive, but it should be used only for this purpose. | + | ===== STEP 2: Setup your partitioning ===== |
| - | - An external USB stick to install CloneZilla. It should be at least 1GB and be used only for CloneZilla | + | |
| - | ==== STEP 1.1: prepare | + | There are several ways a Linux distribution can be installed without encryption. Check the output of: |
| - | + | ||
| - | First, [[https:// | + | |
| - | + | ||
| - | Then, verify the download, using the following commands: | + | |
| < | < | ||
| - | wget https:// | + | lsblk |
| - | wget https:// | + | |
| - | gpg --keyserver hkp:// | + | |
| - | gpg --verify CHECKSUMS.TXT.gpg CHECKSUMS.TXT | + | |
| </ | </ | ||
| - | The output | + | Ideally, you should |
| - | < | + | |
| - | gpg: Good signature from "DRBL Project (Diskless Remote Boot in Linux) < | + | |
| - | </ | + | |
| - | You then have verified that the file CHECKSUM.TXT has been encrypted by the owner of the private key of the clonezilla project. | + | - One of them should be the " |
| - | You can then compare | + | - One of them will contain your root installation (the mount point should be /, the size should be most of the disk size and the filesystem should be ext4). |
| + | - Optionally, you can have a separate ext4 partition for /home, that would then be much bigger than the / (something like 30GB-50GB is typical for /, the rest of the disk for /home), | ||
| + | - Maybe a partition for swap, | ||
| + | - Maybe an ext4 partition for /boot, which is typically around the same size as the EFI partition | ||
| - | < | + | If you have a /home or a /swap partition, we are going to ignore them for now. The setup you will need to make this work is: |
| - | sha256sum clonezilla-live-3.2.1-9-amd64.iso | + | |
| - | </ | + | |
| - | Replace | + | - The ESP (or EFI) partition |
| + | - One partition for the / | ||
| + | - One partition for the /boot | ||
| - | < | + | You most likely have the two first one. If you already have the last one, you can go immediately to the following step. |
| - | cat CHECKSUMS.TXT | + | |
| - | </ | + | |
| - | If so, you are sure to have the right file. You can then proceed to flash you USB key. I recommend using [[balena-etcher|Balena Etcher]], which is Open Source, user friendly and cross platform. | + | ==== In case you do not have a separate /boot partition ==== |
| - | ==== STEP 1.2: prepare | + | === Create |
| - | You are going to make two partitions on your external SSD: | ||
| - | - __One unencrypted partition__, | ||
| - | - __One encrypted partition__, | ||
| - | - One for the backups | + | If you do not have the last one, you need to create one ext4 partition |
| - | - One for snapshots of your root filesystem (so your programs). This should be done later (but it will not be covered in this tutorial), once you have a stable encrypted system, with a program called Timeshift. | + | |
| - | For this you should | + | Once there, |
| - | < | + | |
| - | sudo apt install gparted | + | |
| - | </ | + | |
| - | + | ||
| - | Launch it: | + | |
| < | < | ||
| Line 73: | Line 53: | ||
| </ | </ | ||
| - | This will prompt the following screen, where I will take the example of a small 1GB USB key formatting | + | Select your disk, then locate |
| - | First it will prompt | + | If you see something in the "Mount point" column, right click and click on " |
| - | - One small (500MB for example) unencrypted | + | |
| - | - One small (500MB for example) unencrypted partition in ext4 for the boot partition (/boot) | + | |
| - | - One large (in my case almost 500GB) encrypted partition in etx4 for my whole filesystem | + | |
| - | {{ ::gparted_1.png?600 |}} | + | {{::gparted_12.png?600 |}} |
| - | Select the disk you want to partition | + | You can now create a new ext4 partition |
| - | {{ ::gparted_2.png?600 |}} | + | {{ ::gparted_13.png?600 |}} |
| - | {{ ::gparted_3.png?600 |}} | + | {{ ::gparted_14.png?600 |}} |
| - | To modify the partitions you first need to unmount them | + | |
| - | {{ ::gparted_4.png?600 |}} | + | {{ ::gparted_15.png?600 |}} |
| - | Then you can create a new partition | + | === Associate the boot partition |
| - | {{ :: | ||
| - | {{ ::gparted_6.png?600 |}} | + | Then, you need to reinstall your boot partition. It is possible to do it manually, but I recommend a nice tool called boot-repair. To use it, open a command prompt and type: |
| - | You can make a new partition now by right clicking on the empty space | + | < |
| + | sudo add-apt-repository ppa: | ||
| + | sudo apt-get update | ||
| + | sudo apt-get install boot-repair | ||
| + | boot-repair | ||
| + | </ | ||
| - | {{ ::gparted_7.png?600 |}} | + | you will be prompted with a menu saying "apply the recommanded repairs" |
| - | Choose | + | |
| - | {{ :: | + | Now you can restart your computer, remove |
| - | Repeat | + | < |
| - | {{ :: | + | lsblk |
| + | </ | ||
| - | {{ :: | + | shows among other lines one line for the ESP partition |
| - | click on Apply (the green check mark). **You are warned: all data are going to be lost on this disk**. Click on Apply. | + | |
| - | {{ :: | + | === Do the actual reencryption === |
| - | you can then close gparted. Open a terminal and proceed | + | Insert your live USB key, restart your computer and boot on your live system again. Once this is done, open a command line. We will need to shrink |
| < | < | ||
| - | lsblk | + | sudo dumpe2fs -h / |
| </ | </ | ||
| - | In my case I get the following output: | + | which will give you something like this: |
| < | < | ||
| - | NAME | + | dumpe2fs |
| - | sda 8:0 0 465.8G | + | Block count: |
| - | ├─sda1 | + | Block size: 4096 |
| - | ├─sda2 | + | Blocks per group: 32768 |
| - | └─sda3 | + | |
| - | └─disk1 | + | |
| - | ├─usera--computer--vg-root | + | |
| - | | + | |
| - | | + | |
| - | sdb 8:16 | + | |
| - | ├─sdb1 | + | |
| - | └─sdb2 | + | |
| </ | </ | ||
| - | you can see that this command gives more details than gparted, especially since my encrypted disk is decrypted, and you can see the next level, three "sub partitions" | + | note the "Block count" |
| - | This parenthesis closed, I can identify the partition I want to encrypt: it is called sdb2 (in linux, disks are usually called sda, sdb, sdc..., and partitions in a disk called sda1, sda2, etc.). | + | |
| - | + | ||
| - | To encrypt the second partition, let us install | + | |
| < | < | ||
| - | sudo apt-get -y install cryptsetup | + | python3 |
| </ | </ | ||
| - | Then you can proceed with the encryption (careful: if the partition you want to encrypt has another name, change it accordingly): | + | In my case, I get: |
| < | < | ||
| - | sudo umount | + | python3 -c " |
| - | sudo cryptsetup luksFormat /dev/sdb2 | + | 77875200 |
| </ | </ | ||
| + | Note this number. | ||
| + | Now enter: | ||
| - | You need to enter ' | ||
| < | < | ||
| - | WARNING: Device | + | sudo e2fsck -f /dev/[ROOT_PARTITION] |
| - | + | ||
| - | WARNING! | + | |
| - | ======== | + | |
| - | This will overwrite data on /dev/sdb2 irrevocably. | + | |
| - | + | ||
| - | Are you sure? (Type ' | + | |
| - | Enter passphrase for /dev/sdb2: | + | |
| - | Verify passphrase: | + | |
| </ | </ | ||
| - | After a couple time (a bit more depending on the size of the partition), your partition is now encrypted. | + | in my case, I get: |
| - | + | ||
| - | you can now decrypt it with the following command: | + | |
| < | < | ||
| - | sudo cryptsetup luksOpen | + | mint@mint: |
| + | e2fsck 1.46.5 (30-Dec-2021) | ||
| + | Pass 1: Checking inodes, blocks, and sizes | ||
| + | Pass 2: Checking directory structure | ||
| + | Pass 3: Checking directory connectivity | ||
| + | Pass 4: Checking reference counts | ||
| + | Pass 5: Checking group summary information | ||
| + | /dev/sda2: 311972/ | ||
| </ | </ | ||
| - | you will be prompted with your passphrase. Now that your disk is decrypted, | + | |
| + | then you can do the actual resize of the filesystem, using the number you previously calculated, in my case 77875200. | ||
| < | < | ||
| - | lsblk | + | sudo resize2fs -p / |
| </ | </ | ||
| - | gives a different result: | + | |
| + | I got: | ||
| < | < | ||
| - | NAME | + | resize2fs |
| - | sda 8:0 0 465.8G | + | Resizing the filesystem on /dev/sda2 to 77875200 (4k) blocks. |
| - | ├─sda1 | + | The filesystem on /dev/sda2 is now 77875200 (4k) blocks long. |
| - | ├─sda2 | + | |
| - | └─sda3 | + | |
| - | └─disk1 | + | |
| - | ├─usera--computer--vg-root | + | |
| - | | + | |
| - | └─usera--computer--vg-home | + | |
| - | sdb 8:16 | + | |
| - | ├─sdb1 | + | |
| - | └─sdb2 | + | |
| - | └─cryptodisk | + | |
| - | sr0 | + | |
| </ | </ | ||
| - | We will now prepare the two "sub partitions" | + | you can now do the actual reencription: |
| < | < | ||
| - | sudo apt install lvm2 | + | sudo cryptsetup reencrypt --encrypt --reduce-device-size 32M / |
| </ | </ | ||
| + | which will take a very long time, depending on the size of your disk. In my case, I got: | ||
| - | you will need to execute the following commands: | ||
| - | |||
| - | You first create what we call a " | ||
| < | < | ||
| - | sudo pvcreate | + | mint@mint: |
| + | |||
| + | WARNING! | ||
| + | ======== | ||
| + | This will overwrite data on LUKS2-temp-5c24539d-acc7-4216-bc61-0939b2d4f51f.new irrevocably. | ||
| + | |||
| + | Are you sure? (Type ' | ||
| + | Enter passphrase for LUKS2-temp-5c24539d-acc7-4216-bc61-0939b2d4f51f.new: | ||
| + | Verify passphrase: | ||
| + | Finished, time 358:10.876, 304216 MiB written, speed 14.2 MiB/s\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00 | ||
| </ | </ | ||
| - | then you create a " | + | which took 358 minutes, which is 6 hours. |
| + | |||
| + | Then you can decrypt | ||
| < | < | ||
| - | sudo vgcreate vgbackup | + | sudo cryptsetup luksOpen |
| </ | </ | ||
| - | Then you create two " | + | you will have to enter your passphrase. Then resize |
| < | < | ||
| - | sudo lvcreate -L 250M -n root vgbackup | + | sudo resize2fs / |
| - | sudo lvcreate -l 100%FREE -n home vgbackup | + | |
| </ | </ | ||
| - | then you can check again the output of | + | which gave in my case: |
| < | < | ||
| - | lsblk | + | resize2fs 1.46.5 (30-Dec-2021) |
| + | Resizing the filesystem on / | ||
| + | The filesystem on / | ||
| </ | </ | ||
| - | You can see that we have the right partitioning: | ||
| + | Now, you need to note down the content of the file / | ||
| < | < | ||
| - | NAME | + | cat /etc/resolv.conf |
| - | sda 8:0 0 465.8G | + | |
| - | ├─sda1 | + | |
| - | ├─sda2 | + | |
| - | └─sda3 | + | |
| - | └─disk1 | + | |
| - | ├─usera--computer--vg-root | + | |
| - | ├─usera--computer--vg-swap_1 254:2 0 | + | |
| - | └─usera--computer--vg-home | + | |
| - | sdb 8:16 | + | |
| - | ├─sdb1 | + | |
| - | └─sdb2 | + | |
| - | └─cryptodisk | + | |
| - | ├─vgbackup-root | + | |
| - | └─vgbackup-home | + | |
| - | sr0 | + | |
| </ | </ | ||
| - | Then you need to create | + | and you can now do a special operation to "enter" in your system. It is called a chroot operation, ,it is very convenient to recover a system that does not boot for example (boot with a live USB system, do a chroot, change some things to fix your system, and reboot). |
| + | Enter the following commands, where [BOOT_PARTITION] is the boot partition you created, in my case sda3, and [EFI_PARTITION] is the EFI partition, in my case sda1: | ||
| < | < | ||
| - | sudo mkfs.ext4 | + | sudo mount / |
| - | sudo mkfs.ext4 | + | sudo mount / |
| + | sudo mount / | ||
| + | sudo mount --bind /dev /mnt/dev | ||
| + | sudo mount --bind | ||
| + | sudo mount --bind /sys /mnt/sys | ||
| + | sudo mount --bind /proc /mnt/proc | ||
| + | sudo chroot /mnt | ||
| </ | </ | ||
| - | this formatted those partitions with the filesystem called " | ||
| - | In what we did, there are several levels of partitions | + | the prompt will change a bit: you are now an administrator inside your own system! |
| - | {{ :: | + | Let us first check that we are connected to internet. To do that, you need to try to ping a website: |
| - | You can then close the LVM: | ||
| < | < | ||
| - | sudo vgchange -an vgbackup | + | ping google.com |
| </ | </ | ||
| - | and the luks container: | + | if you see something like this: |
| < | < | ||
| - | sudo cryptsetup luksClose cryptodisk | + | (base) usera@debian: |
| + | PING google.com (142.250.74.78) 56(84) bytes of data. | ||
| + | 64 bytes from arn09s23-in-f14.1e100.net (142.250.74.78): | ||
| + | 64 bytes from arn09s23-in-f14.1e100.net (142.250.74.78): | ||
| + | 64 bytes from arn09s23-in-f14.1e100.net (142.250.74.78): | ||
| </ | </ | ||
| + | then you are good to go, you can do CTRL+C to stop it. Otherwise, try copying the content of the / | ||
| - | ==== STEP 1.3: do the actual backup ==== | ||
| - | |||
| - | you can then follow the [[https:// | ||
| - | |||
| - | Once this is done, you can do another backup of your /home, which contains all your regular files. For this you can use the software backintime. You can install it with: | ||
| < | < | ||
| - | sudo apt install backintime-qt | + | nano / |
| </ | </ | ||
| + | then paste the content, and enter CTRL+X, then Y do save the changes. Try again to ping: | ||
| - | Before launching it, we need to decrypt the encrypted disk, and mount the logical volume we intend to use: | ||
| < | < | ||
| - | sudo cryptsetup luksOpen /dev/sdb2 cryptsetup | + | ping google.com |
| - | sudo vgchange -ay | + | |
| - | sudo mount / | + | |
| </ | </ | ||
| - | now everything written on /mnt on your computer will be written on your disk. | + | It should work now. |
| - | You can then launch backintime with: | + | Now, install some required packages: |
| < | < | ||
| - | sudo backintime-qt | + | sudo apt install cryptsetup cryptsetup-initramfs |
| </ | </ | ||
| - | you can then configure it to save things in /mnt: | + | and let us do some modifications. |
| + | Enter the following command: | ||
| - | {{ :: | + | < |
| + | blkid | ||
| + | </ | ||
| + | in my case I get: | ||
| - | {{ ::backintime_2.png? | + | < |
| + | /dev/sdb2: SEC_TYPE=" | ||
| + | /dev/sdb3: LABEL=" | ||
| + | /dev/sdb1: BLOCK_SIZE=" | ||
| + | /dev/loop0: TYPE=" | ||
| + | / | ||
| + | /dev/sda2: UUID=" | ||
| + | /dev/sda3: UUID=" | ||
| + | /dev/sda1: UUID=" | ||
| + | </ | ||
| + | you should note the UUID of the encrypted disk, in my case sda2, whose UUID is 5c24539d-acc7-4216-bc61-0939b2d4f51f | ||
| + | edit the file / | ||
| - | {{ :: | + | < |
| - | + | nano / | |
| - | And then click on "Take snapshot" | + | </ |
| - | + | ||
| - | {{ :: | + | |
| - | you can then unmount | + | and add the following line, replacing [UUID] with the value you noted: |
| < | < | ||
| - | sudo umount /mnt | + | cryptodisk |
| - | sudo vgchange -an vgbackup | + | |
| - | sudo cryptsetup luksClose | + | |
| </ | </ | ||
| - | ===== STEP 2: Setup your partitioning ===== | + | you can exit the editor with CTRL+X, then type Y for saving it. |
| - | + | then note the UUID of the decrypted partition, here / | |
| - | There are several ways a Linux distribution | + | |
| < | < | ||
| - | lsblk | + | nano /etc/fstab |
| </ | </ | ||
| - | Ideally, | + | you should |
| - | + | ||
| - | - One of them should be the " | + | |
| - | - One of them will contain your root installation (the mount point should be /, the size should be most of the disk size and the filesystem should be ext4). | + | |
| - | - Optionally, you can have a separate ext4 partition for / | + | |
| - | - Maybe a partition for swap, | + | |
| - | - Maybe an ext4 partition for /boot, which is typically around the same size as the EFI partition | + | |
| - | + | ||
| - | If you have a /home or a /swap partition, we are going to ignore them for now. The setup you will need to make this work is: | + | |
| - | + | ||
| - | - The ESP (or EFI) partition | + | |
| - | - One partition for the / | + | |
| - | - One partition for the /boot | + | |
| - | + | ||
| - | You most likely have the two first one. If you already have the last one, you can go immediately to the following | + | |
| - | + | ||
| - | ==== In case you do not have a separate /boot partition ==== | + | |
| - | + | ||
| - | === Create the partition === | + | |
| - | + | ||
| - | + | ||
| - | If you do not have the last one, you need to create one ext4 partition of roughly 500MB. You will need to shrink another partition for this, and you cannot shrink a partition that you are using, You will therefore need to **[[boot_live_usb|boot from a live USB]]** | + | |
| - | + | ||
| - | Once there, you can use the gparted tool again: | + | |
| < | < | ||
| - | sudo gparted | + | / |
| </ | </ | ||
| - | Select your disk, then locate the partition | + | you need finally |
| - | + | ||
| - | If you see something in the "Mount point" column, right click and click on " | + | |
| - | + | ||
| - | {{:: | + | |
| - | + | ||
| - | You can now create a new ext4 partition of size 500MB roughly, and then click on Add, then "Apply all operations" | + | |
| - | + | ||
| - | {{ :: | + | |
| - | + | ||
| - | {{ :: | + | |
| - | + | ||
| - | {{ :: | + | |
| - | + | ||
| - | === Associate the boot partition === | + | |
| - | + | ||
| - | + | ||
| - | Then, you need to reinstall your boot partition. It is possible to do it manually, but I recommend a nice tool called boot-repair. To use it, open a command prompt and type: | + | |
| < | < | ||
| - | sudo add-apt-repository ppa: | + | nano /etc/ |
| - | sudo apt-get update | + | |
| - | sudo apt-get install boot-repair | + | |
| - | boot-repair | + | |
| </ | </ | ||
| - | you will be prompted with a menu saying " | + | you need to locate |
| + | where you change < | ||
| + | You can then enter: | ||
| - | Now you can restart your computer, remove the USB key and let it boot on the normal OS, check that the system boots, and that the result of the command: | ||
| < | < | ||
| - | lsblk | + | sudo update-initramfs -u |
| </ | </ | ||
| - | shows among other lines one line for the ESP partition (Mount point /boot/efi), one for the boot parititon (Mount point / | + | and then: |
| - | + | ||
| - | === Do the actual reencryption === | + | |
| - | + | ||
| - | Insert your live USB key, restart your computer and boot on your live system again. Once this is done, open a command line. We will need to shrink the partition with the root filesystem by 32M in order to encrypt it. To do this, a small computation is necessary. Here are the command you will use, assuming that the partition you want to encrypt is /dev/sda2 | + | |
| < | < | ||
| - | sudo dumpe2fs | + | update-grub |
| </ | </ | ||
| - | which will give you something like this: | + | and finally the following commands: |
| < | < | ||
| - | dumpe2fs 1.47.0 (5-Feb-2023) | + | exit |
| - | Block count: | + | sudo shutdown |
| - | Block size: | + | |
| - | Blocks per group: | + | |
| </ | </ | ||
| - | note the "Block count" | + | you will then remove your USB key and boot in your encrypted system hopefully. You should see a prompt asking to unlock your disk, and then your familiar OS will boot, with all the configuration you previously had. Congratulations ! |
| - | < | + | |
| - | python3 -c " | + | |
| - | </ | + | |
| - | Note this number. | + | |
reencrypt_linux_post_install.1746196497.txt.gz · Last modified: by usera
