Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the chroot command in Linux

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the chroot command in Linux. It is very detailed and has a certain reference value. Friends who are interested must finish it!

The chroot command is used to run instructions in the specified root directory. Chroot, or change root directory (change the root directory). In linux systems, the default directory structure of the system starts with /, that is, the root. After using chroot, the directory structure of the system will take the specified location as / location

Chroot changes the root directory to the specified destination directory

It is added that after the chroot command, the directories and files read by the system will not be the directory structure and files under the old system root but under the new root (that is, the new location specified), so it will roughly bring the following three benefits:

It increases the security of the system and limits the rights of users:

After chroot, the root structure and files of the old system will not be accessed under the new root, thus enhancing the security of the system. This is generally done by using chroot before logging in (login) so that users cannot access certain files.

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

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

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

The role of chroot is to switch the root location of the system, and this effect is most obvious in the processing of the initial boot disk of the system, switching the root location of the system from the initial RAM disk (initrd) and performing the real init. In addition, when there are some problems with the system, we can also use chroot to switch to a temporary system.

Syntax chroot (options) (parameters) options-- help: online help;-- version: display version information. Parameter directory: specify a new root directory

Instruction: specifies the instruction to be executed.

The instance uses target as the root directory (run / bin/sh in it):

Chroot target / bin/sh here, target is the path where busybox is installed, similar to a file system that contains a lot of tools. In this way, you will enter a shell interface where the shell is rooted in target. Run exit to exit the shell and return to the original native environment, or you can use Ctrl+D.

Note:

Only the root user

If chroot target directly looks for / bin/bash of target by default. This will take target as the root directory

Use target as the root directory (run / bin/ls in it):

Chroot target / bin/ls here, target is the path where busybox is installed, similar to a file system that contains a lot of tools. This runs ls in target (not native / bin/ls), and then returns to the immediate native directory environment.

Note that after compiling a program locally to generate a.out, copying it into target/bin/ does not work, because it contains dynamically linked libraries, so you need to use ldd to see which dynamic libraries a.out needs, and copy these libraries to the corresponding path of the new root.

Run a self-compiled program with chroot:

Prepare the root directory of chroot:

Mkdir newRoot compiles its own program:

Gcc main.c here main.c generates a.out, and the function is to output hello.

View the libraries required by the program:

After the ldd a.out is imported, 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 libraries and programs needed by the program to the new root directory:

Cp a.out newRoot mkdir newRoot/lib cp / lib/tls/i686/cmov/libc.so.6 newRoot/lib cp / lib/ld-linux.so.2 newRoot/lib here the newRoot content will be as follows:

A.out lib/ uses chroot to run its own programs:

Su chroot newRoot / a.out will run a.out correctly, because a.out uses other dynamic link libraries, so you need to copy the libraries into newRoot, and if there are no other libraries, then copy a.out directly to run. For example, after statically compiled busybox, the / bin/busybox in its installation directory does not depend on other libraries.

The above is all the contents of the article "how to use chroot commands in Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report