0

Trying to do LFS, but now stalled, because chroot(1) doesn't work properly.

#!/bin/bash
_place="$(pwd)/jail"
# Clear before do something
for _x in dev/shm run sys proc dev/pts dev; do
 sudo umount ${_place}/${_x} >& /dev/null
done
for _x in dev/pts dev run sys proc; do
 sudo rmdir ${_place}/${_x} >& /dev/null
done
sudo rm -rf ${_place}/bin >& /dev/null
sudo rmdir ${_place} >& /dev/null
# Run with "go" to mount actually
if (test "${1}" = "go"); then
 sudo mkdir -p ${_place}/{dev/pts,proc,sys,run} || exit 1
 sudo mount --bind /dev ${_place}/dev || exit 1
 sudo mount -t devpts devpts -o gid=5,mode=0620 ${_place}/dev/pts || exit 1
 sudo mount -t proc proc ${_place}/proc || exit 1
 sudo mount -t sysfs sysfs ${_place}/sys || exit 1
 sudo mount -t tmpfs tmpfs ${_place}/run || exit 1
 if (test -h ${_place}/dev/shm); then
 sudo install -v -d -m 1777 ${_place}$(realpath /dev/shm) || exit 1
 else
 sudo mount -t tmpfs -o nosuid,nodev tmpfs ${_place}/dev/shm || exit 1
 fi
 sudo mkdir -p ${_place}/bin || exit 1
 sudo cp -f /bin/bash ${_place}/bin/ || exit 1
 sudo chroot ${_place}
fi

So I wrote this small script to test if chroot(1) works properly. The goal is to run /bin/bash in a chrooted environment. But it fails with:

chroot: failed to run command ‘/bin/bash’: No such file or directory

May I ask you to answer me what should I do in this script to make this script work properly?

1
  • Does it fail when sudo chroot ${_place} is run? Commented Apr 3, 2025 at 9:25

1 Answer 1

1

Looking at your script; when you chroot you need to copy not just the binary itself but also all its dependencies.

The error message chroot: failed to run command '/bin/bash': No such file or directory happens when the dynamic linker can not resolve the dependencies for bash. The binary exists but it can not be executed because the required shared libraries are not available in the chroot environment.

Try this fixed version:

#!/bin/bash
_place="$(pwd)/jail"
# Clear before do something
for _x in dev/shm run sys proc dev/pts dev; do
 sudo umount ${_place}/${_x} >& /dev/null
done
for _x in dev/pts dev run sys proc; do
 sudo rmdir ${_place}/${_x} >& /dev/null
done
sudo rm -rf ${_place}/bin ${_place}/lib ${_place}/lib64 >& /dev/null
sudo rmdir ${_place} >& /dev/null
# Run with "go" to mount actually
if (test "${1}" = "go"); then
 # Create directory structure
 sudo mkdir -p ${_place}/{bin,lib,lib64,dev/pts,proc,sys,run} || exit 1
 
 # Copy bash and its dependencies
 sudo cp -f /bin/bash ${_place}/bin/ || exit 1
 
 # Get bash dependencies from ldd and copy them
 for lib in $(ldd /bin/bash | grep -v linux-vdso | awk '{print 3ドル}' | grep ^/); do
 sudo mkdir -p ${_place}/$(dirname $lib) || exit 1
 sudo cp -f $lib ${_place}/$lib || exit 1
 done
 
 # Handle dynamically loaded libs, especially libdl (needed by bash)
 sudo cp -f /lib64/ld-linux-* ${_place}/lib64/ 2>/dev/null || sudo cp -f /lib/ld-linux-* ${_place}/lib/ 2>/dev/null
 
 # Do all the mounts from your original script
 sudo mount --bind /dev ${_place}/dev || exit 1
 sudo mount -t devpts devpts -o gid=5,mode=0620 ${_place}/dev/pts || exit 1
 sudo mount -t proc proc ${_place}/proc || exit 1
 sudo mount -t sysfs sysfs ${_place}/sys || exit 1
 sudo mount -t tmpfs tmpfs ${_place}/run || exit 1
 if (test -h ${_place}/dev/shm); then
 sudo install -v -d -m 1777 ${_place}$(realpath /dev/shm) || exit 1
 else
 sudo mount -t tmpfs -o nosuid,nodev tmpfs ${_place}/dev/shm || exit 1
 fi
 # Chroot and specify the command explicitly
 sudo chroot ${_place} /bin/bash
fi

hope this will work for you.

answered Apr 3, 2025 at 11:05
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.