Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to be able to provide the /bin and /lib directories inside a chroot jail so that programs can dynamically link properly.

Is there a way to accomplish this without making a copy of the /bin and /lib dirs to the chroot jail?

I've tried symlinks, and they don't work from inside chroot jails, and directories can not be hardlinked.

share|improve this question
up vote 12 down vote accepted

You could use mount to remount the directories you need in your jail:

# mount --bind /bin /chroot/bin
# mount --bind /lib /chroot/lib
# chroot /chroot

For use in /etc/fstab:

/bin /chroot/bin none bind
/lib /chroot/lib none bind


share|improve this answer
Thanks a lot. Linux just keeps getting better and better the more I play with it :p – anonymous coward Dec 17 '10 at 2:17
I'm glad it worked out well! Cheers! – jgr Dec 17 '10 at 11:00

If you didn't want to mount the directories as jgr said, you can use cp to recursivly copy directories and create hardlinks for all files:

cp -alf /bin /chroot/bin
cp -alf /lib /chroot/lib
chroot /chroot

This way your chroot's /bin and /lib can have slightly different structure / contents than the main directories.

share|improve this answer
Good idea, but if the /chroot is on a different device this won't work. You can't hardlink across devices. – AllenKll May 6 at 18:23

copy_file_and_dependencies() {
    DEPENDENCIES="$(ldd "$PROGRAM" | awk '{ print $3 }' | grep -v '(' | grep -v 'not a dynamic executable')"

    mkdir -p "${JAIL}$(dirname $PROGRAM)"
    cp -Lv "$PROGRAM" "${JAIL}${PROGRAM}"

    for f in $DEPENDENCIES; do
        mkdir -p "${JAIL}$(dirname $f)"
        cp -Lv "$f" "${JAIL}${f}"

export -f copy_file_and_dependencies

copy_file_and_dependencies /etc/ld.so.cache
copy_file_and_dependencies /bin/sh
# ...
share|improve this answer
The intent of the question seems to be to provide access to the existing files rather than copying them into the chroot. – psusi May 18 '15 at 2:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.