Difference between revisions of "Change root"

From ArchWiki
Jump to: navigation, search
(Exiting chroot: /mnt -> /mnt/arch)
(45 intermediate revisions by 18 users not shown)
Line 1: Line 1:
[[Category:System recovery (English)]]
+
[[Category:System recovery]]
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#Bootloader_installation|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.
+
[[es:Change Root]]
 +
[[fa:تغییر ریشه]]
 +
[[fr:Chroot]]
 +
[[ja:Change Root]]
 +
[[ro:Chroot]]
 +
[[zh-CN:Change Root]]
 +
[[Wikipedia:Chroot|Chroot]] 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, such as reinstalling the bootloader or resetting a forgotten password.
  
 
== Requirements ==
 
== Requirements ==
  
* 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 [http://www.archlinux.org/download/ LiveCD and USB Flash disk].
+
* You'll need to boot from another working Linux environment (e.g. from a LiveCD or USB flash media, or from another installed Linux distribution).
  
 
* Root privileges are required in order to chroot.
 
* 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:
+
* Be sure that the architecture 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 with:
  
uname -m
+
: {{bc|# 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 (<code>swapon /dev/<device-or-partition-name></code>) and to connect to your network before chrooting.
+
* If you need any kernel modules loaded in the chroot environment, load them before chrooting. It may also be useful to initialize your swap ({{ic|swapon /dev/sdxY}}) and to establish an internet connection before chrooting.
  
== Mounting the device ==
+
== Mount the partitions ==
  
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:
+
The root partition of the Linux system that you're trying to chroot into needs to be mounted first. To find out the device name assigned by the kernel, run:
  
  fdisk -l
+
  # lsblk /dev/sda
  
Create a directory where you would like to mount the device or partition, then mount it:
+
You can also run the following to get an idea of your partition layout.
  
  mkdir /mnt/arch
+
  # fdisk -l
mount /dev/<device-or-partition-name> /mnt/arch
+
  
== Changing Root ==
+
Now create a directory where you would like to mount the root partition and mount it:
 +
 
 +
# mkdir /mnt/arch
 +
# mount /dev/sda3 /mnt/arch
 +
 
 +
Next, if you have separate partitions for other parts of your system (e.g. {{ic|/boot}}, {{ic|/home}}, {{ic|/var}}, etc), you should mount them, as well:
 +
 
 +
# mount /dev/sda1 /mnt/arch/boot/
 +
# mount /dev/sdb5 /mnt/arch/home/
 +
# mount ...
 +
 
 +
While it's possible to mount filesystems after you've chrooted, it is more convenient to do so beforehand. The reasoning for this is that you'll have to unmount the temporary filesystems after you exit the chroot, so this lets you umount all the filesystems with a single command. This also allows for a safer shutdown. Because the external Linux environment knows all mounted partitions, it can safely unmount them during shutdown.
 +
 
 +
== Change root ==
  
 
Mount the temporary filesystems:
 
Mount the temporary filesystems:
  
  cd /
+
{{Note|Using a newer (2012) Arch release, the following {{ic|mount}} commands can be replaced with {{ic|arch-chroot /mnt/arch}}, if the root partition was mounted in that location. Of course, you may still type these, if you want, or if you only have some other "live" Linux distribution.}}
  mount -t proc proc /mnt/arch/proc
+
 
  mount -t sysfs sys /mnt/arch/sys
+
  # cd /mnt/arch
  mount -o bind /dev /mnt/arch/dev
+
  # mount -t proc proc proc/
 +
  # mount -t sysfs sys sys/
 +
  # mount -o bind /dev dev/
 +
# mount -t devpts pts dev/pts/
  
(If you encounter other chroot instructions, you may see small variations on this: sometimes {{Filename|/mtn/arch/proc}} and {{Filename|/mnt/arch/sys}} are mounted with <code>-t proc none</code> instead of <code>-t proc proc</code>; sometimes they're also mounted using <code>-o bind</code>, like {{Filename|/mnt/arch/dev}}. The differences between these methods don't matter much.)
+
If you established an internet connection and want to use it in the chroot environment, you may have to copy over your DNS servers so that you will be connected to the network:
  
Mount other parts of your filesystem (e.g. {{Filename|/boot}}, {{Filename|/var}}, {{Filename|/usr}}...) that reside on separate partitions but which you need access to. For example:
+
# cp -L /etc/resolv.conf etc/resolv.conf
  
mount /dev/<device-or-partition-name> /mnt/arch/boot
+
Now chroot into your installed system and define your shell:
  
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.
+
  # chroot . /bin/bash
  
If you've setup your network and want to use it in the chroot environment, copy over {{Filename|/etc/resolv.conf}} so that you'll be able to resolve domain names:
+
{{Note|If you see the error {{ic|chroot: cannot run command '/bin/bash': Exec format error}}, it is likely that the two architectures do not match.}}
  
cp -L /etc/resolv.conf /mnt/arch/etc/resolv.conf
+
Optionally, to source your Bash configuration ({{ic|~/.bashrc}} and {{ic|/etc/bash.bashrc}}), run:
  
Now chroot to your installed device or partition and define your shell:
+
# source ~/.bashrc
 +
# source /etc/profile
  
chroot /mnt/arch /bin/bash
+
Optionally, create a unique prompt to be able to differentiate your chroot environment:
  
If you see the error, "<tt>chroot: cannot run command '/bin/bash': Exec format error</tt>" it is likely the two architectures do not match.
+
# export PS1="(chroot) $PS1"
  
If you'll be doing anything with [[GRUB]] inside the chroot environment, you'll need to be sure your {{Filename|/etc/mtab}} is up-to-date:
+
== Run graphical chrooted applications ==
  
grep -v rootfs /proc/mounts > /etc/mtab
+
If you have [[X]] running on your system, you can start graphical applications from the chroot environment.
  
If you use bash and your {{Filename|/home}} is available in the chroot environment, your {{Filename|$HOME/.bashrc}} will be sourced on login. If not, you may want to source the system configuration:
+
To allow the connection to your X server, you have to run the following from a terminal:
  
  source /etc/profile
+
# xhost +
  
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:
+
Then, to direct the applications to your X server, run:
  
  export PS1="(chroot) $PS1"
+
# export DISPLAY=:0.0
  
== Perform System Maintenance ==
+
== Perform system maintenance ==
  
At this point, you can perform whatever system maintenance you need to inside the chroot environment:
+
At this point you can perform whatever system maintenance you require inside the chroot environment. A few common examples are:
  
* upgrade or [[Downgrading_Packages|downgrade]] packages
+
* Reinstall the bootloader.
* [[Mkinitcpio|rebuild your initcpio image]]
+
* Rebuild your [[mkinitcpio|initramfs]] image.
* reset a [[Password_Recovery|forgotten password]]
+
* Upgrade or [[Downgrading_Packages|downgrade]] packages.
* fix your [[Fstab|/etc/fstab]]
+
* Reset a [[Password_Recovery|forgotten password]].
* [[GRUB#Bootloader_installation|reinstall GRUB]] to your disk's [[Master_Boot_Record|MBR]]
+
  
== Exiting chroot ==
+
== Exit the chroot environment ==
  
When you're finished with system maintenance, exit the chroot shell:
+
When you're finished with system maintenance, exit the chroot:
  
  exit
+
  # exit
  
 
Then unmount the temporary filesystems and any mounted devices:
 
Then unmount the temporary filesystems and any mounted devices:
  
  umount /mnt/arch/{proc,sys,dev,boot...}
+
  # umount {proc,sys,dev,boot,[...],}
  
Finally attempt to unmount your hard drive:
+
Finally, attempt to unmount your root partition:
  
  umount /mnt/arch
+
  # cd ..
 +
# umount arch/
  
If you get an error saying that {{Filename|/mnt}} (or any other partition) is busy, this can mean one of two things:
+
{{Note|If you get an error saying that {{ic|/mnt}} (or any other partition) is busy, this can mean one of two things:
  
 
* A program was left running inside of the chroot.
 
* A program was left running inside of the chroot.
* Or more frequently: a mount point still exists on this mount. For example, {{Filename|/mnt/arch/usr}} is still mounted when trying to unmount {{Filename|/mnt/arch}}.
 
  
In the latter case, unmount the offending mount point first. To get a reminder of all the current mount points, run {{Codeline|mount}} with no parameters. If you still are unable to mount a partition, use the force option:
+
* Or, more frequently, a sub-mount still exists (e.g. {{ic|/mnt/arch/boot}} within {{ic|/mnt/arch}}). Check with {{ic|lsblk}} to see if there are any mountpoints left:
  
umount -f /mnt
+
: {{bc|lsblk /dev/sda}}
  
After this you will be able to safely reboot.
+
: If you are still unable to unmount a partition, use the {{ic|--force}} option:
  
== Resources ==
+
: {{bc|# umount -f /mnt}}}}
  
* [http://en.wikipedia.org/wiki/Chroot Wikipedia] - for the introduction.
+
After this, you will be able to safely reboot.

Revision as of 22:46, 22 December 2012

Chroot 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, such as reinstalling the bootloader or resetting a forgotten password.

Requirements

  • You'll need to boot from another working Linux environment (e.g. from a LiveCD or USB flash media, or from another installed Linux distribution).
  • Root privileges are required in order to chroot.
  • Be sure that the architecture 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 with:
# uname -m
  • If you need any kernel modules loaded in the chroot environment, load them before chrooting. It may also be useful to initialize your swap (swapon /dev/sdxY) and to establish an internet connection before chrooting.

Mount the partitions

The root partition of the Linux system that you're trying to chroot into needs to be mounted first. To find out the device name assigned by the kernel, run:

# lsblk /dev/sda

You can also run the following to get an idea of your partition layout.

# fdisk -l

Now create a directory where you would like to mount the root partition and mount it:

# mkdir /mnt/arch
# mount /dev/sda3 /mnt/arch

Next, if you have separate partitions for other parts of your system (e.g. /boot, /home, /var, etc), you should mount them, as well:

# mount /dev/sda1 /mnt/arch/boot/
# mount /dev/sdb5 /mnt/arch/home/
# mount ...

While it's possible to mount filesystems after you've chrooted, it is more convenient to do so beforehand. The reasoning for this is that you'll have to unmount the temporary filesystems after you exit the chroot, so this lets you umount all the filesystems with a single command. This also allows for a safer shutdown. Because the external Linux environment knows all mounted partitions, it can safely unmount them during shutdown.

Change root

Mount the temporary filesystems:

Note: Using a newer (2012) Arch release, the following mount commands can be replaced with arch-chroot /mnt/arch, if the root partition was mounted in that location. Of course, you may still type these, if you want, or if you only have some other "live" Linux distribution.
# cd /mnt/arch
# mount -t proc proc proc/
# mount -t sysfs sys sys/
# mount -o bind /dev dev/
# mount -t devpts pts dev/pts/

If you established an internet connection and want to use it in the chroot environment, you may have to copy over your DNS servers so that you will be connected to the network:

# cp -L /etc/resolv.conf etc/resolv.conf

Now chroot into your installed system and define your shell:

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

Optionally, to source your Bash configuration (~/.bashrc and /etc/bash.bashrc), run:

# source ~/.bashrc
# source /etc/profile

Optionally, create a unique prompt to be able to differentiate your chroot environment:

# export PS1="(chroot) $PS1"

Run graphical chrooted applications

If you have X running on your system, you can start graphical applications from the chroot environment.

To allow the connection to your X server, you have to run the following from a terminal:

# xhost +

Then, to direct the applications to your X server, run:

# export DISPLAY=:0.0

Perform system maintenance

At this point you can perform whatever system maintenance you require inside the chroot environment. A few common examples are:

Exit the chroot environment

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

# exit

Then unmount the temporary filesystems and any mounted devices:

# umount {proc,sys,dev,boot,[...],}

Finally, attempt to unmount your root partition:

# cd ..
# umount arch/
Note: If you get an error saying that /mnt (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 sub-mount still exists (e.g. /mnt/arch/boot within /mnt/arch). Check with lsblk to see if there are any mountpoints left:
lsblk /dev/sda
If you are still unable to unmount a partition, use the --force option:
# umount -f /mnt

After this, you will be able to safely reboot.