我怎样才能将终端限制在 chroot 这样的目录中?

我怎样才能将终端限制在 chroot 这样的目录中?

我想暂时将某个目录设置为顶级目录。假设我打开终端,/home/me/mychroot我该如何设置根目录目录是否作为该终端的顶级目录?

答案1

要更改工作目录,请尝试使用以下命令

gnome-terminal --working-directory=/path/to/dir

您可以使用命令检查更改是否已发生pwd

或者,打开“~/.bashrc”,滚动到底部并添加更改目录命令 -

cd ~/mychroot

答案2

我的问题最接近的解决方案是普罗特但它没有按预期工作。例如当我像这样运行它时

proot -w  ~/mychroot

当我改变到父目录时

cd .. 

然后运行

ls

它确实改变了父目录,它必须被限制在~/mychroot目录中

无论如何,我在一个论坛上找到一个脚本,并根据我的需要对其进行了修改。原始脚本无法正常工作,/usr/sbin/chroot我用它替换了它fakechroot fakeroot chroot并添加了几行新代码。

#!/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

相关内容