So I'm looking at:


and trying the following within a bash script:

sudo chroot chroot

mount none -t proc /proc
mount none -t sysfs /sys
mount none -t devpts /dev/pts

Running the script drops to a shell at sudo chroot chroot. When I exit that I get the expected warnings about mount needing root. Is there a way I can keep all this within one shell script?

Edit: I want this to be a repeatable process, which is why I want to script the whole thing rather than type it in time and time again.

  • It's rather confusing for someone seeking information, to use a directory called "chroot". Suggest "chroot-dir" ? – MikeW Feb 21 '19 at 11:42

Create a second script (e.g. chroot.sh) and place it in your chroot/ folder.

Now edit the command in your original script to this:

chroot chroot/ ./chroot.sh

Now the script chroot.sh will be executed inside your chroot.


Hi u should go with simple solution like pipe:

cat << EOF | chroot chroot
rm -rf /

PS. joking about rm -rf ;), anything you run inside EOF - EOF is ran inside your chrooted directory, you can also use sudo if you like

cat << EOF | sudo chroot chroot
ls /
  • Very handy, nice! – Joril Jul 22 '16 at 10:04
  • This is a great solution if you don't want to create more than one script – musicin3d Dec 16 '18 at 18:23

The thing about chroots and /proc, /sys and /dev/pts is that these three filesystems are provided by the kernel, so they remain the same whether you mount within the chroot or from without. Indeed, you'll see, earlier on in the instructions:

sudo mount --bind /dev chroot/dev

/dev is populated by the kernel, but is not a kernel-provided filesystem, so it had to be bind-mounted. Therefore, in practice, you'll see that mounting it using bind mounts (or otherwise) before entering the chroot works just as well (assume sudo):

for i in dev proc sys dev/pts
    mount -o bind /$i chroot/$i
chroot chroot
for i in dev/pts proc sys dev
    umount -chroot/$i
# or
mount -o bind /dev chroot/dev
mount -t sysfs none chroot/sys
mount -t proc none chroot/proc
mount -t devpts none chroot/dev/pts
chroot chroot
for i in dev/pts proc sys dev
    umount -chroot/$i

Relevant reading:


You could create a .bashrc script or something like it, which is appended to the chroot env's /root/.bashrc, which does all the mounting etc. Aftwerwards you restore the backed up .bashrc in /root and exit the chroot:

Main script:

#!/usr/bin/env bash
cp bashrcscript chroot/root/
if [ -a chroot/root/.bashrc ]; then
    cp chroot/root/.bashrc chroot/root/.bashrc.bak
echo "./bashrcscript" >> chroot/root/.bashrc
chroot chroot/
rm chroot/root/.bashrc
rm chroot/root/bashrcscript
if [ -a chroot/root/.bashrc.bak ]; then
    mv chroot/root/.bashrc.bak chroot/root/.bashrc


mount none -t proc /proc
mount none -t sysfs /sys
mount none -t devpts /dev/pts 
# Anything else you like to do

The bashrcscript will then be executed when the root console is started. Ensure it's executable.

You could even put the resolv.conf copying into the main script etc.

  • This looks promising, I've edited the question a little to clarify that I wanted a scripted solution. Let me test this and get report back. – Dr.Avalanche Nov 19 '14 at 9:16

I think it's not saying that you should put those commands into a script, but that you should type them; i.e., type the mount commands into the sudo shell.

  • Yeah, I'm looking to script this so it's a repeatable process, not type things in. – Dr.Avalanche Nov 19 '14 at 9:18

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.