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/04/30 18:44] – [STEP 2: Setup your partitioning] 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, | + | 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.). | + | < |
| + | python3 | ||
| + | </ | ||
| - | To encrypt the second partition, let us install the cryptsetup tool, in case it is not already there on your computer. Open a terminal and enter: | + | In my case, I get: |
| < | < | ||
| - | sudo apt-get -y install cryptsetup | + | python3 |
| + | 77875200 | ||
| </ | </ | ||
| + | Note this number. | ||
| + | Now enter: | ||
| - | Then you can proceed with the encryption (careful: if the partition you want to encrypt has another name, change it accordingly): | + | < |
| + | sudo e2fsck -f / | ||
| + | </ | ||
| + | in my case, I get: | ||
| < | < | ||
| - | sudo umount | + | mint@mint: |
| - | sudo cryptsetup luksFormat | + | 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 need to enter ' | + | then you can do the actual resize of the filesystem, using the number you previously calculated, in my case 77875200. |
| < | < | ||
| - | WARNING: Device | + | sudo resize2fs -p / |
| + | </ | ||
| + | |||
| + | I got: | ||
| + | < | ||
| + | resize2fs 1.46.5 (30-Dec-2021) | ||
| + | Resizing the filesystem on /dev/sda2 to 77875200 (4k) blocks. | ||
| + | The filesystem on /dev/sda2 is now 77875200 (4k) blocks long. | ||
| + | </ | ||
| + | |||
| + | you can now do the actual reencription: | ||
| + | < | ||
| + | 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: | ||
| + | |||
| + | < | ||
| + | mint@mint: | ||
| WARNING! | WARNING! | ||
| ======== | ======== | ||
| - | This will overwrite data on / | + | This will overwrite data on LUKS2-temp-5c24539d-acc7-4216-bc61-0939b2d4f51f.new |
| Are you sure? (Type ' | Are you sure? (Type ' | ||
| - | Enter passphrase for /dev/sdb2: | + | Enter passphrase for LUKS2-temp-5c24539d-acc7-4216-bc61-0939b2d4f51f.new: |
| - | Verify passphrase: | + | Verify passphrase: |
| + | Finished, time 358:10.876, 304216 MiB written, speed 14.2 MiB/ | ||
| </ | </ | ||
| - | After a couple time (a bit more depending on the size of the partition), your partition | + | which took 358 minutes, which is 6 hours. |
| - | you can now decrypt | + | Then you can decrypt |
| < | < | ||
| - | sudo cryptsetup luksOpen /dev/sdb2 cryptodisk | + | sudo cryptsetup luksOpen /dev/[ROOT_PARTITION] |
| </ | </ | ||
| - | you will be prompted with your passphrase. | + | |
| + | you will have to enter your passphrase. | ||
| < | < | ||
| - | lsblk | + | sudo resize2fs / |
| </ | </ | ||
| - | gives a different result: | + | |
| + | which gave in my case: | ||
| < | < | ||
| - | NAME | + | resize2fs |
| - | sda 8:0 0 465.8G | + | Resizing the filesystem on /dev/ |
| - | ├─sda1 | + | The filesystem on /dev/mapper/ |
| - | ├─sda2 | + | |
| - | └─sda3 | + | |
| - | └─disk1 | + | |
| - | ├─usera--computer--vg-root | + | |
| - | | + | |
| - | └─usera--computer--vg-home | + | |
| - | sdb 8:16 | + | |
| - | ├─sdb1 | + | |
| - | └─sdb2 | + | |
| - | └─cryptodisk | + | |
| - | sr0 | + | |
| </ | </ | ||
| - | We will now prepare | + | Now, you need to note down the content of the file /etc/resolv.conf, which is use by linux to access internet: |
| < | < | ||
| - | sudo apt install lvm2 | + | cat / |
| </ | </ | ||
| - | you will need to execute | + | and you can now do a special operation |
| + | 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: | ||
| - | You first create what we call a " | ||
| < | < | ||
| - | sudo pvcreate | + | sudo mount / |
| + | sudo mount / | ||
| + | sudo mount / | ||
| + | sudo mount --bind /dev /mnt/dev | ||
| + | sudo mount --bind /dev/pts / | ||
| + | sudo mount --bind /sys /mnt/sys | ||
| + | sudo mount --bind /proc /mnt/proc | ||
| + | sudo chroot /mnt | ||
| </ | </ | ||
| - | then you create | + | 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: | ||
| < | < | ||
| - | sudo vgcreate vgbackup / | + | ping google.com |
| </ | </ | ||
| - | Then you create two " | + | if you see something like this: |
| < | < | ||
| - | sudo lvcreate | + | (base) usera@debian: |
| - | sudo lvcreate | + | 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 can check again the output | + | then you are good to go, you can do CTRL+C to stop it. Otherwise, try copying |
| < | < | ||
| - | lsblk | + | nano / |
| </ | </ | ||
| - | You can see that we have the right partitioning: | + | then paste the content, and enter CTRL+X, then Y do save the changes. Try again to ping: |
| < | < | ||
| - | NAME | + | ping google.com |
| - | sda 8:0 0 465.8G 0 disk | + | |
| - | ├─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 a "file system" | + | It should work now. |
| + | |||
| + | Now, install some required packages: | ||
| < | < | ||
| - | sudo mkfs.ext4 / | + | sudo apt install cryptsetup cryptsetup-initramfs |
| - | sudo mkfs.ext4 / | + | |
| </ | </ | ||
| - | this formatted those partitions with the filesystem called " | ||
| - | In what we did, there are several levels of partitions that you need to know: | + | and let us do some modifications. |
| - | {{ :: | + | Enter the following command: |
| - | You can then close the LVM: | ||
| < | < | ||
| - | sudo vgchange -an vgbackup | + | blkid |
| </ | </ | ||
| - | and the luks container: | + | in my case I get: |
| < | < | ||
| - | sudo cryptsetup luksClose | + | /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 / | ||
| - | ==== STEP 1.3: do the actual backup ==== | + | < |
| + | nano / | ||
| + | </ | ||
| - | you can then follow | + | and add the following line, replacing |
| - | 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 | + | cryptodisk UUID=[UUID] none luks, |
| </ | </ | ||
| - | Before launching it, we need to decrypt | + | you can exit the editor with CTRL+X, then type Y for saving it. |
| + | then note the UUID of the decrypted partition, here / | ||
| < | < | ||
| - | sudo cryptsetup luksOpen | + | nano /etc/fstab |
| - | sudo vgchange -ay | + | |
| - | sudo mount / | + | |
| </ | </ | ||
| - | now everything written on /mnt on your computer will be written on your disk. | + | you should check the line that has the mount point "/", and check that it is like the following, otherwise edit it and save it: |
| - | You can then launch backintime with: | ||
| < | < | ||
| - | sudo backintime-qt | + | / |
| </ | </ | ||
| - | you can then configure it to save things in /mnt: | + | you need finally |
| + | < | ||
| + | nano / | ||
| + | </ | ||
| - | {{ :: | + | you need to locate the entry "GRUB_CMDLINE_LINUX=" and change |
| - | + | where you change <LUKS partition UUID> for the UUID of your encrypted partition. | |
| - | + | You can then enter: | |
| - | {{ :: | + | |
| - | + | ||
| - | + | ||
| - | {{ :: | + | |
| - | + | ||
| - | And then click on "Take snapshot" and wait for it to finish: | + | |
| - | + | ||
| - | {{ :: | + | |
| - | + | ||
| - | you can then unmount and remove your backup disk: | + | |
| < | < | ||
| - | sudo umount /mnt | + | sudo update-initramfs -u |
| - | sudo vgchange | + | |
| - | sudo cryptsetup luksClose cryptodisk | + | |
| </ | </ | ||
| - | ===== STEP 2: Setup your partitioning ===== | + | and then: |
| - | + | ||
| - | There are several ways a Linux distribution can be installed without encryption. Check the output of: | + | |
| < | < | ||
| - | lsblk | + | update-grub |
| </ | </ | ||
| - | Ideally, you should have on your laptop hard drive 2 or 3 partitions. | + | and finally |
| - | + | ||
| - | - 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 /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: | + | |
| - | + | ||
| - | - The ESP (or EFI) partition | + | |
| - | - One partition for the / | + | |
| - | - One partition for the /boot | + | |
| - | + | ||
| - | You most likely have the two first one. 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 | + | exit |
| + | sudo shutdown -r now | ||
| </ | </ | ||
| - | Select | + | you will then remove |
| - | + | ||
| - | If you see something in the "Mount point" column, right click and click on " | + | |
| - | + | ||
| - | {{:: | + | |
| - | + | ||
| - | You can now create | + | |
| - | + | ||
| - | {{ :: | + | |
| - | + | ||
| - | {{ :: | + | |
| - | + | ||
| - | {{ :: | + | |
| - | + | ||
reencrypt_linux_post_install.1746038676.txt.gz · Last modified: by usera
