Page 1 of 1

Root shell helper script

Posted: Wed Apr 09, 2025 7:07 am
by Hypex
Hi guys. A common method to fix a broken system is to mount it and chroot a shell into it. This can work well but to do anything more than basic file editing like accessing network and installing software requires more work for it to be functional. Namely some common system mounts need to be active. So I wrote this script to chroot into a mounted volume. It mounts proc,sys, dev and pts then does a chroot into it. When shell is exited it unmounts and returns back to base. It relies on sudo so is Ubuntu centric. I called it rootsh and copied it to my /usr/sbin. :-)

Code: Select all

#/bin/sh

if [ -z "$1" ]; then
	echo "$0 <mount>"
	echo
	echo "Root shell:"
	echo "The rootsh command will bind local proc,sys and dev mounts onto a"
	echo "mounted system and chroot to give root shell."
	
	exit 0
elif ! mountpoint -q "$1" ; then
	echo "Mount point at $1 does not exist"
	
	exit 5
fi

sudo mount -t proc proc "$1/proc"
sudo mount -t sysfs sysfs "$1/sys"
sudo mount -o bind /dev "$1/dev"
sudo mount -t devpts pts "$1/dev/pts"

sudo chroot "$1"

sudo umount "$1/dev/pts"
sudo umount "$1/dev"
sudo umount "$1/sys"
sudo umount "$1/proc"

exit 0

Re: Root shell helper script

Posted: Wed Apr 09, 2025 8:45 am
by sailorMH
@Hypex
nice!

Re: Root shell helper script

Posted: Fri Apr 11, 2025 5:24 pm
by Hypex
Thanks! It's based on research for what needs to be mounted and how. But can have its quirks as binding mount points can affect the host system like not unmounting cleanly.