Difference between revisions of "Change root"

From ArchWiki
Jump to: navigation, search
(bash config files and prompts (Q: is it ~/.bashrc that will be loaded or ~/.bash_profile?))
(Perform System Maintenance section)
Line 63: Line 63:
   export PS1="(chroot) $PS1"
   export PS1="(chroot) $PS1"
== Perform System Maintenance ==
At this point, you can perform whatever system maintenance you need to inside the chroot environment:
* upgrade or [[Downgrading_Packages|downgrade] packages
* [[Mkinitcpio|rebuild your initcpio image]]
* reset a [[Password_Recovery|forgotten password]]
* fix your [[Fstab|/etc/fstab]]
* [[GRUB#Bootloader_installation]]reinstall GRUB]] to your disk's [[Master_Boot_Record|MBR]]
== Exiting chroot ==
== Exiting chroot ==
First the chroot shell must be exited:
When you're finished with system maintenance, exit the chroot shell:

Revision as of 18:34, 31 March 2010

Changing Root is the process of changing of the apparent disk root directory (and the current running process and its children) to another root directory. When you change root to another directory you cannot access files and commands outside that directory. This directory is called a chroot jail. Changing root is commonly done for system maintenance for such tasks as reinstalling GRUB or resetting a forgotten password. Changing root is often done from from a LiveCD or LiveUSB into a mounted partition that contains an installed system.


  • You'll need to boot to another working Linux environment (for example, to a LiveCD or USB flash disk). For Arch install mediums there is both the LiveCD and USB Flash disk.
  • Root privileges are required in order to chroot.
  • Be sure that the architectures of the Linux environment you have booted into matches the architecture of the root directory you wish to enter (i.e. i686, x86_64). You can find the architecture of your current environment by:
uname -m
  • If you'll need any kernel modules loaded in the chroot environment, load them before chrooting. It may also be useful to initialize your swap (swapon /dev/<device-or-partition-name>) and to connect to your network before chrooting.

Mounting the device

The device or partition with the Linux system on it will need to be mounted. To discover the kernel name of the storage device name, type:

fdisk -l

Create a directory where you would like to mount the device or partition, then mount it:

mkdir /mnt/arch
mount /dev/<device-or-partition-name> /mnt/arch

Changing Root

Mount the temporary filesystems:

cd /
mount -t proc proc /mnt/arch/proc
mount -t sysfs sys /mnt/arch/sys
mount -o bind /dev /mnt/arch/dev

(If you encounter other chroot instructions, you may see small variations on this: sometimes Template:Filename and Template:Filename are mounted with -t proc none instead of -t proc proc; sometimes they're also mounted using -o bind, like Template:Filename. The differences between these methods don't matter much.)

Mount other parts of your filesystem (e.g. Template:Filename, Template:Filename, Template:Filename...) that reside on separate partitions but which you need access to. For example:

mount /dev/<device-or-partition-name> /mnt/arch/boot

It's possible to mount filesystems after you've chrooted, but it's more convenient to do so beforehand. The reason is that when you exit a chroot, the external Linux environment will then know about all the mounted filesystems. This lets you umount all the filesystems in a single command; and if you forget to umount the system will know how to do it properly when shutting down, without corrupting the filesystems.

If you've setup your network and want to use it in the chroot environment, copy over Template:Filename so that you'll be able to resolve domain names:

cp -L /etc/resolv.conf /mnt/arch/etc/resolv.conf

Now chroot to your installed device or partition and define your shell:

chroot /mnt/arch /bin/bash

If you see the error, "chroot: cannot run command '/bin/bash': Exec format error" it is likely the two architectures do not match.

If you'll be doing anything with GRUB inside the chroot environment, you'll need to be sure your Template:Filename is up-to-date:

grep -v rootfs /proc/mounts > /etc/mtab

If you use bash and your Template:Filename is available in the chroot environment, your Template:Filename will be sourced on login. If not, you may want to source the system configuration:

 source /etc/profile

If either of those specifies a custom prompt, that may help you see when you're in the chroot environment, and when you're in the external Linux environment. Or you can manually change the prompt as a reminder:

 export PS1="(chroot) $PS1"

Perform System Maintenance

At this point, you can perform whatever system maintenance you need to inside the chroot environment:

Exiting chroot

When you're finished with system maintenance, exit the chroot shell:


Then unmount the temporary filesystems and any mounted devices:

umount /mnt/arch/{proc,sys,dev,boot...}

Finally attempt to unmount your hard drive:

umount /mnt/arch

If you get an error saying that Template:Filename (or any other partition) is busy, this can mean one of two things:

  • A program was left running inside of the chroot.
  • Or more frequently: a mount point still exists on this mount. For example, Template:Filename is still mounted when trying to unmount Template:Filename.

In the latter case, unmount the offending mount point first. To get a reminder of all the current mount points, run Template:Codeline with no parameters. If you still are unable to mount a partition, use the force option:

umount -f /mnt

After this you will be able to safely reboot.