chroot: failed to run command `/bin/bash': No such file or directory

时间:2023-03-08 18:00:53

1 使用chroot命令时报错如下:

testupgrade:/ # chroot /sb
chroot: cannot change root directory to /sb: No such file or directory

2 通过下面命令可以看到,当前连接的shell环境是bash,所有系统默认会在chroot的时候切换shell

testupgrade:/ # echo $SHELL
/bin/bash

3 解决办法:当然是把bash拷贝到chroot的路径下了

3.1 首先要把bash拷贝到/sb路径下

testupgrade:/sb/bin # cp /bin/bash /sb/bin/bash

然而这个时候 运行chroot /sb 还是错误的,这是因为bash命令有其他包的依赖,必须把包拷贝过来才行啊

3.1 把bash的依赖包拷贝过来

# 查找bash命令 依赖
testupgrade:/sb/bin # ldd bash
    linux-vdso.so. =>  (0x00007fff9c7ff000)
    libreadline.so. => /lib64/libreadline.so. (0x00007f31ed16d000)
    libdl.so. => /lib64/libdl.so. (0x00007f31ecf69000)
    libc.so. => /lib64/libc.so. (0x00007f31ecbf1000)
    libncurses.so. => /lib64/libncurses.so. (0x00007f31ec9a9000)
    /lib64/.so. (0x00007f31ed3e8000)

# 复制依赖到chroot后的路径,注意新包在chroot中的位置要和原来的位置一样
#例如原来在/lib/foo.so ,那么chroot后的路径就行/sb/lib/foo.so
testupgrade:/sb #  /sb/lib64/
testupgrade:/sb #  /sb/lib64/
testupgrade:/sb #  /sb/lib64/
testupgrade:/sb #  /sb/lib64/
testupgrade:/sb # .so. /sb/lib64/

4 重新运行chroot,一切ok了

testupgrade:/sb/bin # chroot /sb
bash-3.2# ifconfig
bash: ifconfig: command not found
#因为/sb下面没有ifconfig命令文件啊,所以肯定报错找不到呢