{"id":5415,"date":"2020-07-30T13:14:28","date_gmt":"2020-07-30T05:14:28","guid":{"rendered":"http:\/\/39.97.231.10\/?p=5415"},"modified":"2020-07-30T13:14:28","modified_gmt":"2020-07-30T05:14:28","slug":"lxc-%e5%ae%b9%e5%99%a8%e9%9b%86chroot%e4%bd%bf%e7%94%a8%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=5415","title":{"rendered":"LXC \u5bb9\u5668\u96c6chroot\u4f7f\u7528\u8bf4\u660e"},"content":{"rendered":"<h1>1.1 LXC\u662f\u4ec0\u4e48\uff1f<\/h1>\n<div>\n<div id=\\\"cnblogs_post_body\\\">\n<h3 id=\\\"auto-id-1\\\">1.1.1\u00a0\u5173\u4e8eLXC<\/h3>\n<p>LXC\uff0c\u5176\u540d\u79f0\u6765\u81eaLinux\u8f6f\u4ef6\u5bb9\u5668\uff08Linux Containers\uff09\u7684\u7f29\u5199\uff0c\u4e00\u79cd\u64cd\u4f5c\u7cfb\u7edf\u5c42\u865a\u62df\u5316\uff08Operating system\u2013level virtualization\uff09\u6280\u672f\uff0c\u4e3aLinux\u5185\u6838\u5bb9\u5668\u529f\u80fd\u7684\u4e00\u4e2a\u7528\u6237\u7a7a\u95f4\u63a5\u53e3\u3002\u5b83\u5c06\u5e94\u7528\u8f6f\u4ef6\u7cfb\u7edf\u6253\u5305\u6210\u4e00\u4e2a\u8f6f\u4ef6\u5bb9\u5668\uff08Container\uff09\uff0c\u5185\u542b\u5e94\u7528\u8f6f\u4ef6\u672c\u8eab\u7684\u4ee3\u7801\uff0c\u4ee5\u53ca\u6240\u9700\u8981\u7684\u64cd\u4f5c\u7cfb\u7edf\u6838\u5fc3\u548c\u5e93\u3002\u901a\u8fc7\u7edf\u4e00\u7684\u540d\u5b57\u7a7a\u95f4\u548c\u5171\u7528API\u6765\u5206\u914d\u4e0d\u540c\u8f6f\u4ef6\u5bb9\u5668\u7684\u53ef\u7528\u786c\u4ef6\u8d44\u6e90\uff0c\u521b\u9020\u51fa\u5e94\u7528\u7a0b\u5e8f\u7684\u72ec\u7acb\u6c99\u7bb1\u8fd0\u884c\u73af\u5883\uff0c\u4f7f\u5f97Linux\u7528\u6237\u53ef\u4ee5\u5bb9\u6613\u7684\u521b\u5efa\u548c\u7ba1\u7406\u7cfb\u7edf\u6216\u5e94\u7528\u5bb9\u5668\u3002<\/p>\n<p align=\\\"center\\\"><img src=\\\"https:\/\/images2017.cnblogs.com\/blog\/1190037\/201802\/1190037-20180203180641203-214434730.png\\\" alt=\\\"\\\" \/><\/p>\n<p align=\\\"center\\\">\u56fe\u00a0&#8211; lxc\u5b98\u65b9logo<\/p>\n<p>\u5728Linux\u5185\u6838\u4e2d\uff0c\u63d0\u4f9b\u4e86cgroups\u529f\u80fd\uff0c\u6765\u8fbe\u6210\u8d44\u6e90\u7684\u533a\u9694\u5316\u3002\u5b83\u540c\u65f6\u4e5f\u63d0\u4f9b\u4e86\u540d\u79f0\u7a7a\u95f4\u533a\u9694\u5316\u7684\u529f\u80fd\uff0c\u4f7f\u5e94\u7528\u7a0b\u5e8f\u770b\u5230\u7684\u64cd\u4f5c\u7cfb\u7edf\u73af\u5883\u88ab\u533a\u9694\u6210\u72ec\u7acb\u533a\u95f4\uff0c\u5305\u62ec\u8fdb\u7a0b\u6811\uff0c\u7f51\u7edc\uff0c\u7528\u6237id\uff0c\u4ee5\u53ca\u6302\u8f7d\u7684\u6587\u4ef6\u7cfb\u7edf\u3002\u4f46\u662fcgroups\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u5f15\u5bfc\u4efb\u4f55\u865a\u62df\u673a\u3002<br \/>\nLXC\u5229\u7528cgroups\u4e0e\u540d\u79f0\u7a7a\u95f4\u7684\u529f\u80fd\uff0c\u63d0\u4f9b\u5e94\u7528\u8f6f\u4ef6\u4e00\u4e2a\u72ec\u7acb\u7684\u64cd\u4f5c\u7cfb\u7edf\u73af\u5883\u3002LXC\u4e0d\u9700\u8981Hypervisor\u8fd9\u4e2a\u8f6f\u4ef6\u5c42\uff0c\u8f6f\u4ef6\u5bb9\u5668\uff08Container\uff09\u672c\u8eab\u6781\u4e3a\u8f7b\u91cf\u5316\uff0c\u63d0\u5347\u4e86\u521b\u5efa\u865a\u62df\u673a\u7684\u901f\u5ea6\u3002\u8f6f\u4ef6Docker\u88ab\u7528\u6765\u7ba1\u7406LXC\u7684\u73af\u5883\u3002<\/p>\n<h3 id=\\\"auto-id-2\\\">1.1.2\u00a0\u5173\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u5c42\u865a\u62df\u5316<\/h3>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u5c42\u865a\u62df\u5316\uff08\u82f1\u8bed\uff1aOperating system\u2013level virtualization\uff09\uff0c\u4e00\u79cd\u865a\u62df\u5316\u6280\u672f\uff0c\u8fd9\u79cd\u6280\u672f\u5c06\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u865a\u62df\u5316\uff0c\u53ef\u4ee5\u5141\u8bb8\u4f7f\u7528\u8005\u7a7a\u95f4\u8f6f\u4ef6\u7269\u4ef6\uff08instances\uff09\u88ab\u5206\u5272\u6210\u51e0\u4e2a\u72ec\u7acb\u7684\u5355\u5143\uff0c\u5728\u5185\u6838\u4e2d\u8fd0\u884c\uff0c\u800c\u4e0d\u662f\u53ea\u6709\u4e00\u4e2a\u5355\u4e00\u7269\u4ef6\u8fd0\u884c\u3002\u8fd9\u4e2a\u8f6f\u4ef6\u7269\u4ef6\uff0c\u4e5f\u88ab\u79f0\u4e3a\u662f\u4e00\u4e2a\u5bb9\u5668\uff08containers\uff09\uff0c\u865a\u62df\u5f15\u64ce\uff08Virtualization engine\uff09\uff0c\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668\uff08virtual private servers\uff09\u6216\u662f\u00a0jails\u3002\u5bf9\u6bcf\u4e2a\u884c\u7a0b\u7684\u62e5\u6709\u8005\u4e0e\u4f7f\u7528\u8005\u6765\u8bf4\uff0c\u4ed6\u4eec\u4f7f\u7528\u7684\u670d\u52a1\u5668\u7a0b\u5f0f\uff0c\u770b\u8d77\u6765\u5c31\u50cf\u662f\u81ea\u5df1\u4e13\u7528\u7684\u3002<br \/>\n\u64cd\u4f5c\u7cfb\u7edf\u5c42\u865a\u62df\u5316\u4e4b\u540e\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8f6f\u4ef6\u7684\u5373\u65f6\u8fc1\u79fb\uff08Live migration\uff09\uff0c\u4f7f\u4e00\u4e2a\u8f6f\u4ef6\u5bb9\u5668\u4e2d\u7684\u7269\u4ef6\uff0c\u5373\u65f6\u79fb\u52a8\u5230\u53e6\u4e00\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u4e0b\uff0c\u518d\u91cd\u65b0\u6267\u884c\u8d77\u6765\u3002\u4f46\u662f\u5728\u8fd9\u79cd\u6280\u672f\u4e0b\uff0c\u8f6f\u4ef6\u5373\u65f6\u8fc1\u79fb\uff0c\u53ea\u80fd\u5728\u540c\u6837\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8fdb\u884c\u3002<br \/>\n\u5728\u7c7bUnix\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u4e2a\u6280\u672f\u6700\u65e9\u8d77\u6e90\u4e8e\u6807\u51c6\u7684chroot\u673a\u5236\uff0c\u518d\u8fdb\u4e00\u6b65\u6f14\u5316\u800c\u6210\u3002\u9664\u4e86\u5c06\u8f6f\u4ef6\u72ec\u7acb\u5316\u7684\u673a\u5236\u4e4b\u5916\uff0c\u5185\u6838\u901a\u5e38\u4e5f\u63d0\u4f9b\u8d44\u6e90\u7ba1\u7406\u529f\u80fd\uff0c\u4f7f\u5f97\u5355\u4e00\u8f6f\u4ef6\u5bb9\u5668\u5728\u8fd0\u4f5c\u65f6\uff0c\u5bf9\u4e8e\u5176\u4ed6\u8f6f\u4ef6\u5bb9\u5668\u7684\u9020\u6210\u7684\u4ea4\u4e92\u5f71\u54cd\u6700\u5c0f\u5316\u3002<\/p>\n<h3 id=\\\"auto-id-3\\\">1.1.3 LXC\u7684\u7279\u70b9<\/h3>\n<p>\u76ee\u524d\u7684LXC\u4f7f\u7528\u4e0b\u5217\u5185\u6838\u529f\u80fd\u6765\u63a7\u5236\u8fdb\u7a0b\uff1a<br \/>\n\ud83c\udfc4\u200d\u2642\ufe0f\u00a0\u5185\u6838\u547d\u540d\u7a7a\u95f4\uff08\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3001uts\u3001mount\u3001pid\u3001network\u548cuser\uff09<br \/>\n\ud83c\udfc4\u200d\u2642\ufe0f AppArmor\u548cSELinux\u914d\u7f6e<br \/>\n\ud83c\udfc4\u200d\u2642\ufe0f Seccomp\u7b56\u7565<br \/>\n\ud83c\udfc4\u200d\u2642\ufe0f chroot\uff08\u4f7f\u7528pivot_root\uff09<br \/>\n\ud83c\udfc4\u200d\u2642\ufe0f Kernel Capibilities<br \/>\n\ud83c\udfc4\u200d\u2642\ufe0f\u00a0\u63a7\u5236\u7ec4\uff08cgroups\uff09<br \/>\n\u56e0\u6b64\uff0cLXC\u901a\u5e38\u88ab\u8ba4\u4e3a\u4ecb\u4e8e\u201c\u52a0\u5f3a\u7248\u201d\u7684chroot\u548c\u5b8c\u5168\u6210\u719f\u7684\u865a\u62df\u673a\u4e4b\u95f4\u7684\u6280\u672f\u3002LXC\u7684\u76ee\u6807\u662f\u521b\u5efa\u4e00\u4e2a\u5c3d\u53ef\u80fd\u4e0e\u6807\u51c6\u5b89\u88c5\u7684Linux\u76f8\u540c\u4f46\u53c8\u4e0d\u9700\u8981\u5206\u79bb\u5185\u6838\u7684\u73af\u5883\u3002<br \/>\n\u5173\u4e8echroot\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\\\"https:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-chroot\/\\\">https:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-cn-chroot\/<\/a><\/p>\n<h3 id=\\\"auto-id-4\\\">1.1.4 LXC\u7684\u5e94\u7528<\/h3>\n<p>Docker\uff1a<br \/>\n\u5b83\u57280.9\u7248\u4e4b\u524d\u90fd\u662f\u4f7f\u7528LXC\u6280\u672f\uff0c\u4f46\u57280.9\u7248\u4e4b\u540e\uff0c\u5df2\u4e0d\u518d\u662f\u552f\u4e00\u4e14\u9ed8\u8ba4\u7684\u8fd0\u884c\u73af\u5883\u3002<br \/>\nProxmox VE\uff1a<br \/>\nProxmox VE\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u670d\u52a1\u5668\u865a\u62df\u5316\u73af\u5883Linux\u53d1\u884c\u7248\u3002\u57fa\u4e8eDebian\uff0c\u4f7f\u7528\u57fa\u4e8eUbuntu\u7684\u5b9a\u5236\u5185\u6838\u3002<br \/>\n\u5b83\u76f4\u52304.0\u7248\u624d\u4f7f\u7528LXC\u6280\u672f\uff0c\u5728\u6b64\u4e4b\u524d\u7684\u7248\u672c\u90fd\u662f\u4f7f\u7528OpenVZ\u6280\u672f\u3002<\/p>\n<h2 id=\\\"auto-id-5\\\">1.2\u00a0\u5b89\u88c5LXC<\/h2>\n<h3 id=\\\"auto-id-6\\\">1.2.1\u00a0\u73af\u5883\u8bf4\u660e<\/h3>\n<p>\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/linuxcontainers.org\/lxc\/getting-started\/<br \/>\n\u5b89\u88c5LXC\u5185\u6838\u7248\u672c\u4e0d\u80fd\u4f4e\u4e8e2.6.32\uff0c\u5bf9lxc\u81f3\u6b64\u6700\u4f73\u7684\u4e3aUbuntu\u7cfb\u7edf\u3002<br \/>\n\u5bbf\u4e3b\u673a\u73af\u5883\u8bf4\u660e\uff1a<\/p>\n<div>\n<pre>[root@lxc ~]# cat \/etc\/redhat-release\nCentOS Linux release 7.2.1511 (Core)\n[root@lxc ~]# uname -r\n3.10.0-327.el7.x86_64\n[root@lxc ~]# sestatus\nSELinux status:                 disabled\n[root@lxc ~]# hostname -I\n172.16.1.100 10.0.0.100<\/pre>\n<\/div>\n<p>\u5b89\u88c5lxc\u9700\u8981\u4f7f\u7528\u5230epel\u6e90\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u662faliyun\u6e90\u7ad9<\/p>\n<div>\n<pre>[root@lxc ~]# yum install epel-release -y\n[root@lxc ~]# curl -o \/etc\/yum.repos.d\/epel.repo http:\/\/mirrors.aliyun.com\/repo\/epel-7.repo<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-7\\\">1.2.2\u00a0\u5b89\u88c5LXC<\/h3>\n<div>\n<pre>[root@lxc ~]# yum install lxc-* libcgroup* bridge-utils.x86_64 -y<\/pre>\n<\/div>\n<p>\u5305\u7ec4\u8bf4\u660e<\/p>\n<blockquote><p>\u00a0lxc\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0lxc\u76f8\u5173\u8f6f\u4ef6<br \/>\nlibcgroup\u00a0 \u00a0\u00a0\u8d44\u6e90\u7ba1\u7406\uff0c\u9650\u5236<br \/>\nbridge-utils\u00a0\u7ba1\u7406\u6865\u63a5\u7f51\u5361<\/p><\/blockquote>\n<h3 id=\\\"auto-id-8\\\">1.2.3\u00a0\u4fee\u6539\u7f51\u5361\u914d\u7f6e<\/h3>\n<p>\u521b\u5efa\u6865\u63a5\u7f51\u5361<\/p>\n<div>\n<pre>[root@lxc ~]#  cat \/etc\/sysconfig\/network-scripts\/ifcfg-eth0\nTYPE=Ethernet\nBOOTPROTO=none\nNAME=eth0\nDEVICE=eth0\nONBOOT=yes\nBRIDGE=br0\n[root@lxc ~]#  cat \/etc\/sysconfig\/network-scripts\/ifcfg-br0\nTYPE=Bridge\nBOOTPROTO=static\nNAME=br0\nDEVICE=br0\nONBOOT=yes\nIPADDR=10.0.0.100\nNETMASK=255.255.255.0\nGATEWAY=10.0.0.254\nDNS1=223.5.5.5<\/pre>\n<\/div>\n<p>\u4fee\u6539lxc\u914d\u7f6e\u4f7f\u7528\u6865\u63a5\u7f51\u5361<\/p>\n<div>\n<pre>[root@lxc ~]# vi \/etc\/lxc\/default.conf\nlxc.network.type = veth\nlxc.network.link = br0\nlxc.network.flags = up<\/pre>\n<\/div>\n<p>\u91cd\u542f\u7f51\u5361<\/p>\n<div>\n<pre>[root@lxc ~]# \/etc\/init.d\/network restart\nRestarting network (via systemctl):                        [  \u786e\u5b9a  ]<\/pre>\n<\/div>\n<p>\u67e5\u770b\u6865\u63a5\u4fe1\u606f<\/p>\n<div>\n<pre>[root@lxc ~]# brctl show\nbridge name    bridge id        STP enabled    interfaces\nbr0        8000.000c29c008a6    no        eth0<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-9\\\">1.2.4\u00a0\u542f\u52a8\u670d\u52a1<\/h3>\n<div>\n<pre>[root@lxc ~]# systemctl restart cgconfig.service\n[root@lxc ~]# systemctl enable cgconfig.service\n[root@lxc ~]# ystemctl restart lxc.service<\/pre>\n<\/div>\n<h2 id=\\\"auto-id-10\\\">1.3\u00a0\u542f\u52a8\u7b2c\u4e00\u4e2aLXC\u5bb9\u5668<\/h2>\n<p>\u53c2\u8003\u65b9\u6cd5:\u00a0<a href=\\\"https:\/\/mirrors.tuna.tsinghua.edu.cn\/help\/lxc-images\/\\\">https:\/\/mirrors.tuna.tsinghua.edu.cn\/help\/lxc-images\/<\/a><br \/>\nlxc\u542f\u52a8\u5bb9\u5668\u65f6\uff0c\u63a8\u8350\u4f7f\u7528ubuntu\/centos6\u7cfb\u7edf\uff0ccentos7\u7cfb\u7edf\u4f7f\u7528\u4f1a\u6709\u95ee\u9898\u3002<\/p>\n<div>\n<pre>[root@docker01 ~]# lxc-create -t download -n centos7 -- --server mirrors.tuna.tsinghua.edu.cn\/lxc-images -d centos -r 7 -a amd64<\/pre>\n<\/div>\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<table border=\\\"1\\\" cellspacing=\\\"0\\\" cellpadding=\\\"0\\\">\n<tbody>\n<tr>\n<td width=\\\"189\\\">\n<p align=\\\"center\\\">\u53c2\u6570<\/p>\n<\/td>\n<td width=\\\"508\\\">\n<p align=\\\"center\\\">\u8bf4\u660e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\\\"189\\\">-t, &#8211;template=t<\/td>\n<td width=\\\"508\\\">\u8bbe\u7f6e\u5bb9\u5668\u7684\u6a21\u677f.LXC 1.0\u00a0\u4ee5\u4e0a\u7248\u672c\u589e\u52a0\u4e86\u00a0download\u00a0\u6a21\u7248\uff0c\u652f\u6301\u4e0b\u8f7d\u5b9a\u4e49\u597d\u7684\u7cfb\u7edf\u955c\u50cf\u3002<\/td>\n<\/tr>\n<tr>\n<td width=\\\"189\\\">-n, &#8211;name=NAME<\/td>\n<td width=\\\"508\\\">\u4e3a\u5bb9\u5668\u8bbe\u7f6e\u540d\u79f0<\/td>\n<\/tr>\n<tr>\n<td width=\\\"189\\\">&#8211;server<\/td>\n<td width=\\\"508\\\">\u6307\u5b9a\u670d\u52a1\u5668\u5730\u5740<\/td>\n<\/tr>\n<tr>\n<td width=\\\"189\\\">-d\u00a0DIST<\/td>\n<td width=\\\"508\\\">\u53d1\u884c\u7248\u672c<\/td>\n<\/tr>\n<tr>\n<td width=\\\"189\\\">-r\u00a0RELEASE<\/td>\n<td width=\\\"508\\\">\u7cfb\u7edf\u7248\u672c<\/td>\n<\/tr>\n<tr>\n<td width=\\\"189\\\">-a\u00a0ARCH<\/td>\n<td width=\\\"508\\\">\u67b6\u6784<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u67e5\u770b\u4f60lxc\u7684\u7f13\u5b58\u6587\u4ef6\u4f4d\u7f6e<\/p>\n<div>\n<pre>[root@lxc lxc]# cd \/var\/cache\/lxc\n[root@lxc lxc]# tree\n.\n\u2514\u2500\u2500 download\n    \u2514\u2500\u2500 centos\n        \u251c\u2500\u2500 6\n        \u2502   \u2514\u2500\u2500 amd64\n        \u2502       \u2514\u2500\u2500 default\n        \u2502           \u251c\u2500\u2500 build_id\n        \u2502           \u251c\u2500\u2500 config\n        \u2502           \u251c\u2500\u2500 config-user\n        \u2502           \u251c\u2500\u2500 create-message\n        \u2502           \u251c\u2500\u2500 excludes-user\n        \u2502           \u251c\u2500\u2500 expiry\n        \u2502           \u251c\u2500\u2500 rootfs.tar.xz\n        \u2502           \u2514\u2500\u2500 templates\n        \u2514\u2500\u2500 7\n            \u2514\u2500\u2500 amd64\n                \u2514\u2500\u2500 default\n                    \u251c\u2500\u2500 build_id\n                    \u251c\u2500\u2500 config\n                    \u251c\u2500\u2500 config-user\n                    \u251c\u2500\u2500 create-message\n                    \u251c\u2500\u2500 excludes-user\n                    \u251c\u2500\u2500 expiry\n                    \u251c\u2500\u2500 rootfs.tar.xz\n                    \u2514\u2500\u2500 templates<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-11\\\">1.3.1\u00a0\u67e5\u770b\u4f60\u5f53\u524d\u5bb9\u5668\u5217\u8868<\/h3>\n<div>\n<pre>[root@lxc ~]# lxc-ls\ncentos6  centos7<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-12\\\">1.3.2\u00a0\u4fee\u6539\u5bb9\u5668\u7684\u5bc6\u7801<\/h3>\n<p>\u4fee\u6539\u5bc6\u7801\u4f7f\u7528chroot\u7684\u65b9\u5f0f\u8fdb\u884c\u4fee\u6539<\/p>\n<div>\n<pre>[root@lxc ~]# chroot \/var\/lib\/lxc\/centos7\/rootfs\/ passwd\nChanging password for user root.\nNew password: 123456\nBAD PASSWORD: The password is shorter than 8 characters\nRetype new password: 123456\npasswd: all authentication tokens updated successfully.<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-13\\\">1.3.3\u00a0\u542f\u52a8\u5bb9\u5668<\/h3>\n<p>\u5728\u542f\u52a8\u5bb9\u5668\u4e4b\u524d\uff0c\u5148\u4fee\u6539\u5bb9\u5668\u914d\u7f6e\u6587\u4ef6\uff0c\u6dfb\u52a0ip\u5730\u5740\u4fe1\u606f<\/p>\n<div>\n<pre>[root@lxc ~]# vim \/var\/lib\/lxc\/centos7\/config\nlxc.network.name = eth0\nlxc.network.ipv4 = 10.0.0.111\/24\nlxc.network.ipv4.gateway = 10.0.0.254<\/pre>\n<\/div>\n<p>ip\u8bbe\u7f6e\u597d\u540e\u53ef\u4ee5\u542f\u52a8\u5bb9\u5668<\/p>\n<div>\n<pre>[root@lxc ~]# lxc-start -n centos7\ncentos7 login: root\nPassword:\nLast failed login: Wed Jan 31 10:12:22 UTC 2018 on lxc\/console\nThere was 1 failed login attempt since the last successful login.\n[root@centos7 ~]# yum install openssh-server -y\n[root@centos7 ~]# systemctl start sshd<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-14\\\">1.3.4\u00a0\u542f\u52a8\u5bb9\u5668\u7684\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<div>\n<pre>lxc-create -t centos -n test<\/pre>\n<\/div>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u542f\u52a8\u7684\u5bb9\u5668\u4f1a\u751f\u6210\u9ed8\u8ba4\u7684\u5bc6\u7801<br \/>\n\u9ed8\u8ba4\u5bc6\u7801\u4f4d\u7f6e;<\/p>\n<div>\n<pre>cat \/var\/lib\/lxc\/test\/tmp_root_pass<\/pre>\n<\/div>\n<h2 id=\\\"auto-id-15\\\">1.4 LXC\u7ba1\u7406\u64cd\u4f5c<\/h2>\n<p>LXC\u5b98\u65b9\u624b\u518c\uff1a<a href=\\\"http:\/\/lxc.sourceforge.net\/man\/\\\">http:\/\/lxc.sourceforge.net\/man\/<\/a><br \/>\n\u514b\u9686\u4e00\u4e2a\u5bb9\u5668<\/p>\n<div>\n<pre>[root@docker01 ~]# lxc-clone  test  clsn-image\nCreated container clsn-image as copy of test<\/pre>\n<\/div>\n<p>\u68c0\u67e5lxc\u914d\u7f6e<\/p>\n<div>\n<pre>[root@docker01 ~]# lxc-checkconfig\n--- Namespaces ---\nNamespaces: enabled\nUtsname namespace: enabled\nIpc namespace: enabled\nPid namespace: enabled\nUser namespace: enabled\nNetwork namespace: enabled\nMultiple \/dev\/pts instances: enabled\n\u00b7\u00b7\u00b7<\/pre>\n<\/div>\n<p>\u6301\u7eed\u89c2\u5bdf\u5bb9\u5668\u7684\u72b6\u6001\u548c\u4f18\u5148\u7ea7\u53d8\u5316\uff1a<\/p>\n<div>\n<pre>lxc-monitor -n name<\/pre>\n<\/div>\n<p>LXC\u00a0\u4f7f\u7528\u00a0cgroup\u00a0\u6587\u4ef6\u7cfb\u7edf\u7ba1\u7406\u5bb9\u5668\u3002<br \/>\n\u53ef\u4ee5\u901a\u8fc7\u00a0LXC\u00a0\u8bfb\u548c\u64cd\u7eb5\u00a0cgroup\u00a0\u6587\u4ef6\u7cfb\u7edf\u7684\u4e00\u4e9b\u90e8\u5206\u3002\u8981\u7ba1\u7406\u6bcf\u4e2a\u5bb9\u5668\u5bf9\u00a0cpu\u00a0\u7684\u4f7f\u7528\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u548c\u8c03\u6574\u5bb9\u5668\u7684\u00a0cpu.shares\u00a0\u6765\u8fdb\u884c\uff1a<\/p>\n<div>\n<pre>lxc-cgroup -n name cpu.shares\nlxc-cgroup -n name cpu.shares howmany<\/pre>\n<\/div>\n<p>\u6682\u505c\u548c\u6062\u590d<\/p>\n<div>\n<pre>lxc-freeze -n name\nlxc-unfreeze -n name<\/pre>\n<\/div>\n<p>\u505c\u6b62\uff0c\u505c\u6b62\u4e00\u4e2a\u5bb9\u5668\u5c06\u5bfc\u81f4\u8be5\u5bb9\u5668\u4e2d\u542f\u52a8\u7684\u6240\u6709\u8fdb\u7a0b\u5168\u4f53\u6b7b\u4ea1\uff0c\u5e76\u4e14\u6e05\u7406\u5bb9\u5668\uff1a<\/p>\n<div>\nlxc-stop -n name\n<\/div>\n<p>\u9500\u6bc1\uff0c\u9500\u6bc1\u5bb9\u5668\u662f\u6307\u5220\u9664\u901a\u8fc7\u00a0lxc-create\u00a0\u6b65\u9aa4\u4e0e\u540d\u79f0\u5173\u8054\u7684\u914d\u7f6e\u6587\u4ef6\u548c\u5143\u6570\u636e\uff1a<\/p>\n<div>\nlxc-destroy -n name\n<\/div>\n<p>\u67e5\u770b\u5bb9\u5668\u5f53\u524d\u72b6\u6001<\/p>\n<div>\n<pre>[root@lxc ~]# lxc-info --name centos6\nName:           centos6\nState:          STOPPED<\/pre>\n<\/div>\n<p>\u67e5\u770blxc\u4f7f\u7528\u8fdb\u7a0b\u60c5\u51b5<\/p>\n<div>\nlxc-top\n<\/div>\n<h2 id=\\\"auto-id-16\\\">1.5\u00a0\u4f7f\u7528\u00a0chroot\u00a0\u76d1\u72f1\u9650\u5236\u00a0SSH\u00a0\u7528\u6237\u8bbf\u95ee\u6307\u5b9a\u76ee\u5f55<\/h2>\n<h3 id=\\\"auto-id-17\\\">1.5.1\u00a0\u5f53\u524d\u7f51\u7edc\u5a01\u80c1<\/h3>\n<p>\u66b4\u9732\u5728\u4e92\u8054\u7f51\u4e0a\u7684\u7cfb\u7edf\u8981\u5c06\u574f\u4eba\u9694\u79bb\u5728\u5916\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u6311\u6218\uff0c\u800c\u4e14\u4e00\u76f4\u66f4\u65b0\u6700\u65b0\u7684\u5b89\u5168\u8865\u4e01\u4e5f\u4e0d\u592a\u5bb9\u6613\u3002\u56e0\u6b64\uff0c\u4e00\u4e9b\u806a\u660e\u7684\u7ba1\u7406\u5458\u4eec\u5c1d\u8bd5\u91c7\u7528\u7cfb\u7edf\u7684\u65b9\u6cd5\u6765\u9650\u5236\u53ef\u80fd\u53d1\u751f\u7684\u5165\u4fb5\uff0c\u8fd9\u5176\u4e2d\u4e00\u4e2a\u7edd\u4f73\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528chroot\u76d1\u72f1\uff08jail\uff09\u3002<br \/>\nchroot\u76d1\u72f1\u6781\u5927\u5730\u9650\u5236\u4e86\u5e94\u7528\u7a0b\u5e8f\u53ef\u67e5\u770b\u7684\u6587\u4ef6\u7cfb\u7edf\u8303\u56f4\uff0c\u62e5\u6709\u66f4\u5c11\u7684\u7cfb\u7edf\u6743\u9650\uff0c\u8fd9\u4e9b\u90fd\u662f\u4e3a\u4e86\u9650\u5236\u5e94\u7528\u7a0b\u5e8f\u8bef\u64cd\u4f5c\u6216\u8005\u88ab\u574f\u4eba\u5229\u7528\u4ece\u800c\u5bf9\u7cfb\u7edf\u9020\u6210\u635f\u5bb3\u3002<br \/>\n\u672c\u6587\u7b80\u8ff0\u4e00\u4e0bchroot\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u5e76\u7740\u91cd\u8ba8\u8bba\u4e00\u4e0b\u5f00\u53d1\u8005\u548c\u7ba1\u7406\u5458\u80fd\u591f\u7528\u5230\u7684\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u6765\u8ba9\u7cfb\u7edf\u66f4\u52a0\u5b89\u5168\u3002<\/p>\n<h3 id=\\\"auto-id-18\\\">1.5.2 chroot\u505a\u4ec0\u4e48<\/h3>\n<p>\u5c06\u00a0SSH\u00a0\u7528\u6237\u4f1a\u8bdd\u9650\u5236\u8bbf\u95ee\u5230\u7279\u5b9a\u7684\u76ee\u5f55\u5185\uff0c\u7279\u522b\u662f\u5728\u00a0web\u00a0\u670d\u52a1\u5668\u4e0a\uff0c\u8fd9\u6837\u505a\u6709\u591a\u4e2a\u539f\u56e0\uff0c\u4f46\u6700\u663e\u800c\u6613\u89c1\u7684\u662f\u4e3a\u4e86\u7cfb\u7edf\u5b89\u5168\u3002\u4e3a\u4e86\u9501\u5b9a\u00a0SSH\u00a0\u7528\u6237\u5728\u67d0\u4e2a\u76ee\u5f55\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u00a0chroot\u00a0\u673a\u5236\u3002<br \/>\n\u5728\u8bf8\u5982\u00a0Linux\u00a0\u4e4b\u7c7b\u7684\u7c7b\u00a0Unix\u00a0\u7cfb\u7edf\u4e2d\u66f4\u6539\u00a0root\uff08chroot\uff09\u662f\u5c06\u7279\u5b9a\u7528\u6237\u64cd\u4f5c\u4e0e\u5176\u4ed6\u00a0Linux\u00a0\u7cfb\u7edf\u5206\u79bb\u7684\u4e00\u79cd\u624b\u6bb5\uff1b\u4f7f\u7528\u79f0\u4e3a\u00a0chrooted\u00a0\u76d1\u72f1\u00a0\u7684\u65b0\u6839\u76ee\u5f55\u66f4\u6539\u5f53\u524d\u8fd0\u884c\u7684\u7528\u6237\u8fdb\u7a0b\u53ca\u5176\u5b50\u8fdb\u7a0b\u7684\u660e\u663e\u6839\u76ee\u5f55\u3002<br \/>\nchroot\u7cfb\u7edf\u8c03\u7528\u5c06\u5f53\u524d\u8fdb\u7a0b\u53ca\u5176\u5b50\u8fdb\u7a0b\u7684root\u76ee\u5f55\u4fee\u6539\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u8def\u5f84\uff0c\u901a\u5e38\u662f\u5728\u6587\u4ef6\u7cfb\u7edf\u771f\u6b63\u7684root\u76ee\u5f55\u4e0b\u7684\u4e00\u4e9b\u53d7\u9650\u7684\u5b50\u76ee\u5f55\u4e2d\u3002\u8fdb\u7a0b\u8ba4\u4e3a\u65b0\u7684\u8def\u5f84\u5c31\u662f\u7cfb\u7edf\u7684\u201c\/\u201d\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u8fd9\u4e2a\u53d7\u9650\u5236\u7684\u73af\u5883\u79f0\u4e3a\u201c\u76d1\u72f1\u201d\u3002\u9664\u975e\u4e00\u4e9b\u7279\u6b8a\u624b\u6bb5\uff0c\u8981\u4ece\u76d1\u72f1\u91cc\u9762\u9003\u51fa\u6765\u662f\u6839\u672c\u4e0d\u53ef\u80fd\u7684\u3002<br \/>\nchroot\u7cfb\u7edf\u8c03\u7528\u5b58\u5728\u4e8e\u6240\u6709\u5df2\u77e5\u7684UNIX\u7248\u672c\u4e2d\uff0c\u5b83\u80fd\u591f\u4e3a\u8fd0\u884c\u7684\u8fdb\u7a0b\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6839\u76ee\u5f55\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5c06\u4e00\u4e2a\u53d7\u9650\u5236\u7684\u6587\u4ef6\u7cfb\u7edf\uff08\u6bd4\u5982\uff0c\/chroot\/named)\u4f5c\u4e3a\u8fdb\u7a0b\u53ef\u89c1\u7684\u6700\u4e0a\u5c42\u76ee\u5f55\u3002<\/p>\n<h3 id=\\\"auto-id-19\\\">1.5.3\u00a0\u521b\u5efa\u00a0SSH chroot\u00a0\u76d1\u72f1<\/h3>\n<p>1\u3001\u4f7f\u7528\u00a0mkdir\u00a0\u547d\u4ee4\u5f00\u59cb\u521b\u5efa\u00a0chroot\u00a0\u76d1\u72f1<\/p>\n<div>\n<pre>[root@clsn ~]# mkdir -p \/opt\/clsn<\/pre>\n<\/div>\n<p>2\u3001\u6839\u636e\u00a0sshd_config\u00a0\u624b\u518c\u627e\u5230\u6240\u9700\u7684\u6587\u4ef6\uff0cChrootDirectory\u00a0\u9009\u9879\u6307\u5b9a\u5728\u8eab\u4efd\u9a8c\u8bc1\u540e\u8981\u00a0chroot\u00a0\u5230\u7684\u76ee\u5f55\u7684\u8def\u5f84\u540d\u3002\u8be5\u76ee\u5f55\u5fc5\u987b\u5305\u542b\u652f\u6301\u7528\u6237\u4f1a\u8bdd\u6240\u5fc5\u9700\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002<br \/>\n\u5bf9\u4e8e\u4ea4\u4e92\u5f0f\u4f1a\u8bdd\uff0c\u8fd9\u9700\u8981\u81f3\u5c11\u4e00\u4e2a\u00a0shell\uff0c\u901a\u5e38\u4e3a\u00a0sh\u00a0\u548c\u57fa\u672c\u7684\u00a0\/dev\u00a0\u8282\u70b9\uff0c\u4f8b\u5982\u00a0null\u3001zero\u3001stdin\u3001stdout\u3001stderr\u00a0\u548c\u00a0tty\u00a0\u8bbe\u5907<\/p>\n<div>\n<pre>[root@clsn ~]# ls -l \/dev\/{null,zero,stdin,stdout,stderr,random,tty}\ncrw-rw-rw- 1 root root 1, 3 Jan 31 13:24 \/dev\/null\ncrw-rw-rw- 1 root root 1, 8 Jan 31 13:24 \/dev\/random\nlrwxrwxrwx 1 root root   15 Jan 31  2018 \/dev\/stderr -&gt; \/proc\/self\/fd\/2\nlrwxrwxrwx 1 root root   15 Jan 31  2018 \/dev\/stdin -&gt; \/proc\/self\/fd\/0\nlrwxrwxrwx 1 root root   15 Jan 31  2018 \/dev\/stdout -&gt; \/proc\/self\/fd\/1\ncrw-rw-rw- 1 root tty  5, 0 Jan 31 13:24 \/dev\/tty\ncrw-rw-rw- 1 root root 1, 5 Jan 31 13:24 \/dev\/zero<\/pre>\n<\/div>\n<p>3\u3001\u4f7f\u7528\u00a0mknod\u00a0\u547d\u4ee4\u521b\u5efa\u00a0\/dev\u00a0\u4e0b\u7684\u6587\u4ef6\u3002\u5728\u4e0b\u9762\u7684\u547d\u4ee4\u4e2d\uff0c-m\u00a0\u6807\u5fd7\u7528\u6765\u6307\u5b9a\u6587\u4ef6\u6743\u9650\u4f4d\uff0cc\u00a0\u610f\u601d\u662f\u5b57\u7b26\u6587\u4ef6\uff0c\u4e24\u4e2a\u6570\u5b57\u5206\u522b\u662f\u6587\u4ef6\u6307\u5411\u7684\u4e3b\u8981\u53f7\u548c\u6b21\u8981\u53f7\u3002<\/p>\n<div>\n<pre>[root@clsn ~]# mkdir -p \/opt\/clsn\/dev\n[root@clsn ~]# cd \/opt\/clsn\/dev\n[root@clsn dev]# mknod -m 666 null c 1 3\n[root@clsn dev]# mknod -m 666 tty c 5 0\n[root@clsn dev]# mknod -m 666 zero c 1 5\n[root@clsn dev]# mknod -m 666 random c 1 8<\/pre>\n<\/div>\n<p>4\u3001\u5728\u00a0chroot\u00a0\u76d1\u72f1\u4e2d\u8bbe\u7f6e\u5408\u9002\u7684\u6743\u9650\u3002\u6ce8\u610f\u00a0chroot\u00a0\u76d1\u72f1\u548c\u5b83\u7684\u5b50\u76ee\u5f55\u4ee5\u53ca\u5b50\u6587\u4ef6\u5fc5\u987b\u88ab\u00a0root\u00a0\u7528\u6237\u6240\u6709\uff0c\u5e76\u4e14\u5bf9\u666e\u901a\u7528\u6237\u6216\u7528\u6237\u7ec4\u4e0d\u53ef\u5199<\/p>\n<div>\n<pre>[root@clsn dev]# chown root.root \/opt\/clsn\n[root@clsn dev]# chmod 0755 \/opt\/clsn\n[root@clsn dev]# ls -ld \/opt\/clsn\ndrwxr-xr-x 3 root root 4096 Jan 31 13:36 \/opt\/clsn<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-20\\\">1.5.4\u00a0\u4e3a\u00a0SSH chroot\u00a0\u76d1\u72f1\u8bbe\u7f6e\u4ea4\u4e92\u5f0f\u00a0shell<\/h3>\n<p>1\u3001\u521b\u5efa\u00a0bin\u00a0\u76ee\u5f55\u5e76\u590d\u5236\u00a0\/bin\/bash\u00a0\u5230\u00a0bin\u00a0\u4e2d<\/p>\n<div>\n<pre>[root@clsn dev]# mkdir -p \/opt\/clsn\/bin\n[root@clsn dev]# cp -v \/bin\/bash  \/opt\/clsn\/bin\/\n`\/bin\/bash\\' -&gt; `\/opt\/clsn\/bin\/bash\\'<\/pre>\n<\/div>\n<p>2\u3001\u8bc6\u522b\u00a0bash\u00a0\u6240\u9700\u7684\u5171\u4eab\u5e93\uff0c\u5982\u4e0b\u6240\u793a\u590d\u5236\u5b83\u4eec\u5230\u00a0lib64\u00a0\u4e2d<\/p>\n<div>\n<pre>[root@clsn dev]# ldd \/bin\/bash\n    linux-vdso.so.1 =&gt;  (0x00007fffb68fb000)\n    libtinfo.so.5 =&gt; \/lib64\/libtinfo.so.5 (0x0000003600a00000)\n    libdl.so.2 =&gt; \/lib64\/libdl.so.2 (0x00000035fee00000)\n    libc.so.6 =&gt; \/lib64\/libc.so.6 (0x00000035ff200000)\n    \/lib64\/ld-linux-x86-64.so.2 (0x00000035fea00000)\n[root@clsn dev]# mkdir -p \/opt\/clsn\/lib64\n[root@clsn dev]# cp -v \/lib64\/{libtinfo.so.5,libdl.so.2,libc.so.6,ld-linux-x86-64.so.2} \/opt\/clsn\/lib64\/\n`\/lib64\/libtinfo.so.5\\' -&gt; `\/opt\/clsn\/lib64\/libtinfo.so.5\\'\n`\/lib64\/libdl.so.2\\' -&gt; `\/opt\/clsn\/lib64\/libdl.so.2\\'\n`\/lib64\/libc.so.6\\' -&gt; `\/opt\/clsn\/lib64\/libc.so.6\\'\n`\/lib64\/ld-linux-x86-64.so.2\\' -&gt; `\/opt\/clsn\/lib64\/ld-linux-x86-64.so.2\\'<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-21\\\">1.5.5\u00a0\u521b\u5efa\u5e76\u914d\u7f6e\u00a0SSH\u00a0\u7528\u6237<\/h3>\n<p>1\u3001\u4f7f\u7528\u00a0useradd\u00a0\u547d\u4ee4\u521b\u5efa\u00a0SSH\u00a0\u7528\u6237\uff0c\u5e76\u8bbe\u7f6e\u5b89\u5168\u5bc6\u7801<\/p>\n<div>\n<pre>[root@clsn dev]# useradd  clsn\n[root@clsn dev]# echo 123456|passwd --stdin clsn\nChanging password for user clsn.\npasswd: all authentication tokens updated successfully.<\/pre>\n<\/div>\n<p>2\u3001\u521b\u5efa\u00a0chroot\u00a0\u76d1\u72f1\u901a\u7528\u914d\u7f6e\u76ee\u5f55\u00a0\/opt\/etc\u00a0\u5e76\u590d\u5236\u5df2\u66f4\u65b0\u7684\u8d26\u53f7\u6587\u4ef6\uff08\/etc\/passwd\u00a0\u548c\u00a0\/etc\/group\uff09\u5230\u8fd9\u4e2a\u76ee\u5f55\u4e2d<\/p>\n<div>\n<pre>[root@clsn dev]# mkdir -p \/opt\/clsn\/etc\n[root@clsn dev]# cp -vf \/etc\/{passwd,group} \/opt\/clsn\/etc\/\n`\/etc\/passwd\\' -&gt; `\/opt\/clsn\/etc\/passwd\\'\n`\/etc\/group\\' -&gt; `\/opt\/clsn\/etc\/group\\'<\/pre>\n<\/div>\n<p>\u6ce8\u610f\uff1a\u6bcf\u6b21\u5411\u7cfb\u7edf\u6dfb\u52a0\u66f4\u591a\u00a0SSH\u00a0\u7528\u6237\u65f6\uff0c\u90fd\u9700\u8981\u5c06\u66f4\u65b0\u7684\u5e10\u6237\u6587\u4ef6\u590d\u5236\u5230\u00a0\/opt\/clsn\/etc\/\u00a0\u76ee\u5f55\u4e2d\u3002<\/p>\n<h3 id=\\\"auto-id-22\\\">1.5.6\u00a0\u914d\u7f6e\u00a0SSH\u00a0\u6765\u4f7f\u7528\u00a0chroot\u00a0\u76d1\u72f1<\/h3>\n<p>1\u3001\u914d\u7f6esshd\u914d\u7f6e\u6587\u4ef6\uff0c\/etc\/ssh\/sshd_config<\/p>\n<div>\n<pre>cat &gt;&gt;\/etc\/ssh\/sshd_config &lt;&lt;\\'EOF\\'\nMatch User clsn\nChrootDirectory \/opt\/clsn\nEOF<\/pre>\n<\/div>\n<p>\u914d\u7f6e\u53c2\u6570\u8bf4\u660e<\/p>\n<div>\n<pre># \u5b9a\u4e49\u8981\u4f7f\u7528 chroot \u76d1\u72f1\u7684\u7528\u6237\nMatch User clsn\n# \u6307\u5b9a chroot \u76d1\u72f1\nChrootDirectory \/opt\/clsn<\/pre>\n<\/div>\n<p>2\u3001\u91cd\u65b0\u542f\u52a8sshd\u670d\u52a1<\/p>\n<div>\n<pre>[root@clsn dev]# \/etc\/init.d\/sshd restart\nStopping sshd:                                             [  OK  ]\nStarting sshd:                                             [  OK  ]<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-23\\\">1.5.7\u00a0\u6d4b\u8bd5\u00a0SSH\u00a0\u7684\u00a0chroot\u00a0\u76d1\u72f1<\/h3>\n<div>\n<pre>[C:\\\\]$ ssh clsn@10.0.0.188\nConnecting to 10.0.0.188:22...\nConnection established.\nTo escape to local shell, press \\'Ctrl+Alt+]\\'.\nLast login: Wed Jan 31 13:49:31 2018 from mirrors.aliyuncs.com\n-bash-4.1$\n-bash-4.1$ ls\n-bash: ls: command not found\n-bash-4.1$ date\n-bash: date: command not found<\/pre>\n<\/div>\n<p>\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa\u00a0SSH\u00a0\u7528\u6237\u88ab\u9501\u5b9a\u5728\u4e86\u00a0chroot\u00a0\u76d1\u72f1\u4e2d\uff0c\u5e76\u4e14\u4e0d\u80fd\u4f7f\u7528\u4efb\u4f55\u5916\u90e8\u547d\u4ee4\u5982\uff08ls\u3001date\u7b49\u7b49\uff09\u3002<br \/>\n\u7528\u6237\u53ea\u53ef\u4ee5\u6267\u884c\u00a0bash\u00a0\u4ee5\u53ca\u5b83\u5185\u7f6e\u7684\u547d\u4ee4\uff08\u6bd4\u5982\uff1apwd\u3001history\u3001echo\u00a0\u7b49\u7b49\uff09\uff1a<\/p>\n<div>\n<pre>[C:\\\\]$ ssh clsn@10.0.0.188\nConnecting to 10.0.0.188:22...\nConnection established.\nTo escape to local shell, press \\'Ctrl+Alt+]\\'.\nLast login: Wed Jan 31 13:49:31 2018 from mirrors.aliyuncs.com\n-bash-4.1$\n-bash-4.1$ ls\n-bash: ls: command not found\n-bash-4.1$ date\n-bash: date: command not found<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-24\\\">1.5.8\u00a0\u521b\u5efa\u7528\u6237\u7684\u4e3b\u76ee\u5f55\u5e76\u6dfb\u52a0\u00a0Linux\u00a0\u547d\u4ee4<\/h3>\n<p>1\u3001\u4ece\u524d\u9762\u7684\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7528\u6237\u88ab\u9501\u5b9a\u5728\u4e86\u6839\u76ee\u5f55\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u00a0SSH\u00a0\u7528\u6237\u521b\u5efa\u4e00\u4e2a\u4e3b\u76ee\u5f55\uff08\u4e3a\u6240\u6709\u5c06\u6765\u7684\u7528\u6237\u53ef\u4ee5\u8fd9\u4e48\u505a\uff09<\/p>\n<div>\n<pre>[root@clsn dev]# mkdir -p \/opt\/clsn\/home\/clsn\n[root@clsn dev]# chown -R clsn.clsn \/opt\/clsn\/home\/clsn\n[root@clsn dev]# chmod -R 0700 \/opt\/clsn\/home\/clsn<\/pre>\n<\/div>\n<p>2\u3001\u5c06\u5728\u00a0bin\u00a0\u76ee\u5f55\u4e2d\u590d\u5236\u8fdb\u51e0\u4e2a\u7528\u6237\u547d\u4ee4\uff0c\u5982\u00a0ls\u3001date\u3001mkdir<\/p>\n<div>\n<pre>[root@clsn dev]# cp -v \/bin\/ls \/opt\/clsn\/bin\/\n`\/bin\/ls\\' -&gt; `\/opt\/clsn\/bin\/ls\\'\n[root@clsn dev]# cp -v \/bin\/date \/opt\/clsn\/bin\/\n`\/bin\/date\\' -&gt; `\/opt\/clsn\/bin\/date\\'\n[root@clsn dev]# cp -v \/bin\/mkdir \/opt\/clsn\/bin\/\n`\/bin\/mkdir\\' -&gt; `\/opt\/clsn\/bin\/mkdir\\'<\/pre>\n<\/div>\n<p>3\u3001\u63a5\u4e0b\u6765\uff0c\u68c0\u67e5\u4e0a\u9762\u547d\u4ee4\u7684\u5171\u4eab\u5e93\u5e76\u5c06\u5b83\u4eec\u79fb\u5230\u00a0chroot\u00a0\u76d1\u72f1\u7684\u5e93\u76ee\u5f55\u4e2d<br \/>\n\u67e5\u770bls\u547d\u4ee4\u5171\u4eab\u5e93<\/p>\n<div>\n<pre>[root@clsn dev]# ldd \/bin\/ls\n    linux-vdso.so.1 =&gt;  (0x00007ffe47abf000)\n    libselinux.so.1 =&gt; \/lib64\/libselinux.so.1 (0x0000003600600000)\n    librt.so.1 =&gt; \/lib64\/librt.so.1 (0x00000035ffa00000)\n    libcap.so.2 =&gt; \/lib64\/libcap.so.2 (0x0000003605200000)\n    libacl.so.1 =&gt; \/lib64\/libacl.so.1 (0x0000003604600000)\n    libc.so.6 =&gt; \/lib64\/libc.so.6 (0x00000035ff200000)\n    libdl.so.2 =&gt; \/lib64\/libdl.so.2 (0x00000035fee00000)\n    \/lib64\/ld-linux-x86-64.so.2 (0x00000035fea00000)\n    libpthread.so.0 =&gt; \/lib64\/libpthread.so.0 (0x00000035ff600000)\n    libattr.so.1 =&gt; \/lib64\/libattr.so.1 (0x0000003602e00000)<\/pre>\n<\/div>\n<p>\u590d\u5236\u5171\u4eab\u5e93\u6587\u4ef6<\/p>\n<div>\n<pre>[root@clsn dev]# \\\\cp -v \/lib64\/{libselinux.so.1,libcap.so.2,libacl.so.1,libc.so.6,libdl.so.2,ld-linux-x86-64.so.2,libattr.so.1,libpthread.so.0,librt.so.1} \/opt\/clsn\/lib64\/\n`\/lib64\/libselinux.so.1\\' -&gt; `\/opt\/clsn\/lib64\/libselinux.so.1\\'\n`\/lib64\/libcap.so.2\\' -&gt; `\/opt\/clsn\/lib64\/libcap.so.2\\'\n`\/lib64\/libacl.so.1\\' -&gt; `\/opt\/clsn\/lib64\/libacl.so.1\\'\n`\/lib64\/libc.so.6\\' -&gt; `\/opt\/clsn\/lib64\/libc.so.6\\'\n`\/lib64\/libdl.so.2\\' -&gt; `\/opt\/clsn\/lib64\/libdl.so.2\\'\n`\/lib64\/ld-linux-x86-64.so.2\\' -&gt; `\/opt\/clsn\/lib64\/ld-linux-x86-64.so.2\\'\ncp: cannot create regular file `\/opt\/clsn\/lib64\/ld-linux-x86-64.so.2\\': Text file busy\n`\/lib64\/libattr.so.1\\' -&gt; `\/opt\/clsn\/lib64\/libattr.so.1\\'\n`\/lib64\/libpthread.so.0\\' -&gt; `\/opt\/clsn\/lib64\/libpthread.so.0\\'<\/pre>\n<\/div>\n<h3 id=\\\"auto-id-25\\\">1.5.9\u00a0\u6d4b\u8bd5\u00a0sftp\u00a0\u7684\u00a0\u7528\u00a0chroot\u00a0\u76d1\u72f1<\/h3>\n<p>1\u3001\u7528\u00a0sftp\u00a0\u505a\u4e00\u4e2a\u6d4b\u8bd5\uff1b\u6d4b\u8bd5\u4f60\u5148\u524d\u5b89\u88c5\u7684\u547d\u4ee4\u662f\u5426\u53ef\u7528\u3002<br \/>\n\u5728\u00a0\/etc\/ssh\/sshd_config\u00a0\u4e2d\u6dfb\u52a0\u4e0b\u9762\u7684\u884c<\/p>\n<div>\necho \\&#8217;ForceCommand internal-sftp\\&#8217;\u00a0 &gt;&gt; \/etc\/ssh\/sshd_config\n<\/div>\n<p>\u91cd\u542f sshd \u670d\u52a1<\/p>\n<div>\n<pre>[root@clsn dev]# \/etc\/init.d\/sshd restart\nStopping sshd:                                             [  OK  ]\nStarting sshd:                                             [  OK  ]<\/pre>\n<\/div>\n<p>2\u3001\u73b0\u5728\u4f7f\u7528\u00a0ssh\u00a0\u6d4b\u8bd5\uff0c\u4f60\u4f1a\u5f97\u5230\u4e0b\u9762\u7684\u9519\u8bef\uff1a<\/p>\n<div>\n<pre>[root@lx ~]# ssh clsn@10.0.0.188\nclsn@10.0.0.188\\'s password:\nThis service allows sftp connections only.\nConnection to 10.0.0.188 closed.<\/pre>\n<\/div>\n<p>3\u3001\u8bd5\u4e0b\u4f7f\u7528\u00a0sftp<br \/>\n\u590d\u5236sftp\u7a0b\u5e8f<\/p>\n<div>\n<pre>[root@clsn ~]# cp -v  \/usr\/libexec\/openssh\/sftp-server \/opt\/clsn\/usr\/libexec\/openssh\/\n`\/usr\/libexec\/openssh\/sftp-server\\' -&gt; `\/opt\/clsn\/usr\/libexec\/openssh\/sftp-server\\'<\/pre>\n<\/div>\n<p>sftp\u4f20\u8f93\u6587\u4ef6\u6d4b\u8bd5<\/p>\n<div>\n<pre>[root@lx ~]# sftp clsn@10.0.0.188\nclsn@10.0.0.188\\'s password:\nConnected to 10.0.0.188.\nsftp&gt; pwd\nRemote working directory: \/home\/clsn\nsftp&gt; mkdir -p nmtui\nsftp&gt; cd nmtui\/\nsftp&gt; put  anaconda-ks.cfg\nUploading anaconda-ks.cfg to \/home\/clsn\/nmtui\/anaconda-ks.cfg\nanaconda-ks.cfg                                   100% 1207     1.2KB\/s   00:00<\/pre>\n<\/div>\n<p>\u5230\u8fdc\u7aef\u8fdb\u884c\u67e5\u770b<\/p>\n<div>\n<pre>[root@clsn ~]# ll \/opt\/clsn\/home\/clsn\/nmtui\/\ntotal 4\n-rw------- 1 clsn clsn 1207 Jan 31 14:21 anaconda-ks.cfg<\/pre>\n<\/div>\n<p>\u81f3\u6b64chroot\u00a0\u76d1\u72f1\u5c31\u914d\u7f6e\u5b8c\u6210\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1.1 LXC\u662f\u4ec0\u4e48\uff1f 1.1.1\u00a0\u5173\u4e8eLXC LXC\uff0c\u5176\u540d\u79f0\u6765\u81eaLinux\u8f6f\u4ef6\u5bb9\u5668\uff08Linux Contai &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=5415\"> <span class=\"screen-reader-text\">LXC \u5bb9\u5668\u96c6chroot\u4f7f\u7528\u8bf4\u660e<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[108,123],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"1.1 LXC\u662f\u4ec0\u4e48\uff1f 1.1.1\u00a0\u5173\u4e8eLXC LXC\uff0c\u5176\u540d\u79f0\u6765\u81eaLinux\u8f6f\u4ef6\u5bb9\u5668\uff08Linux Contai&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5415"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5415"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5415\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5415"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}