我正在尝试了解 chroot。
我正尝试在 chroot 环境中运行“ls”命令。
因此,我将“ls”和“bash”及其依赖项(如 ldd 所示)复制到新根目录中的相应 bin、lib、lib64 目录,然后运行 chroot 命令。但我收到以下错误。
root@vig-debian:/home/vignesh# chroot /home/vignesh/my_chroot/ my_chroot/bin/bash
chroot: failed to run command ‘my_chroot/bin/bash’: No such file or directory
以下是我尝试过的日志。
=========================================================
root@vig-debian:/home/vignesh/my_chroot# mkdir bin
root@vig-debian:/home/vignesh/my_chroot# mkdir lib
root@vig-debian:/home/vignesh/my_chroot# mkdir lib64
root@vig-debian:/home/vignesh/my_choot# cp /bin/ls bin/
root@vig-debian:/home/vignesh/my_choot# cp /bin/bash bin/
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot# ldd bin/ls
linux-vdso.so.1 (0x00007ffd463f2000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007fa1e4bf8000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007fa1e49ef000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa1e4644000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fa1e43d6000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa1e41d2000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa1e4e1d000)
libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007fa1e3fcd000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa1e3db0000)
root@vig-debian:/home/vignesh/my_chroot# ldd bin/bash
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot#
linux-vdso.so.1 (0x00007fff276dd000)
libncurses.so.5 => /lib/x86_64-linux-gnu/libncurses.so.5 (0x00007f5ecbaab000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007f5ecb881000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f5ecb67d000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5ecb2d2000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5ecbcd0000)
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot# cp /lib/x86_64-linux-gnu/libselinux.so.1 /lib/x86_64-linux-gnu/libacl.so.1 /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libpcre.so.3 /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libattr.so.1 /lib/x86_64-linux-gnu/libpthread.so.0 lib/
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot# cp /lib64/ld-linux-x86-64.so.2 lib64/
root@vig-debian:/home/vignesh/my_chroot# cp /lib/x86_64-linux-gnu/libncurses.so.5 /lib/x86_64-linux-gnu/libtinfo.so.5 /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libc.so.6 lib/
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot#
root@vig-debian:/home/vignesh/my_chroot# ls
bin lib lib64
root@vig-debian:/home/vignesh/my_chroot# cd ../
root@vig-debian:/home/vignesh# chroot /home/vignesh/my_chroot/ my_chroot/bin/bash
chroot: failed to run command ‘my_chroot/bin/bash’: No such file or directory
root@vig-debian:/home/vignesh#
然后我使用“debootstap”创建了 debian 映像,然后可以执行“chroot”而不会出现任何错误。
那么,我尝试的一切都是错的吗?
答案1
您应该运行chroot /home/vignesh/my_chroot/ /bin/bash
。第一个参数是 chroot 的位置,第二个参数是命令。命令的路径与您 chroot 的位置相关。