1.1 LXC是什么?

1.1.1 关于LXC

1.1.2 关于操作系统层虚拟化

1.1.3 LXC的特点

目前的LXC使用下列内核功能来控制进程:

1.1.4 LXC的应用

Docker****<span style=“font-family: ‘微软雅黑’,sans-serif; courier new"4courier new”; background: lime;">:

Proxmox VE****<span style=“font-family: ‘微软雅黑’,sans-serif; courier new"4courier new”; background: lime;">:

1.2 安装LXC

1.2.1 环境说明

   安装LXC内核版本不能低于2.6.32,对lxc至此最佳的为Ubuntu系统。

宿主机环境说明:

1.2.2 安装LXC

包组说明

 lxc          lxc相关软件

 libcgroup    资源管理,限制

 bridge-utils 管理桥接网卡

1.2.3 修改网卡配置

   修改lxc配置使用桥接网卡

重启网卡

查看桥接信息

1.2.4 启动服务

1.3 启动第一个LXC容器

参数说明:

<td style="width: 381.3pt; border-top: 1pt solid #9bbb59; border-right: 1pt solid #9bbb59; border-bottom: 1pt solid #9bbb59; border-left: none; background: #9bbb59; padding: 0cm 5.4pt;" width="508">
  <p style="text-align: center;" align="center">
    <strong><span style="font-family: '微软雅黑',sans-serif; courier new"4courier new";color: white;">说明</span></strong>
  </p>
</td>
<td style="width: 381.3pt; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="508">
  <p style="text-align: justify; text-justify: inter-ideograph;">
    <span style="font-family: '微软雅黑',sans-serif;">设置容器的模板</span>.LXC 1.0 <span style="font-family: '微软雅黑',sans-serif;">以上版本增加了</span> download <span style="font-family: '微软雅黑',sans-serif;">模版,支持下载定义好的系统镜像。</span>
  </p>
</td>
<td style="width: 381.3pt; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="508">
  <p style="text-align: justify; text-justify: inter-ideograph;">
    <span style="font-family: '微软雅黑',sans-serif;">为容器设置名称</span>
  </p>
</td>
<td style="width: 381.3pt; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="508">
  <p style="text-align: justify; text-justify: inter-ideograph;">
    <span style="font-family: '微软雅黑',sans-serif;">指定服务器地址</span>
  </p>
</td>
<td style="width: 381.3pt; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="508">
  <p style="text-align: justify; text-justify: inter-ideograph;">
    <span style="font-family: '微软雅黑',sans-serif;">发行版本</span>
  </p>
</td>
<td style="width: 381.3pt; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; background: #EAF1DD; padding: 0cm 5.4pt 0cm 5.4pt;" width="508">
  <p style="text-align: justify; text-justify: inter-ideograph;">
    <span style="font-family: '微软雅黑',sans-serif;">系统版本</span>
  </p>
</td>
<td style="width: 381.3pt; border-top: none; border-left: none; border-bottom: solid #C2D69B 1.0pt; border-right: solid #C2D69B 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt;" width="508">
  <p style="text-align: justify; text-justify: inter-ideograph;">
    <span style="font-family: '微软雅黑',sans-serif;">架构</span>
  </p>
</td>

查看你lxc的缓存文件位置

1.3.1 查看你当前容器列表

1.3.2 修改容器的密码

1.3.3 启动容器

   ip设置好后可以启动容器

1.3.4 启动容器的其他方法

1.4 LXC管理操作

克隆一个容器

检查lxc配置

持续观察容器的状态和优先级变化:

 LXC 使用 cgroup 文件系统管理容器。

可以通过 LXC 读和操纵 cgroup 文件系统的一些部分。要管理每个容器对 cpu 的使用,则可以通过读取和调整容器的 cpu.shares 来进行:

暂停和恢复

停止,停止一个容器将导致该容器中启动的所有进程全体死亡,并且清理容器:

销毁,销毁容器是指删除通过 lxc-create 步骤与名称关联的配置文件和元数据:

查看容器当前状态

查看lxc使用进程情况

1.5 使用 chroot 监狱限制 SSH 用户访问指定目录

1.5.1 当前网络威胁

本文简述一下chroot是如何工作的,并着重讨论一下开发者和管理员能够用到的一些最佳实践来让系统更加安全。

1.5.2 chroot做什么

1.5.3 创建 SSH chroot 监狱

1、使用 mkdir 命令开始创建 chroot 监狱

2、根据 sshd_config 手册找到所需的文件,ChrootDirectory 选项指定在身份验证后要 chroot 到的目录的路径名。该目录必须包含支持用户会话所必需的文件和目录。

3、使用 mknod 命令创建 /dev 下的文件。在下面的命令中,-m 标志用来指定文件权限位,c 意思是字符文件,两个数字分别是文件指向的主要号和次要号。

4、在 chroot 监狱中设置合适的权限。注意 chroot 监狱和它的子目录以及子文件必须被 root 用户所有,并且对普通用户或用户组不可写

1.5.4 SSH chroot 监狱设置交互式 shell

1、创建 bin 目录并复制 /bin/bash bin

2、识别 bash 所需的共享库,如下所示复制它们到 lib64

1.5.5 创建并配置 SSH 用户

1、使用 useradd 命令创建 SSH 用户,并设置安全密码

2、创建 chroot 监狱通用配置目录 /opt/etc 并复制已更新的账号文件(/etc/passwd /etc/group)到这个目录中

1.5.6 配置 SSH 来使用 chroot 监狱

1、配置sshd配置文件,/etc/ssh/sshd_config

配置参数说明

2、重新启动sshd服务

1.5.7 测试 SSH chroot 监狱

   用户只可以执行 bash 以及它内置的命令(比如:pwdhistoryecho 等等):

1.5.8 创建用户的主目录并添加 Linux 命令

2、将在 bin 目录中复制进几个用户命令,如 lsdatemkdir

3、接下来,检查上面命令的共享库并将它们移到 chroot 监狱的库目录中

   查看ls命令共享库

   复制共享库文件

1.5.9 测试 sftp chroot 监狱

   /etc/ssh/sshd_config 中添加下面的行

2、现在使用 ssh 测试,你会得到下面的错误:

3、试下使用 sftp

   复制sftp程序

   sftp传输文件测试

   到远端进行查看

   <span style=“font-family: ‘微软雅黑’,sans-serif; courier new"4courier new”; color: red; background: yellow;">至此chroot 监狱就配置完成

1.6 参考文献

 1 https://linuxcontainers.org

2 https://zh.wikipedia.org/wiki/LXC

[3] https://www.ibm.com/developerworks/cn/linux/l-lxc-containers/

[4] https://www.tecmint.com/restrict-ssh-user-to-directory-using-chrooted-jail/

[5] https://linux.cn/article-8313-1.html

[6] http://blog.csdn.net/napolunyishi/article/details/21078799

<li>
  <a href="#12_LXC">1.2 安装LXC</a><ul>
    <li>
      <a href="#121">1.2.1 环境说明</a>
    </li>
    <li>
      <a href="#122_LXC">1.2.2 安装LXC</a>
    </li>
    <li>
      <a href="#123">1.2.3 修改网卡配置</a>
    </li>
    <li>
      <a href="#124">1.2.4 启动服务</a>
    </li>
  </ul>
</li>

<li>
  <a href="#13_LXC">1.3 启动第一个LXC容器</a><ul>
    <li>
      <a href="#131">1.3.1 查看你当前容器列表</a>
    </li>
    <li>
      <a href="#132">1.3.2 修改容器的密码</a>
    </li>
    <li>
      <a href="#133">1.3.3 启动容器</a>
    </li>
    <li>
      <a href="#134">1.3.4 启动容器的其他方法</a>
    </li>
  </ul>
</li>

<li>
  <a href="#14_LXC">1.4 LXC管理操作</a>
</li>
<li>
  <a href="#15_chroot_SSH">1.5 使用 chroot 监狱限制 SSH 用户访问指定目录</a><ul>
    <li>
      <a href="#151">1.5.1 当前网络威胁</a>
    </li>
    <li>
      <a href="#152_chroot">1.5.2 chroot做什么</a>
    </li>
    <li>
      <a href="#153_SSH_chroot">1.5.3 创建 SSH chroot 监狱</a>
    </li>
    <li>
      <a href="#154_SSH_chroot_shell">1.5.4 为 SSH chroot 监狱设置交互式 shell</a>
    </li>
    <li>
      <a href="#155_SSH">1.5.5 创建并配置 SSH 用户</a>
    </li>
    <li>
      <a href="#156_SSH_chroot">1.5.6 配置 SSH 来使用 chroot 监狱</a>
    </li>
    <li>
      <a href="#157_SSH_chroot">1.5.7 测试 SSH 的 chroot 监狱</a>
    </li>
    <li>
      <a href="#158_Linux">1.5.8 创建用户的主目录并添加 Linux 命令</a>
    </li>
    <li>
      <a href="#159_sftp__chroot">1.5.9 测试 sftp 的 用 chroot 监狱</a>
    </li>
  </ul>
</li>

<li>
  <a href="#16">1.6 参考文献</a>
</li>