Root shell helper script
Posted: Wed Apr 09, 2025 7:07 am
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