How to execute " ls " command in the chroot?

chroot: failed to run command ‘ls’: No such file or directory

To run any command inside the chroot, you need to have this program available in the chroot (since it can not use the program installed in the / of filesystem.

The simplest way is to copy the /usr/bin/ls from to /home/kuba/projects/jcubic/leash/usr/bin/ (you will also need the dependent shared libraries: ldd /usr/bin/ls).


It's possible to run ls in a different root, but the ls command and all the files it depends on must be present inside that root. There's no way to start the ls command, and then inside that process change the root: ls doesn't have this feature.

The whole point of a chroot is to limit the visibility of files to those under a certain directory. When you run chroot ~/projects/jcubic/leash ls, the chroot command changes the root of its own process (changing the root directory only affects the process that does it and any process that it subsequently runs), then attempts to run an executable file called ls in a directory on the search path. Since the root is now ~/projects/jcubic/leash, the executable must be present in a directory such as ~/projects/jcubic/leash/bin.

If you want to have a normal system with the usual commands in the chroot, you need to install one. Tools such as debootstrap (to install a Debian system) or provisioning tools such as Docker (which sets up an environment that's chrooted and is confined in other ways) can help.

If you just want the ls command, you'll still need to copy more than ls. To run ls, you'll need to copy the ls command as well as all the files it depends on. Since ls is a dynamically linked program, you need its dynamic loader as well as all the dynamic libraries it depends on. Run ldd /bin/ls to list the required dynamic libraries. For example:

mkdir -p bin lib/x86_64-linux-gnu
rsync -a /bin/ls bin/
rsync -a /lib/x86_64-linux-gnu/ lib/x86_64-linux-gnu/
chroot . ls

Alternatively, a simpler way to explore is to get a statically-linked binary, for example of BusyBox (available on Debian and derivatives as the busybox-static package).

cp /bin/busybox .
chroot . ./busybox ls

Tags:

Ls

Chroot