Problem

Steps to build a chroot environment:

  1. Make a directory /tmp/newroot as the new root:

    mkdir -p /tmp/newroot
    
  2. Compile a helloworld program in the new root:

    // helloworld.c
    #include <stdio.h>
    int main()
    {
        printf("hello, world\n");
        return 0;
    }
    
    # gcc -o helloworld helloworld.c
    # mv helloworld /tmp/newroot/helloworld
    
  3. Run helloworld in the new root:

    # chroot /tmp/newroot /helloworld
    

And you will see an error:

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

Solution

Run ldd /tmp/newroot/helloworld and you will soon understand why: The dynamic linker/loader ld-linux.so* is missing.

One way to fix this is to compile the program statically:

    # gcc -static -o helloworld-static helloworld.c
    # mv helloworld-static /tmp/newroot/helloworld-static

This time it works:

    # chroot /tmp/newroot /helloworld-static
    hello, world

References