How to Setup a Chroot

This guide will help you make a chroot of your Ubuntu install from a Live USB.
You will be able to access your Ubuntu install and use it, such as using apt-get and other stuff as if you had booted it.

Making an Ubuntu Live USB

Download Ubuntu, a Linux based OS.

If you have a 64bit Ubuntu install, you'll want to use this link http://releases.ubuntu.com/16.04.4/ubuntu-16.04.4-desktop-amd64.iso

If you have a 32bit Ubuntu install, you'll want to use this link http://releases.ubuntu.com/16.04.4/ubuntu-16.04.4-desktop-i386.iso

You need to get the same version as your Ubuntu install in order to chroot.

After downloading the file, head on over to https://www.linuxliveusb.com/en/download and install Linux Live USB Creator. Follow the instructions on how to make a Live USB.

If you're using mac osx click on https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-macos#0

Starting the Live USB

Plug the Live USB into the PC and then turn it on.
If you need to use the chroot to reinstall grub or any UEFI related stuff, then you'll need to start the Live USB in UEFI mode.

If you aren't greeted with an Ubuntu screen showing "Try Ubuntu" or "Install Ubuntu" click on Troubleshooting Starting a Live USB.

Choose "Try Ubuntu" you should get a desktop after a few mins.
If you're stuck at a black screen, click on Black Screen After Starting a Live USB.

Press on Ctrl + Alt + T to open a terminal. You should see a command prompt such as ubuntu@ubuntu:~$

Getting the Partitions

Type sudo parted -ls

You should get a list of the disks and partitions
ubuntu@ubuntu:~$ sudo parted -ls
Model: ATA HGST HTS541010A9 (scsi) <---- Disk name 
Disk /dev/sda: 1000GB <---- Disk name as known to Linux with size
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags: 
Number  Start   End     Size    File system     Name                  Flags
 1      1049kB  316MB   315MB   fat16           EFI Partition         boot, esp
 4      316MB   9979MB  9664MB  linux-swap(v1)
 5      9979MB  16.8GB  6784MB  ext4
 2      16.8GB  896GB   880GB   ntfs            Microsoft basic data  msftdata
 3      896GB   1000GB  104GB   ext4

Model: Seagate BUP Slim BK (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 
Number  Start   End     Size    Type     File system  Flags
 1      1049kB  2000GB  2000GB  primary  ntfs         boot
Get the name of the disk that has Ubuntu and the partition's number under the "Number" column that has the Ubuntu root filesystem. By default it should say "ext4" under the "File system" column.
Sometimes you'll have a separate home partition or boot partition so there might be a few.
In Linux the partitions are referred to as the disk followed by the partition number. For instance /dev/sda2 is partition #2 on the /dev/sda disk.
Type sudo mount <disk name and partition number> /mnt such as sudo mount /dev/sda3 /mnt

Type ls /mnt and verify that you get a directory listing similar to below

ubuntu@ubuntu:~$ ls /mnt
bin    dev   initrd.img      lib64       mnt   root  snap  tmp  vmlinuz
boot   etc   initrd.img.old  lost+found  opt   run   srv   usr  vmlinuz.old
cdrom  home  lib             media       proc  sbin  sys   var
If you get a different output, such as your home partition or boot partition, type sudo umount /mnt and try a different partition.

Mount Binding the Virtual Filesystems

In order to use the chroot almost as if you booted it, you'll need to bind some system directories from the Live USB to the install dir.
These are directories that are full of information related to your PC such as the devices and other stuff which get filled when the PC boots.

Type for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done

Type sudo chroot /mnt to chroot, you should get a prompt similar to root@ubuntu:~#

If you get the error chroot: failed to run command `/bin/bash': Exec format error , it means you used a 64bit Live USB with a 32bit Ubuntu install or vice-versa. You'll have to download the appropriate Live USB iso again.

Type mount -a to mount all the filesystems such as any home, boot, efi, or other partitions.

You now have a working chroot that you can use to troubleshoot your system.
If the Live USB had an internet connection it should also be available in the chroot.

When you're done type exit to exit the chroot then you can reboot.

RELATED TOPICS
Live USB, Ubuntu, Grub, Booting, Partition table, Partitions, Filesystems, Testdisk