#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <enter_name_of_your_chroot_directory>"
else
#remove trail slash
DESTINATION_PATH=$PWD/${1%/}
mkdir -p $DESTINATION_PATH
if [ ! -d "$DESTINATION_PATH" ]; then
echo "Invalid destination path ${DESTINATION_PATH} it does not exists"
exit
fi
if [ ! -d "${DESTINATION_PATH}/dev" ]; then
echo "Create dir path ${DESTINATION_PATH}/dev"
mkdir -p ${DESTINATION_PATH}"/"dev
fi
if ! grep -qs ${DESTINATION_PATH}"/"dev /proc/mounts; then
mount --bind /dev ${DESTINATION_PATH}"/"dev
if [ $? -eq 0 ]; then
echo "Mount success ${DESTINATION_PATH}"/"dev"
else
echo "Something went wrong with the mount ${DESTINATION_PATH}"/"dev"
fi
fi
if [ ! -d "${DESTINATION_PATH}/proc" ]; then
echo "Create dir path ${DESTINATION_PATH}/proc"
mkdir -p ${DESTINATION_PATH}"/"proc
fi
if ! grep -qs ${DESTINATION_PATH}"/"proc /proc/mounts; then
mount --bind /proc ${DESTINATION_PATH}"/"proc
if [ $? -eq 0 ]; then
echo "Mount success ${DESTINATION_PATH}"/"proc"
else
echo "Something went wrong with the mount ${DESTINATION_PATH}"/"proc"
fi
fi
if [ ! -d "${DESTINATION_PATH}/sys" ]; then
echo "Create dir path ${DESTINATION_PATH}/sys"
mkdir -p ${DESTINATION_PATH}"/"sys
fi
if ! grep -qs ${DESTINATION_PATH}"/"sys /proc/mounts; then
mount --bind /sys ${DESTINATION_PATH}"/"sys
if [ $? -eq 0 ]; then
echo "Mount success ${DESTINATION_PATH}"/"sys"
else
echo "Something went wrong with the mount ${DESTINATION_PATH}"/"sys"
fi
fi
if [ ! -d "${DESTINATION_PATH}/dev/pts" ]; then
echo "Create dir path ${DESTINATION_PATH}/dev/pts"
mkdir -p ${DESTINATION_PATH}"/"dev/pts
fi
if ! grep -qs ${DESTINATION_PATH}"/"dev/pts /proc/mounts; then
mount --bind /dev/pts ${DESTINATION_PATH}"/"dev/pts
if [ $? -eq 0 ]; then
echo "Mount success ${DESTINATION_PATH}"/"dev/pts"
else
echo "Something went wrong with the mount ${DESTINATION_PATH}"/"dev/pts"
fi
fi
if [ ! -d "${DESTINATION_PATH}/etc" ]; then
echo "Create dir path ${DESTINATION_PATH}/etc"
mkdir -p ${DESTINATION_PATH}"/"etc
cp /etc/resolv.conf ${DESTINATION_PATH}"/"etc/resolv.conf
fi
for i in $( ldd /bin/bash | grep -v dynamic | cut -d " " -f 3 | sed 's/://' | sort | uniq )
do
cp --parents $i ${DESTINATION_PATH}
done
# ARCH amd64
if [ -f /lib64/ld-linux-x86-64.so.2 ]; then
cp --parents /lib64/ld-linux-x86-64.so.2 /${DESTINATION_PATH}
fi
# ARCH i386
if [ -f /lib/ld-linux.so.2 ]; then
cp --parents /lib/ld-linux.so.2 /${DESTINATION_PATH}
fi
echo "Chroot jail is ready: ${DESTINATION_PATH}"
if [ ! -d "${DESTINATION_PATH}/bin" ]; then
echo "Create dir path ${DESTINATION_PATH}/bin"
mkdir -p ${DESTINATION_PATH}"/"bin
cp /bin/{cat,echo,rm,bash,sh,ls,mkdir} ${DESTINATION_PATH}"/bin/"
fi
fakechroot fakeroot chroot ${DESTINATION_PATH}
fi
要更改工作目录,请尝试使用以下命令
gnome-terminal --working-directory=/path/to/dir
您可以使用命令检查更改是否已生效
pwd
。或者,打开“~/.bashrc”,滚动到底部并添加更改目录命令 -
cd ~/mychroot
最接近我的问题的解决方案是proot但它没有按预期工作。例如当我运行它时
当我切换到父目录时
并运行
它真的改变到父目录,它必须限制在
~/mychroot
目录中无论如何,我在其中一个论坛中找到了一个脚本并根据我的需要对其进行了修改。在原始脚本中它不起作用,
/usr/sbin/chroot
我将其替换为fakechroot fakeroot chroot
并添加了一些新行。