chroot
Change the root directory to a specified target directory
Additional Explanation
<span>chroot</span> command is used to run commands in a specified root directory.<span>chroot</span>, which stands for change root directory, modifies the default directory structure of the Linux system, which typically starts from<span>/</span>, the root. After using<span>chroot</span>, the system’s directory structure will start from the specified location as the new root.
After executing the <span>chroot</span> command, the directories and files that the system reads will no longer be from the old system root but from the new root (i.e., the specified new location), thus providing the following three main benefits:
Increased system security by limiting user privileges:
After using chroot, the new root will not have access to the old system’s directory structure and files, enhancing system security. This is generally done before user login to prevent access to certain files.
Establishing an isolated system directory structure for user development:
After using chroot, the system reads directories and files from the new root, which is unrelated to the original system’s root file structure. This new environment can be used for testing software static compilation and independent development unrelated to the system.
Switching the system’s root directory for booting Linux systems and recovery systems:
The function of chroot is to switch the system’s root location, which is most evident during the initial boot disk processing, where it switches the system’s root from the initial RAM disk (initrd) and executes the actual init. Additionally, when the system encounters issues, we can also use chroot to switch to a temporary system.
Detailed Usage
- Prepare the target root filesystem
- Mount the necessary virtual filesystems, as the chroot environment needs access to core virtual filesystems to function properly. (The following are provided: process information, system devices and driver information, device files, pseudo terminals, temporary files)
mount -t proc /proc /mnt/test_root/proc
mount -t sysfs /sys /mnt/test_root/sys
mount -t devtmpfs /dev /mnt/test_root/dev
mount -t devpts /dev/pts /mnt/test_root/dev/pts
mount -t tmpfs /tmp /mnt/test_root/tmp
- Enter the mounted environment using chroot
chroot /mnt/test_root
- This is equivalent to being in a completely new system; to exit, follow these steps:
exit
umount /mnt/test_root/proc
umount /mnt/test_root/sys
umount /mnt/test_root/dev/pts
umount /mnt/test_root/dev
umount /mnt/test_root/tmp
umount /mnt/test_root
Common Scenarios
Repairing Grub Bootloader
After entering from Live, mount the original system’s root partition (/dev/sda1) to /mnt,<span>mount /dev/sda1 /mnt</span>. Then mount other filesystems as follows:<span>mount --bind /dev /mnt/dev</span>,<span>mount --bind /proc /mnt/proc</span>,<span>mount --bind /sys /mnt/sys</span>. Finally, use chroot to enter the original system,<span>chroot /mnt</span>. Reinstall and configure grub with<span>grub-install /dev/sda</span> and<span>update-grub</span>. Finally,<span>exit</span> and unmount the previously mounted filesystems, then reboot.
Related Extensions
Windows Security HardeningLinux Security HardeningIntroduction to IC Design Process and R&D EnvironmentInternal Network Data Security, Methods to Access R&D Environment via Bastion HostNFS Permission Denied When IC Users Exceed 16 GroupsCadence EDA Tool InstallationHarmony Patch Tools for the Three Major EDA Companies (S/C/M)Finding Features in EDA and Extracting to .src FilesRecently Released Synopsys License Generation ToolCommon EDA Tool License Call Variable ConfigurationInstalling EDA Tools Using WSL2