Title: 🐧 Chroot Cheatsheet Group: Storage & FS Icon: 🐧 Order: 4 # Chroot / Π‘ΠΌΠ΅Π½Π° ΠΊΠΎΡ€Π½Π΅Π²ΠΎΠ³ΠΎ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π° Chroot (change root) is an operation that changes the apparent root directory for the current running process and its children. This is typically used for system recovery, bootloader repair, or software testing in an isolated environment. ## Table of Contents - [Core Management](#core-management) - [Sysadmin Operations](#sysadmin-operations) - [Comparison: BIOS vs UEFI](#comparison-bios-vs-uefi) - [Troubleshooting & Tips](#troubleshooting--tips) --- ## Core Management / ОсновноС ΡƒΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ ### 1. Identify Partitions / ΠžΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ Ρ€Π°Π·Π΄Π΅Π»ΠΎΠ² Identify the root, boot, and EFI partitions before mounting. ```bash lsblk -f # List disks and partitions with filesystems / Бписок дисков ΠΈ Ρ€Π°Π·Π΄Π΅Π»ΠΎΠ² blkid # Show UUIDs of partitions / ΠŸΠΎΠΊΠ°Π·Π°Ρ‚ΡŒ UUID Ρ€Π°Π·Π΄Π΅Π»ΠΎΠ² ``` ### 2. Mount Root Filesystem / ΠœΠΎΠ½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΊΠΎΡ€Π½Π΅Π²ΠΎΠΉ систСмы Mount the main partition where the OS is installed. ```bash mkdir -p /mnt/sysroot # Create mount point / Π‘ΠΎΠ·Π΄Π°Ρ‚ΡŒ Ρ‚ΠΎΡ‡ΠΊΡƒ монтирования mount /dev/sdXN /mnt/sysroot # Mount root partition / Π‘ΠΌΠΎΠ½Ρ‚ΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠΎΡ€Π½Π΅Π²ΠΎΠΉ Ρ€Π°Π·Π΄Π΅Π» ``` ### 3. Mount System Directories / ΠœΠΎΠ½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ систСмных Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΉ Bind essential system virtual filesystems to the new root. ```bash mount --bind /dev /mnt/sysroot/dev # Bind devices / ΠŸΡ€ΠΎΠ±Ρ€ΠΎΡΠΈΡ‚ΡŒ устройства mount --bind /proc /mnt/sysroot/proc # Bind processes / ΠŸΡ€ΠΎΠ±Ρ€ΠΎΡΠΈΡ‚ΡŒ процСссы mount --bind /sys /mnt/sysroot/sys # Bind system info / ΠŸΡ€ΠΎΠ±Ρ€ΠΎΡΠΈΡ‚ΡŒ ΡΠΈΡΡ‚Π΅ΠΌΠ½ΡƒΡŽ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ mount --bind /run /mnt/sysroot/run # Bind runtime data / ΠŸΡ€ΠΎΠ±Ρ€ΠΎΡΠΈΡ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ выполнСния ``` ### 4. Enter Chroot Environment / Π’Ρ…ΠΎΠ΄ Π² ΠΎΠΊΡ€ΡƒΠΆΠ΅Π½ΠΈΠ΅ Chroot Switch to the mounted system environment. ```bash chroot /mnt/sysroot /bin/bash # Enter using Bash / Π’ΠΎΠΉΡ‚ΠΈ Ρ‡Π΅Ρ€Π΅Π· Bash # OR / Π˜Π›Π˜ chroot /mnt/sysroot /bin/sh # Fallback to SH / Π’ΠΎΠΉΡ‚ΠΈ Ρ‡Π΅Ρ€Π΅Π· SH ``` ### 5. Exit and Cleanup / Π’Ρ‹Ρ…ΠΎΠ΄ ΠΈ очистка Always unmount recursively to ensure all bind mounts are released. ```bash exit # Exit chroot / Π’Ρ‹ΠΉΡ‚ΠΈ ΠΈΠ· chroot umount -R /mnt/sysroot # Recursive unmount / РСкурсивноС Ρ€Π°Π·ΠΌΠΎΠ½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ ``` > [!CAUTION] > Failure to unmount correctly before rebooting can occasionally lead to filesystem synchronization issues. --- ## Sysadmin Operations / ΠžΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ систСмного администратора ### Kernel & Initramfs Management / Π£ΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅ ядром ΠΈ Initramfs Rebuilding the initial ramdisk after kernel updates. ```bash ls /boot # List kernels / Бписок ядСр dpkg -l | grep linux-image # Check installed kernels / ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ установлСнныС ядра update-initramfs -u -k # Update specific kernel / ΠžΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΠΎΠ΅ ядро update-initramfs -u -k all # Update all kernels / ΠžΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ всС ядра ``` ### Bootloader Repair (GRUB) / ВосстановлСниС Π·Π°Π³Ρ€ΡƒΠ·Ρ‡ΠΈΠΊΠ° (GRUB) Installing and updating the GRUB configuration. ```bash # BIOS Version: update-grub # Update config / ΠžΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΡŽ # UEFI Version: grub-install /dev/sdX # Install bootloader to disk / Π£ΡΡ‚Π°Π½ΠΎΠ²ΠΈΡ‚ΡŒ Π·Π°Π³Ρ€ΡƒΠ·Ρ‡ΠΈΠΊ Π½Π° диск update-grub # Update config / ΠžΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΡŽ ``` --- ## Comparison: BIOS vs UEFI / Π‘Ρ€Π°Π²Π½Π΅Π½ΠΈΠ΅: BIOS ΠΈ UEFI | Feature | BIOS (Legacy) | UEFI | | :--- | :--- | :--- | | **Boot Partition** | Often optional or integrated. | **MANDATORY** FAT32 EFI System Partition (ESP). | | **Mounting** | `mount /dev/sdXY /mnt/sysroot/boot` | `mount /dev/sdXZ /mnt/sysroot/boot/efi` | | **Firmware Bits** | N/A | `mount --bind /sys/firmware/efi/efivars ...` | | **Why?** | Simple MBR-based boot. | Modern, supports Secure Boot and GPT > 2TB. | --- ## Troubleshooting & Tips / УстранСниС Π½Π΅ΠΏΠΎΠ»Π°Π΄ΠΎΠΊ ΠΈ совСты ### Common Fixes / Π’ΠΈΠΏΠΈΡ‡Π½Ρ‹Π΅ исправлСния If the system fails to boot after an update, reinstalling the kernel often helps. ```bash apt update # Update package lists / ΠžΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ списки ΠΏΠ°ΠΊΠ΅Ρ‚ΠΎΠ² apt install --reinstall linux-image-generic # Reinstall kernel / ΠŸΠ΅Ρ€Π΅ΡƒΡΡ‚Π°Π½ΠΎΠ²ΠΈΡ‚ΡŒ ядро update-initramfs -u -k all # Rebuild images / ΠŸΠ΅Ρ€Π΅ΡΠΎΠ±Ρ€Π°Ρ‚ΡŒ ΠΎΠ±Ρ€Π°Π·Ρ‹ update-grub # Refresh GRUB / ΠžΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ GRUB ``` > [!TIP] > Use `ls -lh /boot` to verify that both `vmlinuz-*` (kernel) and `initrd.img-*` (initramfs) pairs exist for your version. ### Log Locations / РасполоТСниС Π»ΠΎΠ³ΠΎΠ² Checking logs inside chroot after a failed boot attempt. - `/var/log/syslog` β€” General system logs / ΠžΠ±Ρ‰ΠΈΠ΅ систСмныС Π»ΠΎΠ³ΠΈ - `/var/log/apt/history.log` β€” Package history / Π˜ΡΡ‚ΠΎΡ€ΠΈΡ установки ΠΏΠ°ΠΊΠ΅Ρ‚ΠΎΠ² - `/var/log/boot.log` β€” Boot process logs / Π›ΠΎΠ³ΠΈ процСсса Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ