← 返回命令列表

Linux command

chroot 命令

安全

权限或系统影响较大,执行前请核对目标。

常用示例

Run a command with a different root directory

sudo chroot [path/to/new_root] [command]

Run an interactive shell in the new root

sudo chroot [path/to/new_root]

Specify a custom shell to run

sudo chroot [path/to/new_root] /bin/bash

Specify user and group for the command

sudo chroot --userspec=[user:group] [path/to/new_root] [command]

Change to a specific directory after chrooting

sudo chroot [path/to/new_root] /bin/sh -c "cd /home && ls"

说明

chroot changes the root directory of the calling process to the specified path and runs a command within that environment. All pathname lookups starting with / will be relative to the new root. If no command is specified, chroot runs the shell from the SHELL environment variable or defaults to /bin/sh. The new root directory must contain all necessary files, libraries, and device nodes required by the command being executed.

参数

NEWROOT
The directory to use as the new root filesystem
COMMAND
Command to run in the chroot environment
ARG
Arguments to pass to the command
--userspec=_USER:GROUP_
Specify user and group (name or ID) to run as
--groups=_GROUPS_
Specify supplementary groups
--skip-chdir
Do not change working directory to /

FAQ

What is the chroot command used for?

chroot changes the root directory of the calling process to the specified path and runs a command within that environment. All pathname lookups starting with / will be relative to the new root. If no command is specified, chroot runs the shell from the SHELL environment variable or defaults to /bin/sh. The new root directory must contain all necessary files, libraries, and device nodes required by the command being executed.

How do I run a basic chroot example?

Run `sudo chroot [path/to/new_root] [command]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does NEWROOT do in chroot?

The directory to use as the new root filesystem