When I run the
chroot command an error is given:
failed to run command ‘/bin/bash’: No such file or directory
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
This error means that there is no
/bin/bash directory inside chroot. Make sure you point it to where
bash (or other shell's) executable is in
If you have
/mnt/somedir/usr/bin/bash then execute
chroot /mnt/somedir /usr/bin/bash.
Apart from the above, you also need to add
libc directory dependencies, as mentioned in the answer here.
/bin/bash inside chrooted directory, but I didn't have /lib and /lib64 inside it. The message from chroot could be more descriptive. "no such file or directory" really means "I can't run this...".
/bin/bash depends of course on libc, ld-linux, libdl etc., you can use
ldd /bin/bash to see which libraries it requires.
1) You can
mount -o bind these directories under chroot
2) Or you can copy these libraries to chroot, if you don't trust the chrooted env to not corrupt them, like so:
cp -a /usr rootfs/ cp -a /lib rootfs/ cp -a /lib64 rootfs/
chroot tries to start the shell that is set in your
$SHELL environment variable by default, but it looks for it in your new root dir, which seems not to contain
/bin/bash, so it cannot start.
You can tell chroot to start another program inside the new root by simply adding it as a parameter:
chroot /your/new/root /bin/foo --options...
Note that the path of the command is interpreted inside your new root, so in this example the called program is in fact in
I was getting the same error when trying to ssh to a chrooted account on a remote server. In my case, I was missing the following file in the remote lib64 directory. Server is Centos6.9
It was fixed by executing the following:
cp /lib64/ld-linux-x86-64.so.2 /secure/jail/lib64/
In case you are doing a cross compilation you need to use qemu simulator which can run /mnt/somedir/bin/bash. Below are the steps for armhf cross compilation. Steps for other architectures should be similar.
sudo apt-get install qemu-user-static
sudo cp /usr/bin/qemu-arm-static /mnt/usr/bin/
Once you copy the qemu-arm-static into the /mnt/usr/bin you will be able to do chroot.
Check this out for more details: https://blog.lazy-evaluation.net/posts/linux/debian-armhf-bootstrap.html
Something no one has mentioned yet, if the goal is not to keep copies of libraries you locate with ldd. When you build busybox it respects
LDFLAGS=--static per their FAQ. This will build all necessary libraries into your binaries. This does increase the size of the binaries, but... you'd need most of this disk space to store what you're locating with ldd anyway.
Note that you may still need to copy your c library (libc.so.6), core math library (libm.so.6), namespace resolution library (libresolv.so.2), and kernel library (in my case, since I am using a raspberry pi, ld-linux-armhf.so.3) . You can use the ldd tool as directed in other answers on your static busybox binary to discover whether this is the case.
These may in turn depend on other libraries. To discover whether this is the case you can use the file tool. I am using the full path to raspberry pi's libm.so.6 as an example :
In my case, since ARM processors need many libraries, I copied my entire arm-linux-gnueabihf folder into my lib folder, allowing me to access my chroot.
ldd /bin/bashto see dependences. Wiil reply like follows:
linux-vdso.so.1 (0x00007ffd057eb000) libtinfo.so.6 => /lib/x86_64-linux-gnu/libtinfo.so.6 (0x00007f83183bc000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f83183b6000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f83181c4000) /lib64/ld-linux-x86-64.so.2 (0x00007f8318536000)
Then for each run
diff /lib/x86_64-linux-gnu/libtinfo.so.6 /mnt/lib/x86_64-linux-gnu/libtinfo.so.6 etc. (Change
/mnt to your mount path.)
Also run diff for the bash, like
diff /usr/bin/bash /mnt/usr/bin/bash.
linux-vdso.so.1 because it inserted into kernel.
If will found differences then make replace these by files from Live CD.
Also check that the symlink links
/lib64 lead to the correct directories (to