如何在Ubuntu 14.04中配置Chroot环境

时间:2020-03-05 15:31:51  来源:igfitidea点击:

在许多情况下,我们可能希望隔离Linux系统中的某些应用程序,用户或者环境。
不同的操作系统使用不同的方法来实现隔离,在Linux中,经典的方法是通过“ chroot”环境。

在本教程中,我们将向我们逐步展示如何使用chroot设置隔离的环境,以在常规操作系统和所包含的环境之间建立屏障。
这主要用于测试目的。
我们将教我们有关Ubuntu 14.04 VPS实例的步骤。

大多数系统管理员将从了解如何实现快速便捷的chroot环境中受益,这是一项宝贵的技能。

chroot环境

chroot环境是一个操作系统调用,它将临时将根位置更改为新文件夹。
通常,操作系统对根目录的概念是位于“'/'”的实际根目录。
但是,使用'chroot',我们可以指定另一个目录作为chroot持续时间的顶级目录。

从原则上说,从“ chroot”内部运行的任何应用程序都将看不到操作系统的其余部分。

Chroot环境的优势

- Test applications without the risk of compromising the entire host system.
-  From the security point of view, whatever happens in the chroot environment won't affect the host system (not even under root user).
-  A different operating system running in the same hardware.

例如,它允许我们在与正常操作系统分离的环境中构建,安装和测试软件。
它也可以用作在64位环境中运行32位应用程序的方法。

但是,尽管chroot环境肯定会为没有特权的用户带来更多工作,但应该将其视为强化功能而不是安全功能,这意味着它们试图减少攻击媒介的数量而不是创建完整的解决方案。
如果需要完全隔离,请考虑使用更完整的解决方案,例如Linux容器,Docker,vserver等。

Debootstrap和Schroot

设置chroot环境所需的软件包是debootstrap和schroot,它们在ubuntu存储库中可用。
schroot命令用于设置chroot环境。

Debootstrap允许我们从目录中的存储库中安装任何Debian(或者基于debian)系统的新副本,其中包含运行操作系统基本实例所需的所有基本命令和二进制文件。

schroot允许使用相同机制的普通用户访问chroot,但是具有权限检查并允许chroot环境的其他自动设置,例如安装其他文件系统和其他配置任务。

这些是在Ubuntu 14.04 LTS中实现此功能的步骤:

1.安装软件包

首先,我们要在主机Ubuntu 14.04 LTS中安装debootstrap和schroot。

$sudo apt-get install debootstrap 
$sudo apt-get install schroot

2.配置Schroot

现在我们有了适当的工具,我们只需要指定一个目录即可用作chroot环境。
我们将在根目录中创建一个名为theitroad的目录,以在该目录中设置chroot:

sudo mkdir /theitroad

我们必须在配置文件中配置schroot以适应我们的需求。
我们将使用需要配置的信息来修改“ schroot”配置文件。

sudo nano /etc/schroot/schroot.conf

当前,我们使用的是Ubuntu 14.04 LTS(Trusty Tahr)系统,但是,我们想测试一下Ubuntu 13.10上可用的一些软件包,其代码为“ Saucy Salamander”。
我们可以通过创建一个如下所示的条目来做到这一点:

[saucy]
description=Ubuntu Saucy
location=/theitroad
priority=3
users=arun
root-groups=root

在上面的示例中修改配置参数的值以适合系统:

3.使用debootstrap安装32位Ubuntu

Debootstrap在chroot环境中下载并安装最小的操作系统。
只要我们有可用的存储库,就可以安装我们选择的任何基于debian的发行版。

上面,我们将chroot环境放在目录/theitroad下,这是chroot环境的根目录。
因此,我们需要在已经创建的目录中运行debootstrap:

cd /theitroad
sudo debootstrap --variant=buildd --arch amd64 saucy /theitroad/http://archive.ubuntu.com/ubuntu/sudo chroot /theitroad /debootstrap/debootstrap --second-stage

我们可以将--arch中的amd64替换为i386或者要在存储库中设置的其他位操作系统。
我们可以将上面的镜像http://archive.ubuntu.com/ubuntu/替换为最接近的镜像,我们可以从官方的Ubuntu Mirror Page获取最接近的镜像。

注意:如果我们选择在64位主机Ubuntu中设置i386位操作系统,则需要在第三行命令上方添加--foreign:

sudo debootstrap --variant=buildd --foreign --arch i386 saucy /theitroad/http://archive.ubuntu.com/ubuntu/It takes some time (depending on your bandwidth) to download, install and configure the complete system. It takes about 500 MBs for a minimal installation.4. Finallizing the chroot environmentAfter the system is installed, we'll need to do some final configurations to make sure the system functions correctly. First, we'll want to make sure our host fstab is aware of some pseudo-systems in our guest. sudo nano /etc/fstabAdd the below lines like these to the bottom of your fstab:proc /theitroad/proc proc defaults 0 0
sysfs /theitroad/sys sysfs defaults 0 0

保存并关闭文件。

现在,我们将需要在客户机中挂载这些文件系统:

$sudo mount proc /theitroad/proc -t proc
$sudo mount sysfs /theitroad/sys -t sysfs

我们还将要复制我们的“/etc/hosts”文件,以便我们可以访问正确的网络信息:

$sudo cp /etc/hosts /theitroad/etc/hosts

最后,我们可以使用schroot命令列出可用的chroot环境。

$schroot -l

我们可以通过如下命令进入chroot环境:

$sudo chroot /theitroad//bin/bash
You can test the chroot environment by checking the version of distributions installed.
# lsb_release -a
# uname -a

为了完成本教程,为了从chroot运行图形应用程序,我们必须导出DISPLAY环境变量。

$DISPLAY =:0.0 ./apps

其中我们已成功在主机Ubuntu 14.04 LTS(Trusty Tahr)中安装了Chrooted Ubuntu 13.10(Saucy Salamander)。

我们可以通过运行以下命令成功退出chroot环境:

# exit

之后,我们需要卸载proc和sys文件系统:

$sudo umount /test/proc
$sudo umount /test/sys