Learnt from this article by Dave McKay .
Create the root directory of the chroot
environment if it doesn’t exist.
1chr=/home/ricky/testroot
2mkdir -p $chr
Create necessary directories and then get into the environment.
1mkdir -p $chr/{bin,lib,lib64}
2cd $chr
Copy the binaries we need for surviving on this “barren land”.
1cp -v /bin/{bash,touch,ls,rm} $chr/bin
For each of these binaries file, discover its library dependencies and then copy them as well into the environment. The following code does this only for bash
. Adapt and run it for touch
, ls
and rm
as well.
1# Example with /bin/bash.
2# Do this for /bin/{touch,ls,rm} as well
3list="$(ldd /bin/bash | egrep -o '/lib/.*\.[0-9]')"
4for i in $list; do cp -v --parents "$i" "$chr"; done
Finally, chroot
into the environment and specify which program to run right after the environment changing. Note that we should specify a program within the environment, as we had already lost access to the outer space at this moment (and that’s why we put a bash
binary inside).
1sudo chroot $chr /bin/bash
Now the prompt is most probably changed, and you should be able to use ls
, touch
, rm
and those bash
built-in commands.