Change the root directory to the specified destination directory

added

Using the chroot command, you can run commands in the specified root directory. Chroot, that is, change root directory In Linux, the default directory structure starts with /, that is, root. With chroot, the system’s directory structure takes the specified location as the/location.

After the chroot command, the system will read directories and files under the new root (that is, the specified new location) instead of the old system root directory structure and files, so this brings the following three benefits:

Increased system security and limited user power:

After chroot, the root structure and files of the old system cannot be accessed under the new root, which increases system security. This is typically done by using chroot before login so that the user cannot access certain files.

Establish a system directory structure isolated from the original system to facilitate user development:

With chroot, the system reads the directories and files under the new root, which is a directory structure unrelated to the files under the original system root. In this new environment, it can be used to test static compilation of software and some independent development unrelated to the system.

Switch the root directory of the system, boot the Linux system and the first aid system, etc. :

The role of chroot is to switch the root of the system, and this role is most obvious in the process of the initial boot disk of the system, switching the root of the system from the initial RAM disk (initrd) and performing a real init. Alternatively, we can use chroot to switch to a temporary system when something goes wrong with the system.

The command format

> chroot(option)(parameter)Copy the code

Command options

--help: online help; --version: displays the version information.Copy the code

The command parameter

  • Directory: specifies the new root directory.
  • Instruction: Specifies the instruction to be executed.

The instance

Make target the root directory (run within it/bin/sh) :

chroot target /bin/sh
Copy the code

In this case, target is the path where BusyBox is installed, similar to a file system that contains many tools. This will bring you to a shell interface with Target as the root. To exit the shell and return to the native environment, you can also use Ctrl+D.

Note:

  • Root
  • If chroot target is chroot target, the default is /bin/bash. This will take target as the root directory

Using target as the root directory (run /bin/ls):

chroot target /bin/ls
Copy the code

In this case, target is the path where BusyBox is installed, similar to a file system that contains many tools. This runs ls in target (not native /bin/ls) and returns to the immediate native directory environment.

A.out contains dynamically linked libraries. You need to use LDD to check which dynamic libraries a.out needs. Copy these libraries to the corresponding path of the new root to execute.

Run a self-compiled program with chroot:

Prepare the root directory of chroot:

mkdir rumenz
Copy the code

Compile your own program:

gcc main.c
Copy the code

Here main.c generates a.out, which prints Hello.

View libraries required by the program:

ldd a.out
Copy the code

After the input, the output is as follows:

linux-gate.so.1 = >  (0xb8034000)
libc.so.6 = > /lib/tls/i686/cmov/libc.so.6 (0xb7eab000)
/lib/ld-linux.so.2 (0xb801a000)
Copy the code

Copy the required libraries and programs to the new root directory:

cp a.out rumenz
mkdir rumenz/lib
cp /lib/tls/i686/cmov/libc.so.6 rumenz/lib
cp /lib/ld-linux.so.2 rumenz/lib
Copy the code

Here rumenz will look like this:

a.out lib/
Copy the code

Run your own program using chroot:

su
chroot rumenz /a.out
Copy the code

This will run a.out correctly. Because A.out uses other dynamically linked libraries, you need to copy the libraries to Rumenz. If there are no other libraries, just copy A.out to run. For example, when busybox is statically compiled, /bin/busybox in its installation directory does not depend on other libraries.

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station