Difference between revisions of "Chroot (简体中文)"

From ArchWiki
Jump to navigation Jump to search
m (Add translateme.)
Line 3: Line 3:
 
{{TranslationStatus (简体中文)|Change_Root|2012-11-06|233506}}
 
{{TranslationStatus (简体中文)|Change_Root|2012-11-06|233506}}
 
{{Translateme (简体中文)}}
 
{{Translateme (简体中文)}}
[[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.
+
[[Wikipedia:Chroot|Chroot]] 是将当前磁盘根路径(和当前进程和它们的子进程)更改到另一个根目录。当你更改根路径到另一个目录下时,你不能在那个目录外存取文件和使用命令。这个目录叫作 ''chroot jail''。切换根目录通常为了系统维护,例如重装引导程序或者重置遗忘的密码。
  
== 需求 ==
+
== 必要条件 ==
  
* 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).
+
* 你需要从另一个运行的linux环境启动(例如从liveCD或USB闪存介质,或者从另一个已经安装的linux发行版中)
  
* Root privileges are required in order to chroot.
+
* 为了chroot需要root特权。
  
* 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:
+
* 确定你启动进的linux环境的架构符合你想要更改的根路径的架构(例如,i686,x86_64)。你可以用以下命令得到你的当前环境架构:
  
 
: {{bc|# uname -m}}
 
: {{bc|# 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 ({{ic|swapon /dev/sdxY}}) and to establish an internet connection before chrooting.
+
* 如果你需要任何在chroot环境中使用的内核模块,在chroot之前加载它。初始化你的swap ({{ic|swapon /dev/sdxY}})估计也很有用,并且在chroot之前建立网络连接。
  
 
== 挂载分区 ==
 
== 挂载分区 ==
  
The root partition of the Linux system that you're trying to chroot into needs to be mounted. To find out the device name assigned by the kernel, run:
+
你想尝试chroot进去的linux系统根分区需要被挂载。为了找出内核分配的设备名称,运行:
  
 
  # lsblk /dev/sda
 
  # lsblk /dev/sda
  
Then create a directory where you would like to mount this partition and mount it:
+
然后创建一个你想要挂载分区的目录并且挂载到它:
  
 
  # mkdir /mnt/arch
 
  # mkdir /mnt/arch
 
  # mount /dev/sda3 /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:
+
接着,如果你的系统的其它分区有单独分区(比如说 {{ic|/boot}}{{ic|/home}}{{ic|/var}}等等),你也需要挂载他们:
  
 
  # mount /dev/sda1 /mnt/arch/boot/
 
  # mount /dev/sda1 /mnt/arch/boot/
Line 34: Line 34:
 
  # mount ...
 
  # 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.
+
尽管你chroot之后可以挂载文件系统,之前完事更加方便。原因就是你将不得不在退出chroot之前卸载临时文件系统,而这样做将让你用一个单个命令卸载所有文件系统。这也使得关机更加安全。因为外部linux环境知道所有挂载的分区,它能安全的在关机时卸载他们。
  
 
== Change root ==
 
== Change root ==
  
Mount the temporary filesystems:
+
挂载临时文件系统:
  
{{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.}}
+
{{Note|使用更新的 (2012) Arch 发行版,以下{{ic|mount}}命令可以被{{ic|arch-chroot /mnt/arch}}取代,如果root分区被挂载在那里的话。当然,如果你愿意你还可以键入以下内容,或者如果你仅仅有一些其它“live”LInux发行版。}}
  
 
  # cd /mnt/arch
 
  # cd /mnt/arch
Line 48: Line 48:
 
  # mount -t devpts pts dev/pts/
 
  # 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:
+
如果你已经建立了一个网络连接并且想要在chroot环境中使用它,你可能不得不拷贝你的DNS服务器信息让你能够联网:
  
 
  # cp -L /etc/resolv.conf etc/resolv.conf
 
  # cp -L /etc/resolv.conf etc/resolv.conf
  
Now chroot into your installed system and define your shell:
+
现在chroot到你之前安装的系统中并且指定你的shell
  
 
  # chroot . /bin/bash
 
  # chroot . /bin/bash
  
{{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.}}
+
{{Note|如果你遇到错误 {{ic|chroot: cannot run command '/bin/bash': Exec format error}},很可能两个环境架构不匹配。}}
  
Optionally, to source your Bash configuration ({{ic|~/.bashrc}} and {{ic|/etc/bash.bashrc}}), run:
+
可选地,source你的Bash配置文件({{ic|~/.bashrc}}{{ic|/etc/bash.bashrc}}),运行:
  
 
  # source ~/.bashrc
 
  # source ~/.bashrc
 
  # source /etc/profile
 
  # source /etc/profile
  
Optionally, create a unique prompt to be able to differentiate your chroot environment:
+
可选的,创建一个独特的提示符来区别你的chroot环境:
  
 
  # export PS1="(chroot) $PS1"
 
  # export PS1="(chroot) $PS1"
Line 69: Line 69:
 
== 运行图形化的chrooted应用 ==
 
== 运行图形化的chrooted应用 ==
  
If you have [[X]] running on your system, you can start graphical applications from the chroot environment.
+
如果你在系统上运行了[[X]],你可以在chroot环境启动图形应用。
  
To allow the connection to your X server, you have to run the following from a terminal:
+
为了能连接你的X服务器,你不得不在终端中运行以下命令:
  
 
  # xhost +
 
  # xhost +
  
Then, to direct the applications to your X server, run:
+
然后,将应用指向你的X服务器,运行:
  
 
  # export DISPLAY=:0.0
 
  # export DISPLAY=:0.0
Line 81: Line 81:
 
== 系统维护 ==
 
== 系统维护 ==
  
At this point you can perform whatever system maintenance you require inside the chroot environment. A few common examples are:
+
此时你可以执行任何你需要在chroot环境中执行的系统维护操作。一些常见的例子是:
  
* Reinstall the bootloader.
+
* 重装引导。
* Rebuild your [[mkinitcpio|initramfs]] image.
+
* 重新构建[[mkinitcpio|initramfs]]镜像。
* Upgrade or [[Downgrading_Packages|downgrade]] packages.
+
* 升级或[[Downgrading_Packages_(简体中文)|降级]]软件包。
* Reset a [[Password_Recovery|forgotten password]].
+
* 重置[[Password_Recovery_(简体中文)|遗忘的密码]].
  
 
== 退出chroot环境 ==
 
== 退出chroot环境 ==
  
When you're finished with system maintenance, exit the chroot:
+
当你完成系统维护后,退出chroot:
  
 
  # exit
 
  # exit
  
Then unmount the temporary filesystems and any mounted devices:
+
然后卸载临时文件系统和任何挂载的设备:
  
 
  # umount {proc,sys,dev,boot,[...],}
 
  # umount {proc,sys,dev,boot,[...],}
  
Finally, attempt to unmount your root partition:
+
最后,尝试卸载你的根分区:
  
 
  # cd ..
 
  # cd ..
 
  # umount arch/
 
  # umount arch/
  
{{Note|If you get an error saying that {{ic|/mnt}} (or any other partition) is busy, this can mean one of two things:
+
{{Note|如果你得到错误说{{ic|/mnt}}(或其它任何分区) is busy, 这可能意味着两种情况:
  
* A program was left running inside of the chroot.
+
* chroot环境中残留了一个运行的程序。
  
* 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:
+
* 或者,更常见的是,仍然存在子挂载(例如 {{ic|/mnt/arch/boot}}挂载在{{ic|/mnt/arch}})。通过{{ic|lsblk}}检查是否还存在任何挂载点(你也可以单输入一个mount查看):
  
 
: {{bc|lsblk /dev/sda}}
 
: {{bc|lsblk /dev/sda}}
  
: If you are still unable to unmount a partition, use the {{ic|--force}} option:
+
: 如果你仍然不能卸载分区,使用{{ic|--force}}选项:
  
 
: {{bc|# umount -f /mnt}}}}
 
: {{bc|# umount -f /mnt}}}}
  
After this, you will be able to safely reboot.
+
这之后,你可以安全地的重启了。

Revision as of 08:09, 7 November 2012

翻译状态: 本文是英文页面 Change_Root翻译,最后翻译时间:2012-11-06,点击这里可以查看翻译后英文页面的改动。

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: please use the first argument of the template to provide more detailed indications.

Chroot 是将当前磁盘根路径(和当前进程和它们的子进程)更改到另一个根目录。当你更改根路径到另一个目录下时,你不能在那个目录外存取文件和使用命令。这个目录叫作 chroot jail。切换根目录通常为了系统维护,例如重装引导程序或者重置遗忘的密码。

必要条件

  • 你需要从另一个运行的linux环境启动(例如从liveCD或USB闪存介质,或者从另一个已经安装的linux发行版中)。
  • 为了chroot需要root特权。
  • 确定你启动进的linux环境的架构符合你想要更改的根路径的架构(例如,i686,x86_64)。你可以用以下命令得到你的当前环境架构:
# uname -m
  • 如果你需要任何在chroot环境中使用的内核模块,在chroot之前加载它。初始化你的swap (swapon /dev/sdxY)估计也很有用,并且在chroot之前建立网络连接。

挂载分区

你想尝试chroot进去的linux系统根分区需要被挂载。为了找出内核分配的设备名称,运行:

# lsblk /dev/sda

然后创建一个你想要挂载分区的目录并且挂载到它:

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

接着,如果你的系统的其它分区有单独分区(比如说 /boot/home/var等等),你也需要挂载他们:

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

尽管你chroot之后可以挂载文件系统,之前完事更加方便。原因就是你将不得不在退出chroot之前卸载临时文件系统,而这样做将让你用一个单个命令卸载所有文件系统。这也使得关机更加安全。因为外部linux环境知道所有挂载的分区,它能安全的在关机时卸载他们。

Change root

挂载临时文件系统:

Note: 使用更新的 (2012) Arch 发行版,以下mount命令可以被arch-chroot /mnt/arch取代,如果root分区被挂载在那里的话。当然,如果你愿意你还可以键入以下内容,或者如果你仅仅有一些其它“live”LInux发行版。
# cd /mnt/arch
# mount -t proc proc proc/
# mount -t sysfs sys sys/
# mount -o bind /dev dev/
# mount -t devpts pts dev/pts/

如果你已经建立了一个网络连接并且想要在chroot环境中使用它,你可能不得不拷贝你的DNS服务器信息让你能够联网:

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

现在chroot到你之前安装的系统中并且指定你的shell

# chroot . /bin/bash
Note: 如果你遇到错误 chroot: cannot run command '/bin/bash': Exec format error,很可能两个环境架构不匹配。

可选地,source你的Bash配置文件(~/.bashrc/etc/bash.bashrc),运行:

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

可选的,创建一个独特的提示符来区别你的chroot环境:

# export PS1="(chroot) $PS1"

运行图形化的chrooted应用

如果你在系统上运行了X,你可以在chroot环境启动图形应用。

为了能连接你的X服务器,你不得不在终端中运行以下命令:

# xhost +

然后,将应用指向你的X服务器,运行:

# export DISPLAY=:0.0

系统维护

此时你可以执行任何你需要在chroot环境中执行的系统维护操作。一些常见的例子是:

退出chroot环境

当你完成系统维护后,退出chroot:

# exit

然后卸载临时文件系统和任何挂载的设备:

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

最后,尝试卸载你的根分区:

# cd ..
# umount arch/
Note: 如果你得到错误说/mnt(或其它任何分区) is busy, 这可能意味着两种情况:
  • chroot环境中残留了一个运行的程序。
  • 或者,更常见的是,仍然存在子挂载(例如 /mnt/arch/boot挂载在/mnt/arch)。通过lsblk检查是否还存在任何挂载点(你也可以单输入一个mount查看):
lsblk /dev/sda
如果你仍然不能卸载分区,使用--force选项:
# umount -f /mnt

这之后,你可以安全地的重启了。