![linux chroot](https://i-blog.csdnimg.cn/blog_migrate/4611f52f88d266c38c08ec83f7e19f43.png)
linux chroot
Linux provides different mechanisms for practical and security reasons. chroot
is one of them. Processes in linux can access to the file system or root by default. Linux kernel also provides chroot
mechanism to restrict access to the whole filesystem in Linux.
出于实用和安全原因,Linux提供了不同的机制。 chroot
是其中之一。 缺省情况下,Linux中的进程可以访问文件系统或root。 Linux内核还提供chroot
机制,以限制对Linux中整个文件系统的访问。
句法 (Syntax)
The chroot syntax is like below.
chroot语法如下所示。
chroot OPTION NEWROOT COMMAND
OPTION
is chroot optionOPTION
是chroot选项NEWROOT
is the new root directoryNEWROOT
是新的根目录COMMAND
is the command we want to run in the chrooted directoryCOMMAND
是我们要在chroot目录中运行的命令
Chroot监狱或监狱目录(Chroot Jail or Jailed Directory)
We can create a jailed directory or chroot jail just using chroot
command with the path we want to use as jail. After the chroot the new root will be the given path. In this example we will chroot to the /home/ismail/
. After chroot all contents of the /home/ismail
will be served as /
root directory.
我们可以使用chroot
命令以及要用作监狱的路径来创建一个被监禁的目录或chroot监狱。 在chroot之后,新的根将成为给定的路径。 在此示例中,我们将chroot到/home/ismail/
。 chroot之后, /home/ismail
所有内容将用作/
根目录。
$ chroot /home/ismail /bin/bash
But keep in mind that before chrooting we need two things.
但是请记住,在扎根之前,我们需要两件事。
We need root privileges to run
chroot
command我们需要root特权才能运行
chroot
命令- We should provide command and required libraries we want to run in chrooted environment我们应该提供要在chroot环境中运行的命令和必需的库
指定用户和用户标识(Specify User and User ID)
We can specify the user we want to use in chrooted environment as process owner. We will use --userspec
and the user id or name. In this example we will use user named ismail
.
我们可以指定要在chroot环境中使用的用户作为进程所有者。 我们将使用--userspec
和用户ID或名称。 在此示例中,我们将使用名为ismail
用户。
$ chroot --userspec=ismail /home/ismail /bin/bash
OR we will use user id
否则我们将使用用户ID
$ chroot --userspec=1001 /home/ismail /bin/bash
指定组和组ID (Specify Group and Group ID)
We can also specify the group name or group ID we want to use for the chrooted process. We will use --groups
option. We can specify multiple groups by separating the group ids or names with comma. In this example we will use group ismail
我们还可以指定要用于chroot进程的组名或组ID。 我们将使用--groups
选项。 我们可以通过用逗号分隔组ID或名称来指定多个组。 在此示例中,我们将使用组ismail
$ chroot --groups=ismail /home/ismail /bin/bash
OR we can use group ID like below
或者我们可以使用如下的组ID
$ chroot --groups=1001 /home/ismail /bin/bash
请勿更改工作目录 (Do Not Change Working Directory)
We can also skip changing current working directory of the chrooted process to the /
root. We will us --skip-chdir
. We will use current root as chrooted root.
我们还可以跳过将chroot进程的当前工作目录更改为/
root。 我们将--skip-chdir
。 我们将使用当前根作为chroot根。
$ chroot --skip-chdir / /bin/bash
翻译自: https://www.poftut.com/linux-chroot-command-tutorial-examples/
linux chroot