{"id":290383,"date":"2024-03-16T21:29:12","date_gmt":"2024-03-16T13:29:12","guid":{"rendered":"https:\/\/lrxjmw.cn\/lxtgmlqhgzda.html"},"modified":"2024-03-16T21:29:12","modified_gmt":"2024-03-16T13:29:12","slug":"lxtgmlqhgzda","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/lxtgmlqhgzda.html","title":{"rendered":"linux chroot Linux\u7cfb\u7edf\u6839\u76ee\u5f55\u5207\u6362\u5de5\u4f5c\u7684\u5b89\u5168\u6027\u7b49\u4f7f\u7528\u65b9\u6cd5\u4ecb\u7ecd"},"content":{"rendered":"

chroot\u5728Linux<\/a>\u7cfb\u7edf<\/a>\u4e2d\u53d1\u6325\u4e86\u6839\u76ee\u5f55\u7684\u5207\u6362\u5de5\u4f5clinux chroot<\/strong>\uff0c\u540c\u65f6\u5e26\u6765\u4e86\u7cfb\u7edf\u7684\u5b89\u5168\u6027\u7b49\u76ca\u5904\u3002\u4e0b\u8fb9\u4e3a\u4e25\u6253\u5206\u4eab\u4e00\u4e0blinuxchroot\u547d\u4ee4<\/a>\u7684\u4f7f\u7528\u6280\u5de7\u3002<\/p>\n

chroot\u7b80\u4ecb\uff1a\u4e3a\u4ec0\u4e48\u4f7f\u7528chroot<\/p>\n

\u5728\u7ecf\u8fc7chroot\u4ee5\u540e\uff0c\u7cfb\u7edf\u8bfb\u53d6\u5230\u7684\u76ee\u5f55\u548c\u6587\u4ef6\u5c06\u4e0d\u5728\u662f\u65e7\u7cfb\u7edf\u6839\u4e0b\u7684\u800c\u662f\u65b0\u6839\u4e0b(\u5373\u88ab\u6307\u5b9a\u7684\u65b0\u7684\u4f4d\u7f6e)\u7684\u76ee\u5f55\u7ed3\u6784\u548c\u6587\u4ef6\uff0c\u56e0\u800c\u5b83\u5e26\u6765\u7684\u76ca\u5904\u5927\u81f4\u6709\u4ee5\u4e0b3\u4e2a\uff1a<\/p>\n

\u964d\u4f4e\u4e86\u7cfb\u7edf\u7684\u5b89\u5168\u6027\uff0c\u9650\u5236\u4e86\u7528\u6237\u7684\u6743\u5229\uff1b<\/p>\n

\u5728\u7ecf\u8fc7chroot\u4ee5\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\u63d0\u9ad8\u4e86\u7cfb\u7edf\u7684\u5b89\u5168\u6027\u3002\u8fd9\u4e2a\u901a\u5e38\u662f\u5728\u767b\u9646(login)\u524d\u4f7f\u7528chroot\uff0c\u501f\u6b64\u8fbe\u5230\u7528\u6237\u4e0d\u80fd\u8bbf\u95ee\u4e00\u4e9b\u7279\u5b9a\u7684\u6587\u4ef6\u3002<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

\u6784\u5efa\u4e00\u4e2a\u4e0e\u539f\u7cfb\u7edf\u9694\u79bb\u7684\u7cfb\u7edf\u76ee\u5f55\u7ed3\u6784\u5d4c\u5165\u5f0flinux\u57f9\u8bad\uff0c\u4fbf\u6377\u7528\u6237\u7684\u5f00\u53d1\uff1b<\/p>\n

\u4f7f\u7528chroot\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\u4e2dredflag linux\uff0c\u53ef\u4ee5\u62ff\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

\u5207\u6362\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u4f4d\u7f6e\uff0c\u5f15\u5bfcLinux\u7cfb\u7edf\u542f\u52a8\u4ee5\u53ca\u6025\u6551\u7cfb\u7edf\u7b49\u3002<\/p>\n

chroot\u7684\u4f5c\u7528\u5c31\u662f\u5207\u6362\u7cfb\u7edf\u7684\u6839\u4f4d\u7f6e\uff0c\u800c\u8fd9\u4e2a\u4f5c\u7528\u6700\u4e3a\u663e\u8457\u7684\u662f\u5728\u7cfb\u7edf\u521d\u59cb\u5f15\u5bfcc\u76d8\u7684\u5904\u7406\u8fc7\u7a0b\u4e2d\u4f7f\u7528\uff0c\u4ece\u521d\u59cbRAMc\u76d8(initrd)\u5207\u6362\u7cfb\u7edf\u7684\u6839\u4f4d\u7f6e\u5e76\u6267\u884c\u771f\u6b63\u7684init\u3002\u53e6\u5916\uff0c\u5f53\u7cfb\u7edf\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528chroot\u6765\u5207\u6362\u5230\u4e00\u4e2a\u4e34\u65f6\u7684\u7cfb\u7edf\u3002<\/p>\n

chroot\u7684\u4f7f\u7528<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

\u4e3a\u4e86\u66f4\u597d\u7684\u7406\u89e3chroot\u53d1\u6325\u7684\u4f5c\u7528\uff0c\u6211\u4eec\u5c06\u5c1d\u8bd5\u6307\u5b9a\u4e00\u4e2a\u7279\u5b9a\u7684\u4f4d\u7f6e\u8fdb\u884c\u6839\u76ee\u5f55\u5207\u6362\u3002\u5e76\u4e14\u56e0\u4e3a\u5728\u7ecf\u8fc7chroot\u4ee5\u540e\uff0c\u7cfb\u7edf\u8bfb\u53d6\u5230\u7684bin\/\u7b49\u4e0e\u7cfb\u7edf\u76f8\u5173\u76ee\u5f55\u5c06\u4e0d\u518d\u662f\u65e7\u7cfb\u7edf\u6839\u76ee\u5f55\u4e0b\u7684\uff0c\u800c\u662f\u5207\u6362\u540e\u65b0\u6839\u4e0b\u7684\u76ee\u5f55\u7ed3\u6784\u548c\u6587\u4ef6\uff0c\u56e0\u800c\u6211\u4eec\u6709\u5fc5\u8981\u6253\u7b97\u4e00\u4e9b\u76ee\u5f55\u7ed3\u6784\u4ee5\u53ca\u5fc5\u8981\u7684\u6587\u4ef6\u3002<\/p>\n

\u6e05\u53551.\u6253\u7b97\u5207\u6362\u7684\u76ee\u5f55\u7ed3\u6784<\/p>\n

busybox<\/p>\n

Busybox\u88ab\u79f0\u4e3a\u662f\u5d4c\u5165\u5f0fLinux\u4e2d\u7684\u5fb7\u56fd\u519b\u5200\u3002Busybox\u5305\u542b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u547d\u4ee4\uff0c\u5982cat\u3001find\u7b49linux chroot<\/strong>\uff0c\u5e76\u4e14\u5b83\u7684\u5bb9\u79ef\u5374\u5341\u5206\u7684\u5c0f\u3002<\/p>\n

\n

\u00a0$ pwd\n\u00a0\/home\/wstone\/Build\/work\n\u00a0$ tree\u00a0.\n\u00a0.\n\u00a0|-- bin\n\u00a0| |-- ash\u00a0-> busybox\n\u00a0| |--\u00a0bash\n\u00a0|\u00a0`-- busybox\n\u00a0|-- etc\n\u00a0`-- newhome<\/pre>\n<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

\u8fd9\u513f\u4f7f\u7528\u4e86\u9759\u6001\u7f16\u8bd1\u540e\u7684busybox\u6765\u63d0\u4f9b\u5fc5\u8981\u7684\u547d\u4ee4\uff0c\u4f7f\u7528\u9759\u6001\u7f16\u8bd1\u4ec5\u662f\u4e3a\u4e86\u9632\u6b62\u52a8\u6001\u5e93\u6587\u4ef6\u7684\u62f7\u8d1d\u3002\u5176\u5b9e\u6211\u4eec\u4e5f\u53ef\u4ee5\u62f7\u8d1d\u65e7\u7cfb\u7edf\u7684\u4e0b\u7684\u547d\u4ee4\u5230\u65b0\u7684\u76ee\u5f55\u7ed3\u6784\u4e2d\u4f7f\u7528\uff0c\u800c\u4e14\u8fd9\u4e9b\u547d\u4ee4\u4e00\u822c\u662f\u52a8\u6001\u7f16\u8bd1\u7684\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u6211\u4eec\u4e0d\u5f97\u4e0d\u62f7\u8d1d\u76f8\u5173\u7684\u52a8\u6001\u5e93\u6587\u4ef6\u5230\u76f8\u5e94\u7684\u76ee\u5f55\u7ed3\u6784\u4e2d\u3002\u540c\u65f6\u8fd9\u513f\u7684bash\u4e5f\u975e\u771f\u6b63\u7684BourneAgainshell\uff0c\u800c\u662f\u4e00\u4e2a\u6267\u884cash\u7684shell\u811a\u672c\u3002\u5728\u6e05\u53552\u4e2d\uff0c\u5c55\u793a\u4e86\u5750\u843d\u65e7\u7cfb\u7edf\u4e2d\u7684chroot\u547d\u4ee4\u7684\u4f7f\u7528\u3002\u987b\u8981\u6ce8\u610f\u7684\u662f\u5728\u4f7f\u7528chroot\u65f6\uff0c\u8981\u6c42\u62e5\u6709\u76f8\u5173\u7684\u64cd\u4f5c\u6743\u9650\u3002<\/p>\n

\u6e05\u53552.\u5750\u843d\u7cfb\u7edf\u4e2d\u7684chroot\u7684\u4f7f\u7528<\/p>\n

\n

\u00a0mount \/dev\/hda1 \/new-root\n\u00a0cd\u00a0\/new-root\n\u00a0pivot_root . old-root\n\u00a0exec\u00a0chroot\u00a0. \/sbin\/init dev\/console\u00a02>&1\n\u00a0umount \/old-root<\/pre>\n<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u770b\u89c1\u5f53\u524d\u8def\u5f84(\/home\/wstone\/Build\/work\/)\uff0c\u5728\u7ecf\u8fc7chroot\u540e\u8f6c\u5f04\u6210\u4e86\/\u76ee\u5f55\uff0c\u540c\u65f6\u4ece\u65b0\u6839\u4e0b\u8bfb\u53d6\u4e86\u4e0e\u7cfb\u7edf\u76f8\u5173\u7684\u76ee\u5f55\u7ed3\u6784\u3002\u4f7f\u7528ls\u547d\u4ee4\u5931\u8d25\u662f\u56e0\u4e3a\u6211\u4eec\u521b\u5efa\u7684\u6d4b\u8bd5\u76ee\u5f55\u7ed3\u6784\u4e2d\u5e76\u6ca1\u6709\u5305\u542b\u547d\u4ee4ls\uff0c\u5e76\u4e14\u6211\u4eec\u6210\u529f\u7684\u4f7f\u7528\u4e86busybox\u4e2d\u7684ls\u3002\u4ee5\u4e0a\u89c1\u5230\u7684\u53ea\u662fchroot\u7684\u4e00\u79cd\u4f7f\u7528\u65b9\u6cd5\uff0c\u867d\u7136\u6807\u51c6\u7684chroot(Coreutils\u2013GNUcoreutilities\u63d0\u4f9b\u7684chroot)\u4f7f\u7528\u65b9\u6cd5\u67092\u79cd\uff1a<\/p>\n

\u6e05\u53553.\u6807\u51c6chroot\u76842\u79cd\u4f7f\u7528\u65b9\u6cd5<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

\n

\u00a0chroot\u00a0NEWROOT [COMMAND...]\n\u00a0\n\u00a0chroot\u00a0OPTION<\/pre>\n<\/p>\n

\u521a\u521a\u6211\u4eec\u4f7f\u7528\u7684\u662f\u5f62\u5f0f[2]\u3002\u8fd9\u5c06\u5728\u6ca1\u6709\u7ed9\u5b9a\u73af\u5883\u65f6\uff0c\u9ed8\u8ba4\u6267\u884c\/bin\/sh\uff0c\u5e76\u4e14\u5f53\u7ed9\u5b9a\u73af\u5883\u540e\uff0c\u5c06\u8fd0\u884c${SHELL}\u2013i\uff0c\u5373\u4e0e\u73af\u5883\u76f8\u540c\u7684\u53ef\u4ea4\u4e92\u7684shell\u3002\u6211\u4eec\u7684\u76ee\u5f55\u7ed3\u6784\u4e2d\u5e76\u6ca1\u6709\u5305\u542bsh\uff0c\u5373\u4fbf\u6e05\u53552\u4e2d\u7684chroot\u8fd0\u884c\u4e86${SHELL}\u2013i\u3002\u5176\u5b9e\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u8fdb\u884c\u5207\u6362\u65f6\u6307\u5b9a\u987b\u8981\u7684\u547d\u4ee4\uff0c\u5373\u4f7f\u7528\u65b9\u6cd5[1]\u3002<\/p>\n

\u6e05\u53554.chroot\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684\u4f7f\u7528<\/p>\n

\n

\u00a0# chroot . \/bin\/ash\n\u00a0#<\/pre>\n<\/p>\n

\u5728\u6e05\u53554\u4e2d\uff0c\u5c1d\u8bd5\u4e86\u5728\u7ecf\u8fc7chroot\u540e\uff0c\u6267\u884c\u65b0\u76ee\u5f55\u7ed3\u6784\u4e0b\u7684ashshell\u3002\u4e0d\u5f97\u4e0d\u8bf4\u7684\u662f\uff0c\u5047\u5982\u65b0\u6839\u4e0b\u7684\u76ee\u5f55\u7ed3\u6784\u548c\u6587\u4ef6\u6253\u7b97\u7684\u591f\u5145\u5206\uff0c\u8fd9\u4e48\u4e00\u4e2a\u65b0\u7684\u7b80\u5355\u7684Linux\u7cfb\u7edf\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002\u867d\u7136\u66f4\u4e3a\u5e38\u89c1\u7684\u662f\u5728\u521d\u59cbRAMc\u76d8(initrd)\u4e2d\u4f7f\u7528chroot\uff0c\u501f\u6b64\u6765\u6267\u884c\u7cfb\u7edf\u7684init\u3002\u6e05\u53555\u4e2d\uff0c\u5c55\u793a\u7684\u662f\u5728Linux2.4\u5185\u6838initrd\u4e2d\u4f7f\u7528chroot\u3002<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

\u6e05\u53555.\u5728Linux2.4\u5185\u6838initrd\u4e2d\u4f7f\u7528chroot\u7684\u793a\u4f8b<\/p>\n

\n

\u00a0mount \/dev\/hda1 \/new-root\n\u00a0cd\u00a0\/new-root\n\u00a0pivot_root . old-root\n\u00a0exec\u00a0chroot\u00a0. \/sbin\/init dev\/console\u00a02>&1\n\u00a0umount \/old-root<\/pre>\n<\/p>\n

\u56e0\u4e3aLinux\u5185\u6838\u7684\u5347\u7ea7\uff0cinitrd\u5904\u7406\u673a\u5236\u548c\u683c\u5f0f\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u5728Linux2.6\u5185\u6838initrd\u4e2d\u4e0d\u80fd\u518d\u4f7f\u7528pivot_root\uff0c\u56e0\u800c\u901a\u5e38\u4e5f\u4e0d\u518d\u4f7f\u7528chroot\uff0c\u800c\u662f\u9009\u62e9\u4f7f\u7528busybox\u63d0\u4f9b\u7684switch_root\u6216\u5219klibc\u63d0\u4f9b\u7684run-init\u8fdb\u884c\u6839\u76ee\u5f55\u7684\u5207\u6362\u3002(\u8fd9\u5e76\u4e0d\u662f\u8bf4\u4e0d\u80fd\u5728Linux2.6\u5185\u6838initrd\u4e2d\u4f7f\u7528chroot\uff0c\u9009\u62e9switch_root\u6216run-init\u4ec5\u662f\u51fa\u4e8e\u4e60\u60ef\u548c\u4fbf\u6377\u7684\u8003\u8651\u3002)\u5e76\u4e14\u5b9e\u8d28\u4e0a\uff0c\u5b83\u4eec\u4ec5\u662f\u5c06chroot\u7684\u529f\u80fd\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u501f\u6b64\u6108\u53d1\u4fbf\u6377\u7b80\u5355\u7684\u5207\u6362\u6839\u76ee\u5f55\u3002<\/p>\n

\u6e05\u53556.\u5728Linux2.6\u5185\u6838initrd\u4e2dchroot\u7684\u4f7f\u7528<\/p>\n

\n

\u00a0find\u00a0-xdev\u00a0\/\u00a0-exec\u00a0rm\u00a0'{}'\u00a0';\n\u00a0cd\u00a0\/newmount; mount\u00a0--move\u00a0. \/;\u00a0chroot\u00a0.<\/pre>\n<\/p>\n

switch_root\u548crun-init\u5b8c\u6210\u4e86\u7c7b\u4f3c\u6e05\u53556\u4e2d\u7684\u529f\u80fd\uff0c\u5220\u6389rootfs\u7684\u5168\u90e8\u5185\u5bb9\u4ee5\u91ca\u653e\u7a7a\u95f4\uff0c\u4ee5\u53ca\u6302\u8f7d\u65b0\u7684\u6839\u6587\u4ef6\u7cfb\u7edf\u5e76\u8fdb\u884c\u5207\u6362\u3002\u5728busybox\u548cklibc\u4e2d\u4e5f\u6709\u63d0\u4f9bchroot\u547d\u4ee4\uff0c\u53ea\u662f\u529f\u80fd\u4e0a\u4e0eCoreutils(GNUcoreutilities)\u5305\u542b\u7684chroot\u6709\u7a0d\u7a0d\u5dee\u522b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

chroot\u547d\u4ee4\u7684\u4f7f\u7528\u65b9\u6cd5\u3002chrootchroot\uff0c\u4ee5\u6b64\u8fbe\u5230\u7528\u6237\u4e0d\u80fd\u8bbf\u95ee\u4e00\u4e9b\u7279\u5b9a\u7684\u6587\u4ef6\u3002\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u62f7\u8d1d\u65e7\u7cfb\u7edf\u7684\u4e0b\u7684\u547d\u4ee4\u5230\u65b0\u7684\u76ee\u5f55\u7ed3\u6784\u4e2d\u4f7f\u7528\uff0c\u4f46\u662f\u90a3\u4e9b\u547d\u4ee4\u901a\u5e38\u662f\u52a8\u6001\u7f16\u8bd1\u7684\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u6211\u4eec\u4e0d\u5f97\u4e0d\u62f7\u8d1d\u76f8\u5173\u7684\u52a8\u6001\u5e93\u6587\u4ef6\u5230\u76f8\u5e94\u7684\u76ee\u5f55\u7ed3\u6784\u4e2d\u3002chroot\u3002<\/p>\n","protected":false},"author":1,"featured_media":290384,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-290383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tougao","tag-999"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=290383"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290383\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/290384"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=290383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=290383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=290383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}