虛擬機chroot環境構建+openssh登陸

最近項目須要使用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 debootstrap
2 安裝schroot,它能夠用來管理chroot環境

apt-get -y install schroot
3 /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環境定義文件
    /etc/schroot/chroot.d 該目錄是額外的chroot環境的配置文件存放目錄
    /etc/schroot/setup.d 該目錄是chroot創建過程的setup文件目錄

5 設置/etc/schroot/schroot.conf配置文件,在文件末尾追加以下:

[test]
description=user test chroot
type=directory
directory=/opt
users=test
groups=root
root-groups=root
6 檢查配置文件是否有錯誤
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
hosts
3 修改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

相關文章
相關標籤/搜索