{"id":17139105,"url":"https://github.com/virb3/pi-encrypted-boot-ssh","last_synced_at":"2025-06-16T22:32:07.795Z","repository":{"id":43257669,"uuid":"234822448","full_name":"ViRb3/pi-encrypted-boot-ssh","owner":"ViRb3","description":"🔑 Raspberry Pi Encrypted Boot with Remote SSH","archived":false,"fork":false,"pushed_at":"2024-01-09T22:03:11.000Z","size":50,"stargazers_count":205,"open_issues_count":4,"forks_count":22,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-20T11:58:32.780Z","etag":null,"topics":["boot","dropbear","encrypted","linux","pi","raspberry","remote","secure","ssh","ubuntu"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ViRb3.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-01-19T01:34:37.000Z","updated_at":"2025-03-24T12:34:15.000Z","dependencies_parsed_at":"2024-12-26T19:07:03.145Z","dependency_job_id":"25db58aa-b9ba-40bf-a566-4bb21acf8243","html_url":"https://github.com/ViRb3/pi-encrypted-boot-ssh","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ViRb3/pi-encrypted-boot-ssh","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViRb3%2Fpi-encrypted-boot-ssh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViRb3%2Fpi-encrypted-boot-ssh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViRb3%2Fpi-encrypted-boot-ssh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViRb3%2Fpi-encrypted-boot-ssh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ViRb3","download_url":"https://codeload.github.com/ViRb3/pi-encrypted-boot-ssh/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ViRb3%2Fpi-encrypted-boot-ssh/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260251265,"owners_count":22981003,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["boot","dropbear","encrypted","linux","pi","raspberry","remote","secure","ssh","ubuntu"],"created_at":"2024-10-14T20:11:24.860Z","updated_at":"2025-06-16T22:32:07.768Z","avatar_url":"https://github.com/ViRb3.png","language":null,"readme":"# Raspberry Pi Encrypted Boot with SSH\n\n\u003e ⚠️ This guide is only tested on the [Raspberry Pi 5](https://www.raspberrypi.com/products/raspberry-pi-5/) with [Raspberry Pi OS Lite 64-bit 2023-12-11](https://www.raspberrypi.com/software/operating-systems/). \\\n\u003e Other platforms and distributions may work, but there may be unexpected issues or side effects.\n\n## Introduction\n\nThis guide will show you how to encrypt your Raspberry Pi's root partition and set up an [initramfs](https://en.wikipedia.org/wiki/Initial_ramdisk) that will prompt for the password, decrypt the partition and gracefully resume boot. You will also learn how to enable SSH during this pre-boot stage, allowing you to unlock the partition remotely. There are also optional steps for WiFi setup.\n\nThis guide operates directly on an image file and therefore does not require an SD card for the setup. The resulting image can be flashed to an SD card as usual.\n\n## Table of Content\n\n- [Raspberry Pi Encrypted Boot with SSH](#raspberry-pi-encrypted-boot-with-ssh)\n  - [Introduction](#introduction)\n  - [Table of Content](#table-of-content)\n  - [Requirements](#requirements)\n  - [On the host](#on-the-host)\n  - [In the chroot](#in-the-chroot)\n    - [Prepare](#prepare)\n    - [Device configuration](#device-configuration)\n    - [Cryptsetup](#cryptsetup)\n    - [SSH](#ssh)\n    - [WiFi support](#wifi-support)\n      - [Raspberry Pi OS](#raspberry-pi-os)\n      - [Ubuntu (obsolete)](#ubuntu-obsolete)\n    - [Build initramfs](#build-initramfs)\n    - [Finish](#finish)\n  - [On the host](#on-the-host-1)\n  - [On the Raspberry Pi](#on-the-raspberry-pi)\n  - [Avoiding SSH key collisions](#avoiding-ssh-key-collisions)\n  - [Resources](#resources)\n\n## Requirements\n\n- A Raspberry Pi Linux image (e.g. [Raspberry Pi OS Lite 64-bit 2023-12-11](https://www.raspberrypi.com/software/operating-systems/))\n- A computer (host) running Linux (e.g. [Kali Linux 2023.2](https://www.kali.org/get-kali/#kali-platforms))\n\n  \u003e :warning: **NOTE:** Your host's Linux should be as similar as possible to the Raspberry Pi's Linux. If you are preparing Debian 12/kernel 6.1 for the Raspberry Pi, use similar versions on the host, otherwise you may encounter issues inside the chroot.\n\n## On the host\n\nInstall dependencies:\n\n- You can skip `binfmt-support` and `qemu-user-static` if your host Linux's architecture matches that of the Raspberry Pi's Linux image.\n- If your host Linux doesn't use systemd, such as WSL on Windows, you need to manually run `update-binfmts --enable` after installing `binfmt-support`.\n\n```sh\napt update\napt install -y parted kpartx cryptsetup-bin rsync binfmt-support qemu-user-static\n```\n\nCreate two copies of the Raspberry Pi's Linux image — one to read from (base), and one to write to (target):\n\n- pi-base.img\n- pi-target.img\n\nIncrease the size of the target image or you may run into issues:\n\n```bash\ndd if=/dev/zero bs=1G count=1 \u003e\u003e pi-target.img\nparted pi-target.img resizepart 2 100%\n```\n\nMap both images as devices, ensuring the base is readonly:\n\n```sh\nkpartx -ar \"$PWD/pi-base.img\"\nkpartx -a \"$PWD/pi-target.img\"\n```\n\nIf your system automatically mounted any partitions, unmount them:\n\n```sh\numount /media/**/*\n```\n\nRun [lsblk](https://linux.die.net/man/8/lsblk) and verify the process was successful — you should see two loopback devices, each with two partitions:\n\n```sh\nNAME      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT # COMMENT\nloop0       7:0    0  3.3G  0 loop            # pi-base.img\n├─loop0p1 253:0    0  256M  0 part            # ├─ boot\n└─loop0p2 253:1    0    3G  0 part            # └─ root\nloop1       7:1    0  3.3G  1 loop            # pi-target.img\n├─loop1p1 253:2    0  256M  1 part            # ├─ boot\n└─loop1p2 253:3    0    3G  1 part            # └─ root\n```\n\nMount the base image's root partition:\n\n```sh\nmkdir -p /mnt/original/\nmount /dev/mapper/loop0p2 /mnt/original/\n```\n\nReplace the target image's root partition with a new, encrypted partition:\n\n\u003e :warning: **NOTE:**\n\u003e\n\u003e The default encryption algorithm is `aes-xts-plain64`, which is fast only on the Raspberry Pi 5 due to its hardware AES acceleration. If you have an older generation, then use [aes-adiantum](https://github.com/google/adiantum) instead via `-c xchacha20,aes-adiantum-plain64`. It is much faster than AES in software.\n\u003e\n\u003e By default cryptsetup will [benchmark](https://man7.org/linux/man-pages/man8/cryptsetup-luksformat.8.html) your host and use a memory-hard PBKDF algorithm that can require up to 4GB of RAM. If these settings exceed your Raspberry Pi's available RAM, it will make it impossible to unlock the partition. To work around this, set the [--pbkdf-memory](https://man7.org/linux/man-pages/man8/cryptsetup-luksformat.8.html) and [--pbkdf-parallel](https://man7.org/linux/man-pages/man8/cryptsetup-luksformat.8.html) arguments so when you multiply them, the result is less than your Pi's total RAM. For example: `--pbkdf-memory 512000 --pbkdf-parallel=1`\n\n```sh\ncryptsetup luksFormat /dev/mapper/loop1p2\n```\n\nOpen (decrypt) the new partition:\n\n```\ncryptsetup open /dev/mapper/loop1p2 crypted\n```\n\nThen format and mount it:\n\n```\nmkfs.ext4 /dev/mapper/crypted\nmkdir -p /mnt/chroot/\nmount /dev/mapper/crypted /mnt/chroot/\n```\n\nCopy the base image's root partition files to the target image's new, encrypted root partition. You can use [dd](https://linux.die.net/man/1/dd), but [rsync](https://linux.die.net/man/1/rsync) is faster:\n\n```sh\nrsync --archive --hard-links --acls --xattrs --one-file-system --numeric-ids --info=\"progress2\" /mnt/original/* /mnt/chroot/\n```\n\nSet up a [chroot](https://linux.die.net/man/1/chroot) by mounting the target image's boot partition and required virtual filesystems from the host:\n\n```sh\nmkdir -p /mnt/chroot/boot/\nmount /dev/mapper/loop1p1 /mnt/chroot/boot/\nmount -t proc none /mnt/chroot/proc/\nmount -t sysfs none /mnt/chroot/sys/\nmount -o bind /dev /mnt/chroot/dev/\nmount -o bind /dev/pts /mnt/chroot/dev/pts/\n```\n\nEnter the chroot:\n\n```sh\nLANG=C chroot /mnt/chroot/ /bin/bash\n```\n\n## In the chroot\n\n### Prepare\n\nInstall the dependencies:\n\n```sh\napt update\napt install -y busybox cryptsetup dropbear-initramfs\n```\n\n### Device configuration\n\nEdit [/etc/fstab](https://linux.die.net/man/5/fstab) and replace the root entry with your decrypted (virtual) partition's device name:\n\n```sh\n# Original:\nPARTUUID=e8af6eb2-02 / ext4 defaults,noatime          0 1\n# Replace with:\n/dev/mapper/crypted  / ext4 defaults,noatime          0 1\n```\n\nRun [blkid](https://linux.die.net/man/8/blkid) and note the details of your encrypted partition:\n\n```sh\nblkid | grep crypto_LUKS\n\n/dev/mapper/loop1p2: UUID=\"aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa\" TYPE=\"crypto_LUKS\" PARTUUID=\"cccccccc-cc\"\n```\n\nEdit [/etc/crypttab](https://linux.die.net/man/5/crypttab) and add an entry with your encrypted (raw) partition's UUID:\n\n```sh\ncrypted UUID=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa none luks,initramfs\n```\n\nEdit `/boot/cmdline.txt` and update the root entry:\n\n```sh\n# Original:\nroot=PARTUUID=21e60f8c-02\n# Replace with:\nroot=/dev/mapper/crypted cryptdevice=UUID=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:crypted\n```\n\n### Cryptsetup\n\nEdit the cryptsetup initramfs hook to ensure cryptsetup ends up in the initramfs:\n\n```sh\necho \"CRYPTSETUP=y\" \u003e\u003e /etc/cryptsetup-initramfs/conf-hook\n```\n\nThe [initramfs-tools](https://manpages.ubuntu.com/manpages/xenial/man8/initramfs-tools.8.html) `cryptroot` hook will resolve any UUIDs to device names during initramfs generation. This is a problem because the device names will likely differ between the host and the Raspberry Pi, resulting in failure to boot. To work around this, apply the following patch:\n\n```patch\npatch --no-backup-if-mismatch /usr/share/initramfs-tools/hooks/cryptroot \u003c\u003c 'EOF'\n--- cryptroot\n+++ cryptroot\n@@ -33,7 +33,7 @@\n         printf '%s\\0' \"$target\" \u003e\u003e\"$DESTDIR/cryptroot/targets\"\n         crypttab_find_entry \"$target\" || return 1\n         crypttab_parse_options --missing-path=warn || return 1\n-        crypttab_print_entry\n+        printf '%s %s %s %s\\n' \"$_CRYPTTAB_NAME\" \"$_CRYPTTAB_SOURCE\" \"$_CRYPTTAB_KEY\" \"$_CRYPTTAB_OPTIONS\" \u003e\u00263\n     fi\n }\nEOF\n```\n\nThe default timeout when waiting for decryption (10 seconds) may be too short and result in a timeout error. To work around this, bump the value:\n\n```sh\nsed -i 's/^TIMEOUT=.*/TIMEOUT=100/g' /usr/share/cryptsetup/initramfs/bin/cryptroot-unlock\n```\n\n### SSH\n\nWrite your SSH public key inside dropbear's and your decrypted OS's `authorized_keys` and fix permissions:\n\n```sh\nmkdir -p /root/.ssh \u0026\u0026 chmod 0700 /root/.ssh\necho \"/REDACTED/\" | tee /etc/dropbear/initramfs/authorized_keys /root/.ssh/authorized_keys\nchmod 0600 /etc/dropbear/initramfs/authorized_keys /root/.ssh/authorized_keys\n```\n\n### WiFi support\n\nThis step is optional. If you want the Raspberry Pi to be decryptable over WiFi, check out the guides below. Note that the differences between distros is very small, so you can easily adapt any particular guide.\n\n#### Raspberry Pi OS\n\n- [Wireless-USB2.md](Wireless-USB2.md)\n\n#### Ubuntu (obsolete)\n\n- [Wireless-Builtin.md](Wireless-Builtin.md)\n- [Wireless-USB.md](Wireless-USB.md)\n\n### Build initramfs\n\n\nNote your kernel version. If there are multiple, choose the one you want to run. The `2712` suffix is for Raspberry Pi 5, while `v8` is for all previous generations:\n\n```sh\nls /lib/modules/\n```\n\n\u003e :warning: **NOTE:**\n\u003e\n\u003e Starting with the Raspberry Pi 5, the default kernel page size is 16K instead of 4K. This breaks some software, but more importantly, is experimental in btrfs with 4K sector size disks:\n\u003e\n\u003e \u003e BTRFS warning (device dm-1): read-write for sector size 4096 with page size 16384 is experimental\n\u003e\n\u003e For this reason, you may want to switch back to the old kernel by adding the following to your `/boot/config.txt`:\n\u003e\n\u003e ```sh\n\u003e echo \"kernel=kernel8.img\" \u003e\u003e /boot/config.txt\n\u003e echo \"initramfs initramfs8 followkernel\" \u003e\u003e /boot/config.txt\n\u003e ```\n\nBuild the new initramdisk using the kernel version from above. The `initramfs-tools` package parses the compression method from a file that doesn't exist, so we need to create it:\n\n```sh\nkversion=\"6.1.0-rpi7-rpi-v8\" # \"6.1.0-rpi7-rpi-2712\" for 16k pages\necho \"CONFIG_RD_ZSTD=y\" \u003e /boot/config-$kversion\nmkinitramfs -o /boot/initramfs8 $kversion # \"initramfs_2712\" for 16K pages\nrm /boot/config-$kversion\n```\n\nCustomize first run/init in `/boot/cmdline.txt` as it will break the encrypted setup and prevent booting:\n\n```sh\n# Original:\n[...] quiet init=/usr/lib/raspberrypi-sys-mods/firstboot\n# Replace with:\n[...] systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target\n```\n\nCreate `/boot/firstrun.sh` and customize it for your needs. Most of these scripts allow additional configuration, so feel free to check their source:\n\n```sh\n#!/bin/bash\nset +e\n\n/usr/lib/raspberrypi-sys-mods/imager_custom enable_ssh\n/usr/lib/raspberrypi-sys-mods/regenerate_ssh_host_keys\n/usr/lib/userconf-pi/userconf 'pi'\n/usr/lib/raspberrypi-sys-mods/imager_custom set_wlan 'MyWiFi' 'pass1234' 'GB'\n\nrm -f /boot/firstrun.sh\nsed -i 's| systemd.run.*||g' /boot/cmdline.txt\nexit 0\n```\n\nMake sure the newly created file is executable:\n\n```sh\nchmod +x /boot/firstrun.sh\n```\n\n### Finish\n\n\nSync and exit the chroot:\n\n```sh\nsync\nhistory -c \u0026\u0026 exit\n```\n\n## On the host\n\nUnmount everything and clean up any remaining artifacts:\n\n```sh\numount /mnt/chroot/boot\numount /mnt/chroot/sys\numount /mnt/chroot/proc\numount /mnt/chroot/dev/pts\numount /mnt/chroot/dev\numount /mnt/chroot\ncryptsetup close crypted\numount /mnt/original\nrm -d /mnt/chroot\nrm -d /mnt/original\nkpartx -d \"$PWD/pi-base.img\"\nkpartx -d \"$PWD/pi-target.img\"\n```\n\nYou are now ready to flash `pi-target.img` to an SD card.\n\n## On the Raspberry Pi\n\nBoot the Raspberry Pi with the new SD card. It will obtain an IP address from the DHCP server and start listening for SSH connections. To decrypt the root partition and continue boot, from any shell, simply run `cryptroot-unlock`.\n\nThe first time only:\n\n1. Once decrypted, the system will power off. The reason for this is currently unknown. Restart the device and decrypt again.\n2. The decrypted system will run the init script from before and reboot back into the initramfs. Proceed to decrypt one last time.\n\nOnce booted into the decrypted system, you will notice that the root partition is still sized at ~3GB, no matter how much space you have on the SD card. To fix this, resize the partition:\n\n```sh\nparted /dev/mmcblk0 resizepart 2 100%\ncryptsetup resize crypted\nresize2fs /dev/mapper/crypted\n```\n\nFinally, reboot the system one last time for good measure:\n\n```sh\nreboot\n```\n\n## Avoiding SSH key collisions\n\nTo avoid host key collisions you can configure a separate trusted hosts store in the `~/.ssh/config` of your client:\n\n```ssh\nHost box\n\tHostname 192.168.0.30\n\tUser root\n\nHost box-initramfs\n\tHostname 192.168.0.30\n\tUser root\n\tUserKnownHostsFile ~/.ssh/known_hosts.initramfs\n```\n\n## Resources\n\n- https://www.kali.org/docs/arm/raspberry-pi-with-luks-disk-encryption/\n- https://wiki.archlinux.org/index.php/Dm-crypt/Specialties\n- https://wiki.gentoo.org/wiki/Custom_Initramfs\n- https://www.raspberrypi.org/forums/viewtopic.php?t=252980\n- https://thej6s.com/articles/2019-03-05__decrypting-boot-drives-remotely/\n- https://www.pbworks.net/ubuntu-guide-dropbear-ssh-server-to-unlock-luks-encrypted-pc/\n- https://raspberrypi.stackexchange.com/questions/92557/how-can-i-use-an-init-ramdisk-initramfs-on-boot-up-raspberry-pi/\n- https://www.raspberrypi.com/documentation/computers/configuration.html#setting-up-a-headless-raspberry-pi\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvirb3%2Fpi-encrypted-boot-ssh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvirb3%2Fpi-encrypted-boot-ssh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvirb3%2Fpi-encrypted-boot-ssh/lists"}