{"id":3275,"date":"2018-03-27T15:08:52","date_gmt":"2018-03-27T07:08:52","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=3275"},"modified":"2025-01-27T14:19:05","modified_gmt":"2025-01-27T06:19:05","slug":"linux-chroot%e5%91%bd%e4%bb%a4%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/jishu\/linux-chroot%e5%91%bd%e4%bb%a4%e8%af%a6%e8%a7%a3\/","title":{"rendered":"linux chroot\u547d\u4ee4\u8be6\u89e3"},"content":{"rendered":"\n<p>chroot\uff0c\u5373 change root directory (\u66f4\u6539 root \u76ee\u5f55)\u3002\u5728 linux \u7cfb\u7edf\u4e2d\uff0c\u7cfb\u7edf\u9ed8\u8ba4\u7684\u76ee\u5f55\u7ed3\u6784\u90fd\u662f\u4ee5 \/\uff0c\u5373\u4ee5\u6839 (root) \u5f00\u59cb\u7684\u3002\u800c\u5728\u4f7f\u7528 chroot \u4e4b\u540e\uff0c\u7cfb\u7edf\u7684\u76ee\u5f55\u7ed3\u6784\u5c06\u4ee5\u6307\u5b9a\u7684\u4f4d\u7f6e\u4f5c\u4e3a \/ \u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p><strong>\u57fa\u672c\u8bed\u6cd5<\/strong><\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">chroot NEWROOT [COMMAND [ARG]...]<\/pre>\n<\/div>\n\n\n\n<p>\u5177\u4f53\u7528\u6cd5\u8bf7\u53c2\u8003\u672c\u6587\u7684 demo\u3002<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528 chroot \u547d\u4ee4<\/strong><\/p>\n\n\n\n<p><strong>\u589e\u52a0\u4e86\u7cfb\u7edf\u7684\u5b89\u5168\u6027\uff0c\u9650\u5236\u4e86\u7528\u6237\u7684\u6743\u529b\uff1a<\/strong><\/p>\n\n\n\n<p>\u5728\u7ecf\u8fc7 chroot \u4e4b\u540e\uff0c\u5728\u65b0\u6839\u4e0b\u5c06\u8bbf\u95ee\u4e0d\u5230\u65e7\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u7ed3\u6784\u548c\u6587\u4ef6\uff0c\u8fd9\u6837\u5c31\u589e\u5f3a\u4e86\u7cfb\u7edf\u7684\u5b89\u5168\u6027\u3002\u4e00\u822c\u4f1a\u5728\u7528\u6237\u767b\u5f55\u524d\u5e94\u7528 chroot\uff0c\u628a\u7528\u6237\u7684\u8bbf\u95ee\u80fd\u529b\u63a7\u5236\u5728\u4e00\u5b9a\u7684\u8303\u56f4\u4e4b\u5185\u3002<\/p>\n\n\n\n<p><strong>\u5efa\u7acb\u4e00\u4e2a\u4e0e\u539f\u7cfb\u7edf\u9694\u79bb\u7684\u7cfb\u7edf\u76ee\u5f55\u7ed3\u6784\uff0c\u65b9\u4fbf\u7528\u6237\u7684\u5f00\u53d1\uff1a<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 chroot \u540e\uff0c\u7cfb\u7edf\u8bfb\u53d6\u7684\u662f\u65b0\u6839\u4e0b\u7684\u76ee\u5f55\u548c\u6587\u4ef6\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e0e\u539f\u7cfb\u7edf\u6839\u4e0b\u6587\u4ef6\u4e0d\u76f8\u5173\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u5728\u8fd9\u4e2a\u65b0\u7684\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u7528\u6765\u6d4b\u8bd5\u8f6f\u4ef6\u7684\u9759\u6001\u7f16\u8bd1\u4ee5\u53ca\u4e00\u4e9b\u4e0e\u7cfb\u7edf\u4e0d\u76f8\u5173\u7684\u72ec\u7acb\u5f00\u53d1\u3002<\/p>\n\n\n\n<p><strong>\u5207\u6362\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u4f4d\u7f6e\uff0c\u5f15\u5bfc Linux \u7cfb\u7edf\u542f\u52a8\u4ee5\u53ca\u6025\u6551\u7cfb\u7edf\u7b49\uff1a<\/strong><\/p>\n\n\n\n<p>chroot \u7684\u4f5c\u7528\u5c31\u662f\u5207\u6362\u7cfb\u7edf\u7684\u6839\u4f4d\u7f6e\uff0c\u800c\u8fd9\u4e2a\u4f5c\u7528\u6700\u4e3a\u660e\u663e\u7684\u662f\u5728\u7cfb\u7edf\u521d\u59cb\u5f15\u5bfc\u78c1\u76d8\u7684\u5904\u7406\u8fc7\u7a0b\u4e2d\u4f7f\u7528\uff0c\u4ece\u521d\u59cb RAM \u78c1\u76d8 (initrd) \u5207\u6362\u7cfb\u7edf\u7684\u6839\u4f4d\u7f6e\u5e76\u6267\u884c\u771f\u6b63\u7684 init\uff0c\u672c\u6587\u7684\u6700\u540e\u4e00\u4e2a demo \u4f1a\u8be6\u7ec6\u7684\u4ecb\u7ecd\u8fd9\u79cd\u7528\u6cd5\u3002<\/p>\n\n\n\n<p><strong>\u901a\u8fc7 chroot \u8fd0\u884c busybox \u5de5\u5177<\/strong><\/p>\n\n\n\n<p>busybox \u5305\u542b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u4e9b\u5de5\u5177\u653e\u7f6e\u5728\u4e00\u4e2a\u76ee\u5f55\u4e0b\uff0c\u7136\u540e\u901a\u8fc7 chroot \u6784\u9020\u51fa\u4e00\u4e2a mini \u7cfb\u7edf\u3002\u7b80\u5355\u8d77\u89c1\u6211\u4eec\u76f4\u63a5\u4f7f\u7528 docker \u7684 busybox \u955c\u50cf\u6253\u5305\u7684\u6587\u4ef6\u7cfb\u7edf\u3002\u5148\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55 rootfs\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ mkdir rootfs<\/pre>\n<\/div>\n\n\n\n<p>\u7136\u540e\u628a busybox \u955c\u50cf\u4e2d\u7684\u6587\u4ef6\u91ca\u653e\u5230\u8fd9\u4e2a\u76ee\u5f55\u4e2d\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ (docker export $(docker create busybox) | tar -C rootfs -xvf -)<\/pre>\n<\/div>\n\n\n\n<p>\u901a\u8fc7 ls \u547d\u4ee4\u67e5\u770b rootfs \u6587\u4ef6\u5939\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ ls rootfs<\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/rootfs.png\"><img loading=\"lazy\" decoding=\"async\" width=\"469\" height=\"37\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/rootfs.png\" alt=\"\" class=\"wp-image-9914\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/rootfs.png 469w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/rootfs-300x24.png 300w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4e07\u4e8b\u4ff1\u5907\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\uff01<\/p>\n\n\n\n<p>\u6267\u884c chroot \u540e\u7684 ls \u547d\u4ee4<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ sudo chroot rootfs \/bin\/ls<\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot.png\"><img loading=\"lazy\" decoding=\"async\" width=\"521\" height=\"35\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot.png\" alt=\"\" class=\"wp-image-9916\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot.png 521w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-300x20.png 300w\" sizes=\"auto, (max-width: 521px) 100vw, 521px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u867d\u7136\u8f93\u51fa\u7ed3\u679c\u4e0e\u521a\u624d\u6267\u884c\u7684 ls rootfs \u547d\u4ee4\u5f62\u540c\uff0c\u4f46\u662f\u8fd9\u6b21\u8fd0\u884c\u7684\u547d\u4ee4\u5374\u662f rootfs\/bin\/ls\u3002<\/p>\n\n\n\n<p><strong>\u8fd0\u884c chroot \u540e\u7684 pwd \u547d\u4ee4<\/strong><\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ sudo chroot rootfs \/bin\/pwd<\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/pwd.png\"><img loading=\"lazy\" decoding=\"async\" width=\"412\" height=\"38\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/pwd.png\" alt=\"\" class=\"wp-image-9917\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/pwd.png 412w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/pwd-300x28.png 300w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u54c8\uff0cpwd \u547d\u4ee4\u771f\u628a rootfs \u76ee\u5f55\u5f53\u6839\u76ee\u5f55\u4e86\uff01<\/p>\n\n\n\n<p><strong>\u4e0d\u5e26\u547d\u4ee4\u6267\u884c chroot<\/strong><\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ sudo chroot rootfs<\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-rootfs.png\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"34\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-rootfs.png\" alt=\"\" class=\"wp-image-9918\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-rootfs.png 620w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-rootfs-300x16.png 300w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd9\u6b21\u51fa\u9519\u4e86\uff0c\u56e0\u4e3a\u627e\u4e0d\u5230 \/bin\/bash\u3002\u6211\u4eec\u77e5\u9053 busybox \u4e2d\u662f\u4e0d\u5305\u542b bash \u7684\uff0c\u4f46\u662f chroot \u547d\u4ee4\u4e3a\u4ec0\u4e48\u4f1a\u627e bash \u547d\u4ee4\u5462\uff1f \u539f\u6765\uff0c\u5982\u679c\u4e0d\u7ed9 chroot \u6307\u5b9a\u6267\u884c\u7684\u547d\u4ee4\uff0c\u9ed8\u8ba4\u5b83\u4f1a\u6267\u884c &#8216;${SHELL} -i&#8217;\uff0c\u800c\u6211\u7684\u7cfb\u7edf\u4e2d ${SHELL} \u4e3a \/bin\/bash\u3002<\/p>\n\n\n\n<p>\u65e2\u7136 busybox \u4e2d\u6ca1\u6709 bash\uff0c\u6211\u4eec\u53ea\u597d\u6307\u5b9a \/bin\/sh \u6765\u6267\u884c shell \u4e86\u3002<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ sudo chroot rootfs \/bin\/sh<\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/bash.png\"><img loading=\"lazy\" decoding=\"async\" width=\"409\" height=\"54\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/bash.png\" alt=\"\" class=\"wp-image-9919\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/bash.png 409w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/bash-300x40.png 300w\" sizes=\"auto, (max-width: 409px) 100vw, 409px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd0\u884c sh \u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u5e76\u4e14\u6211\u4eec\u6253\u5370\u51fa\u4e86\u5f53\u524d\u8fdb\u7a0b\u7684 PID\u3002<\/p>\n\n\n\n<p><strong>\u68c0\u67e5\u7a0b\u5e8f\u662f\u5426\u8fd0\u884c\u5728 chroot \u73af\u5883\u4e0b<\/strong><\/p>\n\n\n\n<p>\u867d\u7136\u6211\u4eec\u505a\u4e86\u597d\u51e0\u4e2a\u5b9e\u9a8c\uff0c\u4f46\u662f\u80af\u5b9a\u4f1a\u6709\u670b\u53cb\u5fc3\u5b58\u7591\u95ee\uff0c\u600e\u4e48\u80fd\u8bc1\u660e\u6211\u4eec\u8fd0\u884c\u7684\u547d\u4ee4\u5c31\u662f\u5728 chroot \u76ee\u5f55\u540e\u7684\u8def\u5f84\u4e2d\u5462\uff1f<br>\u5176\u5b9e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 \/proc \u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u68c0\u67e5\u8fdb\u7a0b\u7684\u4e2d\u7684\u6839\u76ee\u5f55\uff0c\u6bd4\u5982\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u4ee3\u7801\u68c0\u67e5\u4e0a\u9762\u8fd0\u884c\u7684 \/bin\/sh \u547d\u4ee4\u7684\u6839\u76ee\u5f55(\u8bf7\u5728\u53e6\u5916\u4e00\u4e2a shell \u4e2d\u6267\u884c)\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ pid=$(pidof -s sh)\n$ sudo ls -ld \/proc\/$pid\/root<\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-proc.png\"><img loading=\"lazy\" decoding=\"async\" width=\"628\" height=\"54\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-proc.png\" alt=\"\" class=\"wp-image-9920\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-proc.png 628w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/chroot-proc-300x26.png 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8f93\u51fa\u4e2d\u7684\u5185\u5bb9\u660e\u786e\u7684\u6307\u51fa PID \u4e3a 46644 \u7684\u8fdb\u7a0b\u7684\u6839\u76ee\u5f55\u88ab\u6620\u5c04\u5230\u4e86 \/tmp\/rootfs \u76ee\u5f55\u3002<\/p>\n\n\n\n<p><strong>\u901a\u8fc7\u4ee3\u7801\u7406\u89e3 chroot \u547d\u4ee4<\/strong><\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a chroot \u7a0b\u5e8f\uff0c\u4ee3\u7801\u4e2d\u6d89\u53ca\u5230\u4e24\u4e2a\u51fd\u6570\uff0c\u5206\u522b\u662f chroot() \u51fd\u6570\u548c chdir() \u51fd\u6570\uff0c\u5176\u5b9e\u771f\u6b63\u7684 chroot \u547d\u4ee4\u4e5f\u662f\u901a\u8fc7\u8c03\u7528\u5b83\u4eec\u5b9e\u73b0\u7684\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:cpp;\">#include &lt;stdio.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;stdlib.h&gt;\n \nint main(int argc, char *argv[])\n{\n  if(argc&lt;2){\n    printf(\"Usage: chroot NEWROOT [COMMAND...] \\n\");\n    return 1;\n  }\n\n  if(chroot(argv[1])) {\n    perror(\"chroot\");\n    return 1;\n  }\n \n  if(chdir(\"\/\")) {\n    perror(\"chdir\");\n    return 1;\n  }\n \n  if(argc == 2) {\n    \/\/ hardcode \/bin\/sh for my busybox tools.\n    argv[0] = (char *)\"\/bin\/sh\";\n \n    argv[1] = (char *) \"-i\";\n    argv[2] = NULL;\n  } else {\n    argv += 2;\n  }\n \n  execvp (argv[0], argv);\n  printf(\"chroot: cannot run command `%s`\\n\", *argv);\n \n  return 0;\n}<\/pre>\n<\/div>\n\n\n\n<p>\u628a\u4e0a\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5230\u6587\u4ef6 mychroot.c \u6587\u4ef6\u4e2d\uff0c\u5e76\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:cpp;\">$ gcc -Wall mychroot.c -o mychroot<\/pre>\n<\/div>\n\n\n\n<p>mychroot \u7684\u7528\u6cd5\u548c chroot \u57fa\u672c\u76f8\u540c\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">$ sudo .\/mychroot .\/rootfs<\/pre>\n<\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/mychroot.png\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"52\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/mychroot.png\" alt=\"\" class=\"wp-image-9921\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/mychroot.png 522w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/mychroot-300x30.png 300w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u7279\u522b\u4e4b\u5904\u662f\u6211\u4eec\u7684 mychroot \u5728\u6ca1\u6709\u4f20\u9012\u547d\u4ee4\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u4e86 \/bin\/sh\uff0c\u539f\u56e0\u5f53\u7136\u662f\u4e3a\u4e86\u652f\u6301\u6211\u4eec\u7684 busybox \u5de5\u5177\u96c6\uff0c\u7b14\u8005\u5728\u4ee3\u7801\u4e2d hardcode \u4e86\u9ed8\u8ba4\u7684 shell\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:cpp;\">argv[0] = (char *)\"\/bin\/sh\";<\/pre>\n<\/div>\n\n\n\n<p>\u4ece\u4ee3\u7801\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u770b\u5230\uff0c\u5b9e\u73b0 chroot \u547d\u4ee4\u7684\u6838\u5fc3\u903b\u8f91\u5176\u5b9e\u5e76\u4e0d\u590d\u6742\u3002<\/p>\n\n\n\n<p><strong>\u5b9e\u4f8b\uff1a\u901a\u8fc7 chroot \u91cd\u65b0\u8bbe\u7f6e root \u5bc6\u7801<\/strong><\/p>\n\n\n\n<p>\u5fd8\u8bb0\u4e86 root \u5bc6\u7801\u8be5\u600e\u4e48\u529e\uff1f\u63a5\u4e0b\u6765\u7684 demo \u5c06\u6f14\u793a\u5982\u4f55\u901a\u8fc7 chroot \u547d\u4ee4\u91cd\u65b0\u8bbe\u7f6e centos7 \u4e2d\u88ab\u5fd8\u8bb0\u4e86\u7684 root \u5bc6\u7801\u3002<br>systemd \u7684\u7ba1\u7406\u673a\u5236\u4e2d\uff0crescure \u6a21\u5f0f\u548c emeryency \u6a21\u5f0f\u662f\u65e0\u6cd5\u76f4\u63a5\u53d6\u5f97 root \u6743\u9650\u7684\uff0c\u9700\u8981\u4f7f\u7528 root \u5bc6\u7801\u624d\u80fd\u8fdb\u5165 rescure \u548c emeryency \u73af\u5883\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u6765\u8bbe\u7f6e root \u5bc6\u7801\u3002\u6211\u4eec\u53ef\u4ee5\u4e3a\u5185\u6838\u7684\u542f\u52a8\u6307\u5b9a &#8220;rd.break&#8221; \u53c2\u6570\uff0c\u4ece\u800c\u8ba9\u7cfb\u7edf\u5728\u542f\u52a8\u7684\u65e9\u671f\u505c\u4e0b\u6765\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 root \u6743\u9650\u5e76\u7ed3\u5408 chroot \u547d\u4ee4\u5b8c\u6210\u8bbe\u7f6e root \u5bc6\u7801\u7684\u64cd\u4f5c\u3002\u4e0b\u9762\u6211\u4eec\u4e00\u8d77\u6765\u770b\u5177\u4f53\u7684\u64cd\u4f5c\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p>\u5728\u7cfb\u7edf\u542f\u52a8\u8fc7\u7a0b\u4e2d\u8fdb\u5165\u5f00\u673a\u83dc\u5355\u65f6\u6309\u4e0b\u5b57\u6bcd\u952e e \u8fdb\u7a0b\u5f00\u673a\u83dc\u5355\u7684\u7f16\u8f91\u6a21\u5f0f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/e.png\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"156\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/e.png\" alt=\"\" class=\"wp-image-9922\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/e.png 631w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/e-300x74.png 300w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u7cfb\u7edf\u7684\u5f00\u673a\u83dc\u5355\uff0c\u6309\u4e0b e \u540e\u8fdb\u5165\u7f16\u8f91\u754c\u9762\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/e2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"232\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/e2.png\" alt=\"\" class=\"wp-image-9923\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/e2.png 726w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/e2-300x96.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u627e\u5230\u4ee5 &#8220;linux16 \/vmlinuz-&#8221; \u5f00\u5934\u7684\u884c\u3002\u5982\u679c\u9ed8\u8ba4\u6ca1\u6709\u770b\u5230\u8be5\u884c\uff0c\u9700\u8981\u6309\u5411\u4e0b\u952e\u628a\u5b83\u6eda\u52a8\u51fa\u6765\u3002<br>\u7136\u540e\u5b9a\u4f4d\u5230\u8be5\u884c\u7ed3\u5c3e\u5904\uff0c\u8f93\u5165\u4e00\u4e2a\u7a7a\u683c\u548c\u5b57\u7b26\u4e32 &#8220;rd.break&#8221;\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/break.png\"><img loading=\"lazy\" decoding=\"async\" width=\"721\" height=\"50\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/break.png\" alt=\"\" class=\"wp-image-9924\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/break.png 721w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/break-300x21.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u63a5\u7740\u6309\u4e0b ctrl + x \u4ee5\u8be5\u8bbe\u7f6e\u7ee7\u7eed\u542f\u52a8\uff0c\u542f\u52a8\u8fc7\u7a0b\u4e2d\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u505c\u4e0b\u6765\uff0c\u8fd9\u662f\u7cfb\u7edf\u542f\u52a8\u8fc7\u7a0b\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u65e9\u7684\u65f6\u95f4\u70b9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/ctrl-x.png\"><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"230\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/ctrl-x.png\" alt=\"\" class=\"wp-image-9925\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/ctrl-x.png 644w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/ctrl-x-300x107.png 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6240\u4ee5\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u8fd8\u6302\u8f7d\u5728 RAM disk \u4e0a(\u5c31\u662f\u5185\u5b58\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf)\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 mount \u547d\u4ee4\u68c0\u67e5\u7cfb\u7edf\u5f53\u524d\u6302\u8f7d\u7684\u6587\u4ef6\u7cfb\u7edf\uff0c\u4e0b\u9762\u662f\u6211\u4eec\u6bd4\u8f83\u5173\u5fc3\u7684\u4e24\u6761\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/disk.png\"><img loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"52\" src=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/disk.png\" alt=\"\" class=\"wp-image-9926\" srcset=\"https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/disk.png 652w, https:\/\/cn.hostease.com\/xueyuan\/wp-content\/uploads\/2018\/03\/disk-300x24.png 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4e0a\u56fe\u4e2d mount \u547d\u4ee4\u8f93\u51fa\u7684\u7b2c\u4e00\u884c\u8bf4\u660e\u6b64\u65f6\u7684\u6839\u76ee\u5f55\u5728\u4e00\u4e2a RAM disk \u4e2d, \u5373 rootfs\u3002<br>\u56fe\u4e2d\u8f93\u51fa\u7684\u7b2c\u4e8c\u884c\u8bf4\u660e\u6211\u4eec\u7684\u6587\u4ef6\u7cfb\u7edf\u6b64\u65f6\u88ab\u6302\u8f7d\u5230\u4e86 \/sysroot \u76ee\u5f55\uff0c\u5e76\u4e14\u662f\u53ea\u8bfb\u7684\u6a21\u5f0f\uff1a<\/p>\n\n\n\n<div class=\"codetitle\"><a id=\"copybut16906\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n\n\n\n<div id=\"code16906\" class=\"codebody\">\/dev\/mapper\/centos-root on \/sysroot type xfs (ro,relatime,attr2,inode64,noquota)<\/div>\n\n\n\n<p>\u800c\u5728\u6211\u4eec\u6b63\u5e38\u767b\u9646\u7cfb\u7edf\u7684\u60c5\u51b5\u4e0b\uff0c\u7cfb\u7edf\u6839\u76ee\u5f55\u7684\u6302\u8f7d\u60c5\u51b5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"codetitle\"><a id=\"copybut8620\" class=\"copybut\"><\/a><u>\u590d\u5236\u4ee3\u7801<\/u>\u4ee3\u7801\u5982\u4e0b:<\/div>\n\n\n\n<div id=\"code8620\" class=\"codebody\">\/dev\/mapper\/centos-root on \/ type xfs (rw,relatime,seclabel,attr2,inode64,noquota)<\/div>\n\n\n\n<p>\u8be5\u65f6\u95f4\u70b9\u7684\u6700\u5927\u4f18\u52bf\u662f\u6211\u4eec\u5177\u6709 root \u6743\u9650\uff01\u6240\u4ee5\u8ba9\u6211\u4eec\u5f00\u59cb\u8bbe\u7f6e\u65b0\u7684 root \u5bc6\u7801\u5427\u3002<\/p>\n\n\n\n<p><strong>\u5148\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u628a \/sysroot \u91cd\u65b0\u6302\u8f7d\u4e3a\u53ef\u8bfb\u5199\u7684\u6a21\u5f0f\uff1a<\/strong><\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">switch_root:\/# mount -o remount,rw \/sysroot<\/pre>\n<\/div>\n\n\n\n<p>\u7136\u540e\u7528\u4e0b\u9762 chroot \u547d\u4ee4\u628a\u6839\u76ee\u5f55\u5207\u6362\u5230\u6211\u4eec\u539f\u6765\u7684\u73af\u5883\u4e2d\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">switch_root:\/# chroot \/sysroot<\/pre>\n<\/div>\n\n\n\n<p>\u6b64\u65f6\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a\u6211\u4eec\u4ee5 root \u6743\u9650\u767b\u5f55\u4e86\u539f\u6765\u7684\u7cfb\u7edf\uff0c\u4fee\u6539\u5bc6\u7801\u5c31\u5f88\u5bb9\u6613\u4e86\uff01\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u4e3a root \u7528\u6237\u8bbe\u7f6e\u65b0\u7684\u5bc6\u7801\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">sh-4.2# echo \"new_root_pw\" | passwd --stdin root<\/pre>\n<\/div>\n\n\n\n<p>\u63a5\u4e0b\u6765\u8fd8\u8981\u5904\u7406 SELinux \u76f8\u5173\u7684\u95ee\u9898\u3002\u7531\u4e8e\u5f53\u524d\u7684\u73af\u5883\u4e2d SELinux \u5e76\u672a\u542f\u52a8\uff0c\u6240\u4ee5\u6211\u4eec\u5bf9\u6587\u4ef6\u7684\u4fee\u6539\u53ef\u80fd\u9020\u6210\u6587\u4ef6\u7684 context \u4e0d\u6b63\u786e\u3002\u4e3a\u4e86\u786e\u4fdd\u5f00\u673a\u65f6\u91cd\u65b0\u8bbe\u5b9a SELinux context\uff0c\u5fc5\u9808\u5728\u6839\u76ee\u5f55\u4e0b\u6dfb\u52a0\u9690\u85cf\u6587\u4ef6 .autorelabel\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">sh-4.2# touch \/.autorelabel<\/pre>\n<\/div>\n\n\n\n<p>\u6700\u540e\u4ece chroot \u4e2d\u9000\u51fa\uff0c\u5e76\u91cd\u542f\u7cfb\u7edf\uff1a<\/p>\n\n\n\n<div class=\"jb51code\">\n<pre class=\"brush:bash;\">sh-4.2# exit\nswitch_root:\/# reboot<\/pre>\n<\/div>\n\n\n\n<p>\u91cd\u65b0\u8fdb\u5165\u767b\u9646\u754c\u9762\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u521a\u624d\u8bbe\u7f6e\u7684\u5bc6\u7801\u4ee5 root \u767b\u9646\u4e86\uff01<\/p>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p>chroot \u662f\u4e00\u4e2a\u5f88\u6709\u610f\u601d\u7684\u547d\u4ee4\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u6765\u7b80\u5355\u7684\u5b9e\u73b0\u6587\u4ef6\u7cfb\u7edf\u7684\u9694\u79bb\u3002\u4f46\u5728\u4e00\u4e2a\u5bb9\u5668\u6280\u672f\u7e41\u8363\u7684\u65f6\u4ee3\uff0c\u7528 chroot \u6765\u8fdb\u884c\u8d44\u6e90\u7684\u9694\u79bb\u5b9e\u5728\u662f low \u4e86\u70b9\u3002\u6240\u4ee5 chroot \u7684\u4e3b\u8981\u7528\u9014\u8fd8\u662f\u96c6\u4e2d\u5728\u7cfb\u7edf\u6551\u63f4\u3001\u7ef4\u62a4\u7b49\u4e00\u4e9b\u7279\u6b8a\u7684\u573a\u666f\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>chroot\uff0c\u5373 change root directory (\u66f4\u6539 root \u76ee\u5f55)\u3002\u5728 linux \u7cfb\u7edf\u4e2d &#8230; <a title=\"linux chroot\u547d\u4ee4\u8be6\u89e3\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/jishu\/linux-chroot%e5%91%bd%e4%bb%a4%e8%af%a6%e8%a7%a3\/\" aria-label=\"\u9605\u8bfb linux chroot\u547d\u4ee4\u8be6\u89e3\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49,5],"tags":[],"class_list":["post-3275","post","type-post","status-publish","format-standard","hentry","category-linux","category-jishu"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=3275"}],"version-history":[{"count":2,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3275\/revisions"}],"predecessor-version":[{"id":9927,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/3275\/revisions\/9927"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=3275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=3275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=3275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}