2

I want to set a particular directory as top directory temporarily. Let's say I open terminal in /home/me/mychroot how can I set mychroot directory as top directory for that terminal?

0

To change your working directory, try using the following command

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

You can check whether the changes have taken plac using the pwd command.

Alternatively, open "~/.bashrc", scroll to the bottom and add a change directory command -

cd ~/mychroot

| improve this answer | |
  • Thank you for your answer but it doesn't work. When I set it like gnome-terminal --working-directory=/home/me/mychroot it opens another window in /home/me – kenn Nov 20 '14 at 17:49
  • it doesn't set mychroot as root directory, it sets working directory. I need something like chroot does – kenn Nov 20 '14 at 17:52
0

Closest solution to my question is proot but it doesn't work as intended. For example when I run it like

proot -w  ~/mychroot

and when I change to parent directory

cd .. 

and run

ls

it really changes to parent directory, it must have confined in ~/mychroot directory

Anyway I found a script in one of the forums and modified it to my needs. In original script it doesn't work with /usr/sbin/chroot I replaced it with fakechroot fakeroot chroot and added a few new lines.

#!/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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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