最近项目须要使用chroot环境,不了解chroot的能够自行google补脑,google作了一些了解后,发现通常有三种作法: html
1 使用宿主机系统现有的文件copy一个子系统出来,copy时容易遗漏重要的文件。 shell
2 使用makejail工具,快速生成一个chroot环境,初步了解了一下makejail工具,发现该工具是根据配置文件配置的服务生成一个环境,在软件包中自带了一些服务模版,应该能够生成能运行该服务的最小chroot环境吧。固然也能够利用多个配置文件生成一个复杂的环境。 bootstrap
3 使用debootstrap工具,该工具是用来构建基本的debian系统的。也就是说利用该工具能够构建一个小型系统,那咱们也就能够利用该软件快速的构建一个比较完整的chroot环境了。 ubuntu
通过综合评估后,以为第三种方法最适合我,虽然它构建的chroot环境会比较大,因此下面的实验就以debootstrap为基础来构建chroot环境。 bash
物理服务器:10.1.6.235 Ubuntu 12.10 服务器
虚拟机:10.1.6.28 Ubuntu 12.10 ssh
1 安装debootstrap,它会下载并安装基本的系统组成部分 ide
apt-get -y install debootstrap2 安装schroot,它能够用来管理chroot环境
apt-get -y install schroot3 /etc/default/schroot文件
该文件是用来管理shcroot服务中止后的默认行为的,默认不须要修改。 工具
4 schroot配置文件目录结构: oop
/etc/schroot ├── buildd ├── chroot.d ├── default ├── desktop ├── minimal ├── sbuild ├── schroot.conf └── setup.d/etc/schroot/schroot.conf 这是chroot环境定义文件
5 设置/etc/schroot/schroot.conf配置文件,在文件末尾追加以下:
[test] description=user test chroot type=directory directory=/opt users=test groups=root root-groups=root6 检查配置文件是否有错误
schroot -l
能够看到名为test的chroot环境,说明是正确的。
7 利用debootstrap构建ubuntu12.10的chroot环境
debootstrap --variant=buildd --arch=amd64 quantal /opt/ http://10.1.1.187/ubuntu
能够看到根据配置文件在/opt目录下已经有了一个相似小型系统的目录
8 安装完成后,登入chroot环境
schroot -c test -u root
schroot会自动执行/etc/schroot/setup.d/底下所找到的scripts,当chroot启动时会用档名顺序而且以setup-start选项执行,而离开chroot时会用相反顺序并以setup-stop选项执行。能够在现有的配置文件中增长你想进行的动做,也能够本身创建新的script。
这样chroot环境就利用debootstrap安装完毕了,若是你认为这样就能够利用到生产环境,那就想的太简单了,咱们还须要进行如下升级:
1 修改schroot的配置文件,上面咱们创建了一个名为test的chroot环境,这个环境要使用到生产环境中,还须要添加一些配置参数:
[test] description=user test chroot type=directory directory=/opt users=root,test,guol root-users=root,guol groups=root root-groups=root setup.copyfiles=default/copyfiles setup.fstab=default/fstab setup.nssdatabases=default/nssdatabases shell=/bin/bash preserve-environment=true简单的解释一下各个参数:
[test]:是该chroot环境的名称
description:是对该chroot环境的描述
type:可为plain、directory、file、loopback、block-device、btrfs-snapshot、lvm-snapshot之一。其中plain和directory都是资料夹,差异在于directory 会自动执行预设的setup script和挂载一些目录,而plain什么都不会作。使用file能够直接把archive当成chroot 目标。
directory:当type为plain或directory时,用来指定目标的位置。
file:当type为file时,用来指定目标的位置。
users:原系统中能够透过schroot进入此子系统的使用者。
root-users:原系统中能够透过schroot进入此子系统并取得root权限的使用者。
groups:原系统中能够透过schroot进入此子系统的group。
root-groups:原系统中能够透过schroot进入此子系统并取得root权限的group。
setup.copyfiles:指定每次进入子系统时,原系统中会被复制进子系统的文件。
setup.fstab:指定原系统当中会被挂载进子系统的目录,格式和系统的/etc/fstab同样。 注意在预设的状况下schroot会自动把host的/home给bind 进来,若是对子系统的/home作变动,原系统也会受到影响。若是没有必要,建议注释掉该行。
setup.nssdatabases:指定每次进入子系统时,原系统中会被复制进子系统的资料库,如passwd、shadow、group等等。注意每次进入都会从新复制一次,因此在子系统内对这些资料库作的变动都会被覆写掉。这个很重要,由于咱们但愿在原系统增长用户,而后让该用户登录进chroot环境,因此若是你在chroot中增长一个用户后,再次进入chroot环境时,相关文件会被原系统的覆盖掉。
shell:指定登陆chroot环境后使用的shell。
2 修改登入chroot环境时默认加载的文件:
#/etc/schroot/default/copyfiles /etc/hosts /etc/resolv.conf /etc/network/interfaces /etc/apt/sources.list /etc/hostname /etc/sudoers #/etc/schroot/default/fstab /proc /proc none rw,bind 0 0 /sys /sys none rw,bind 0 0 /dev /dev none rw,bind 0 0 /dev/pts /dev/pts none rw,bind 0 0 /tmp /tmp none rw,bind 0 0 tmpfs /dev/shm tmpfs defaults 0 0 /run /run none rw,bind 0 0 /run/lock /run/lock none rw,bind 0 0 /dev/shm /dev/shm none rw,bind 0 0 /run/shm /run/shm none rw,bind 0 0 #/etc/schroot/default/nssdatabases passwd shadow group gshadow services protocols networks hosts3 修改ssh配置,让普通用户经过ssh能够直接登陆进ssh环境,在openssh 4.9版本后就支持该功能了。在配置文件末尾添加以下:
#/etc/ssh/sshd_config Match User test ChrootDirectory /opt/ ForceCommand internal-sftp AllowTcpForwarding no Match User guol ChrootDirectory /opt ForceCommand internal-sftp AllowTcpForwarding no为啥必定加到ssh配置文件的末尾呢,由于Match配置段的结束是如下一个Match配置开始为止的。Match除了能够匹配user还能够根据不少选项来匹配,具体信息能够man一下。
咱们先测试一下目前改进的配置
1 先测试在本机直接登陆chroot环境。分别用test和guol用户测试
能够看到test用户登陆进入怎么有warning提示,而guol用户登陆却正常。根据提示使用--directory参数。man解释以下:
Change to directory inside the chroot before running the command or login shell. If directory is not available, schroot will exit with an error status. The default behaviour is as follows (all directory paths are inside the chroot). A login shell is run in the current working directory. If this is not available, it will try $HOME (when --preserve-environment is used), then the user's home directory, and / inside the chroot in turn. A command is always run in the current working directory inside the chroot. If none of the directories are available, schroot will exit with an error status.根据解释,在登陆chroot环境的shell时,会给你选择一个登入时的目录,若是没有设置,默认选择当前执行进入chroot环境的目录,若是当前目录不可用,则会寻找你的家目录、而后寻找根目录。看来执行schroot登陆chroot时还和当前上下文有关系。chroot环境重确定没有/etc/schroot目录了,而且咱们在fstab的bind时把/home目录给取消掉了,因此在chroot环境中并无相关用户的家目录。
改进以后以下:
2 测试经过ssh直接登录进chroot环境
这样基本的配置就已经操做完毕,各位能够根据具体需求对chroot环境进行深度定制。
在chroot环境中使用apt-get时,遇到了如下一个错误:
dpkg: unrecoverable fatal error, aborting: syntax error: unknown group 'ssl-cert' in statoverride file E: Sub-process /usr/bin/dpkg returned an error code (2)提示在使用dpkg时系统没有ssl-cert组,可是看了宿主系统的group文件,也没有该组,新键该组后就能够正常使用了。
若是不想使用schroot管理chroot环境,也能够在使用debootstrap安装完小型chroot环境后,执行以下操做:
cp /etc/resolv.conf /opt/etc/resolv.conf
cp /etc/resolv.conf /opt/etc/resolv.conf
cp /etc/hosts /opt/etc/hosts
cp /etc/network/interfaces /opt/etc/network/interfaces
cp /etc/apt/sources.list /opt/etc/apt/sources.list
cp /etc/hostname /opt/etc/hostname
cp /etc/sudoers /opt/etc/sudoers
cp /etc/passwd /opt/etc/passwd
cp /etc/shadow /opt/etc/shadow
cp /etc/group /opt/etc/group
cp /etc/gshadow /opt/etc/gshadow
cp /etc/services /opt/etc/services
cp /etc/protocols /opt/etc/protocols
cp /etc/networks /opt/etc/networks
mount -o bind /proc /opt/proc
mount -o bind /dev /opt/dev
mount -o bind /dev/pts /opt/dev/pts
mount -o bind /dev/shm /opt/dev/shm
mount -o bind /tmp /opt/tmp
mount -o bind /home /opt/home
mount -o bind /sys /opt/sys
以上操做相似于shcroot管理chroot环境时作的初始化。
参考:
http://www.ibm.com/developerworks/cn/aix/library/au-new_openssh/index.html?ca=drs-?
http://ftp.gnumonks.org/pub/doc/chroot-howto.html
https://help.ubuntu.com/community/DebootstrapChroot
http://www.option-c.com/xwiki/Create_a_Debian_VM_with_debootstrap