{"id":290374,"date":"2024-03-16T05:11:11","date_gmt":"2024-03-15T21:11:11","guid":{"rendered":"https:\/\/lrxjmw.cn\/dljcsjzlcyxb.html"},"modified":"2024-03-16T05:11:11","modified_gmt":"2024-03-15T21:11:11","slug":"dljcsjzlcyxb","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/dljcsjzlcyxb.html","title":{"rendered":"\u5927\u4f6c\u6559\u7a0b\u6536\u96c6\u6574\u7406\uff1achroot\u8fd0\u884cbusybox\u5de5\u5177busybox\uff08\u66f4\u6539root\u76ee\u5f55\uff09"},"content":{"rendered":"

\u5927\u9cc4\u6559\u7a0b\u641c\u96c6\u6574\u7406\u7684\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86linuxchroot\u547d\u4ee4<\/a>\uff0c\u5927\u9cc4\u6559\u7a0b\u5927\u9cc4\u611f\u89c9\u633a\u4e0d\u9519\u7684\u4e2d\u6807linux\uff0c\u5982\u4eca\u5206\u4eab\u7ed9\u4f60\u4eeclinux\u67e5\u770b\u8fdb\u7a0b\uff0c\u4e5f\u7ed9\u4f60\u4eec\u505a\u4e2a\u53c2\u8003\u3002<\/p>\n

chroot\uff0c\u5373Changerootdirectory(\u4fee\u6539root\u76ee\u5f55)\u3002\u5728linux\u7cfb\u7edf<\/a>\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\u7528chroot\u4ee5\u540e\uff0c\u7cfb\u7edf\u7684\u76ee\u5f55\u7ed3\u6784\u5c06\u4ee5\u6307\u5b9a\u7684\u4f4d\u7f6e\u4f5c\u4e3a\/\u4f4d\u7f6e\u3002<\/p>\n

\u57fa\u672c\u53e5\u578b<\/p>\n

chrootNEWROOT[COMMAND[ARG]...]<\/p>\n

\u5177\u4f53\u7528\u6cd5\u8bf7\u53c2\u8003\u672c\u6587\u7684demo\u3002<\/p>\n

\u4e3a\u4f55\u8981\u4f7f\u7528chroot\u547d\u4ee4<\/p>\n

\u964d\u4f4e\u4e86\u7cfb\u7edf\u7684\u5b89\u5168\u6027\uff0c\u9650\u5236\u4e86\u7528\u6237\u7684\u6743\u5229\uff1a\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\u901a\u5e38\u4f1a\u5728\u7528\u6237\u767b\u9646\u524d\u5e94\u7528chroot\uff0c\u628a\u7528\u6237\u7684\u8bbf\u95ee\u80fd\u529b\u63a7\u5236\u5728\u4e00\u5b9a\u7684\u8303\u56f4\u4e4b\u5185\u3002<\/p>\n

\u6784\u5efa\u4e00\u4e2a\u4e0e\u539f\u7cfb\u7edf\u9694\u79bb\u7684\u7cfb\u7edf\u76ee\u5f55\u7ed3\u6784\uff0c\u4fbf\u6377\u7528\u6237\u7684\u5f00\u53d1\uff1a\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\u4e2d\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\u7f6elinux chroot<\/strong>\uff0c\u5f15\u5bfcLinux<\/a>\u7cfb\u7edf\u542f\u52a8\u4ee5\u53ca\u6025\u6551\u7cfb\u7edf\u7b49\uff1achroot\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\uff0c\u672c\u6587\u7684\u6700\u540e\u4e00\u4e2ademo\u4f1a\u8be6\u5c3d\u7684\u4ecb\u7ecd\u8fd9\u4e9b\u7528\u6cd5\u3002<\/p>\n

\u901a\u8fc7chroot\u8fd0\u884cbusybox\u5de5\u5177<\/p>\n

busybox\u5305\u542b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u79cd\u5de5\u5177\u653e\u7f6e\u5728\u4e00\u4e2a\u76ee\u5f55\u4e0b\uff0c\u4e4b\u540e\u901a\u8fc7chroot\u6784\u9020\u51fa\u4e00\u4e2amini\u7cfb\u7edf\u3002\u7b80\u5355\u8d77\u89c1\u6211\u4eec\u76f4\u63a5\u4f7f\u7528docker\u7684busybox\u955c\u50cf\u6253\u5305\u7684\u6587\u4ef6\u7cfb\u7edf\u3002\u5148\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55rootfs\uff1a<\/p>\n

\n

$ mkdir<\/span> rootfs<\/pre>\n<\/p>\n

\u4e4b\u540e\u628abusybox\u955c\u50cf\u4e2d\u7684\u6587\u4ef6\u91ca\u653e\u5230\u8fd9\u4e2a\u76ee\u5f55\u4e2d\uff1a<\/p>\n

\n

$ (docker export $(docker create busyboX)<\/a> | tar<\/span> -C rootfs -xvf -)<\/pre>\n<\/p>\n

\u901a\u8fc7ls\u547d\u4ee4\u67e5\u770brootfs\u6587\u4ef6\u5939\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n

\n

$ ls<\/span> rootfs<\/pre>\n<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u4e07\u4e8b\u4ff1\u5907\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u5427\uff01<\/p>\n

\u6267\u884cchroot\u540e\u7684ls\u547d\u4ee4<\/p>\n

\n

$ sudo<\/span> chroot<\/span> rootfs \/bin\/ls<\/span><\/pre>\n<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u5c3d\u7ba1\u8f93\u51fa\u7ed3\u679c\u4e0e\u521a\u521a\u6267\u884c\u7684lsrootfs\u547d\u4ee4\u5f62\u540c\uff0c\u7136\u800c\u6b64\u6b21\u8fd0\u884c\u7684\u547d\u4ee4\u5374\u662frootfs\/bin\/ls\u3002<\/p>\n

\u8fd0\u884cchroot\u540e\u7684pwd\u547d\u4ee4<\/p>\n

\n

$ sudo<\/span> chroot<\/span> rootfs \/bin\/pwd<\/span><\/pre>\n<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u54c8\uff0cpwd\u547d\u4ee4\u771f\u628arootfs\u76ee\u5f55\u5f53\u6839\u76ee\u5f55\u4e86\uff01<\/p>\n

\u4e0d\u5e26\u547d\u4ee4\u6267\u884cchroot<\/p>\n

\n

$ sudo<\/span> chroot<\/span> rootfs<\/pre>\n<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u6b64\u6b21\u51fa\u9519\u4e86\uff0c\u7531\u4e8e\u627e\u4e0d\u5230\/bin\/bash\u3002\u6211\u4eec\u6653\u5f97busybox\u4e2d\u662f\u4e0d\u5305\u542bbash\u7684\uff0c\u800c\u4e14chroot\u547d\u4ee4\u4e3a\u4f55\u4f1a\u627ebash\u547d\u4ee4\u5462\uff1f\u539f\u5148\uff0c\u5047\u82e5\u4e0d\u7ed9chroot\u6307\u5b9a\u6267\u884c\u7684\u547d\u4ee4\uff0c\u9ed8\u8ba4\u5b83\u4f1a\u6267\u884c'${SHelL}-i'\uff0c\u800c\u6211\u7684\u7cfb\u7edf\u4e2d${SHelL}\u4e3a\/bin\/bash\u3002\u65e2\u7136busybox\u4e2d\u6ca1\u6709bash\uff0c\u6211\u4eec\u53ea\u5f97\u6307\u5b9a\/bin\/sh\u6765\u6267\u884csHell\u4e86\u3002<\/p>\n

\n

$ sudo<\/span> chroot<\/span> rootfs \/bin\/sh<\/span><\/pre>\n<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u8fd0\u884csh\u662f\u6ca1\u6709\u95ee\u9898\u7684\uff0c\u4f46\u662f\u6211\u4eec\u590d\u5370\u51fa\u4e86\u5f53\u524d\u8fdb\u7a0b\u7684PID\u3002<\/p>\n

\u68c0\u6d4b\u7a0b\u5e8f\u662f\u5426\u8fd0\u884c\u5728chroot\u73af\u5883\u4e0b<\/p>\n

\u5176\u5b9e\u6211\u4eec\u505a\u4e86\u597d\u51e0\u4e2a\u5b9e\u9a8c\uff0c\u5e76\u4e14\u80af\u5b9a\u4f1a\u6709\u540c\u5b66\u5fc3\u5b58\u7591\u60d1\uff0c\u5982\u4f55\u80fd\u8bc1\u660e\u6211\u4eec\u8fd0\u884c\u7684\u547d\u4ee4\u5c31\u662f\u5728chroot\u76ee\u5f55\u540e\u7684\u8def\u5f84\u4e2d\u5462\uff1f\u663e\u7136\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\/proc\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u68c0\u6d4b\u8fdb\u7a0b\u7684\u4e2d\u7684\u6839\u76ee\u5f55\uff0c\u4f8b\u5982\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u8fb9\u7684\u4ee3\u7801\u68c0\u6d4b\u91cc\u9762\u8fd0\u884c\u7684\/bin\/sh\u547d\u4ee4\u7684\u6839\u76ee\u5f55(\u8bf7\u5728\u53e6\u5916\u4e00\u4e2asHell\u4e2d\u6267\u884c)\uff1a<\/p>\n

\n

$ pid=$(pidof<\/span> -s sh<\/span>)\n$ <\/span>sudo<\/span> ls<\/span> -ld<\/span> \/proc\/$pid\/root<\/pre>\n<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u8f93\u51fa\u4e2d\u7684\u5185\u5bb9\u660e\u6670\u7684\u5f3a\u8c03PID\u4e3a46644\u7684\u8fdb\u7a0b\u7684\u6839\u76ee\u5f55\u88ab\u6620\u5c04\u5230\u4e86\/tmp\/rootfs\u76ee\u5f55\u3002<\/p>\n

\u901a\u8fc7\u4ee3\u7801\u7406\u89e3chroot\u547d\u4ee4<\/p>\n

\u4e0b\u8fb9\u6211\u4eec\u5c1d\u8bd5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2achroot\u7a0b\u5e8f\uff0c\u4ee3\u7801\u4e2d\u6d89\u53ca\u5230\u4e24\u4e2a\u51fd\u6570\uff0c\u5206\u522b\u662fchroot()\u51fd\u6570\u548cchdir()\u51fd\u6570\uff0c\u867d\u7136\u771f\u6b63\u7684chroot\u547d\u4ee4\u4e5f\u662f\u901a\u8fc7\u8c03\u7528\u5b83\u4eec\u5b9e\u73b0\u7684\uff1a<\/p>\n

\n

#include \n#include <\/span>\n#include <\/span>\n

int<\/span> main(int<\/span> argc,char<\/span> *argv[])
\n{<\/a>
\n<\/span>if<\/span>(argc<2<\/span>)
{<\/a>
\nprintf(<\/span>\"<\/span>Usage: chroot NEWROOT [COMMAND...] n<\/span>\"<\/span>
);<\/a>
\nreturn <\/span>1<\/span>;
\n}<\/span><\/p>\n<\/pre>\n

<span style="color: #0000ff">if(<span style="color: #0000ff">chroot(argv[<span style="color: #800080">1<span style="color: #000000">])) {<\/a>\n    perror(<span style="color: #800000">"<span style="color: #800000">chroot<span style="color: #800000">"<span style="color: #000000">);<\/a>\n    return <span style="color: #800080">1<span style="color: #000000">;\n}\n<span style="color: #0000ff">if(chdir(<span style="color: #800000">"<span style="color: #800000">\/<span style="color: #800000">"<span style="color: #000000">)) {<\/a>\n    perror(<span style="color: #800000">"<span style="color: #800000">chdir<span style="color: #800000">"<span style="color: #000000">);<\/a>\n    return <span style="color: #800080">1<span style="color: #000000">;\n}\n<span style="color: #0000ff">if(argc == <span style="color: #800080">2<span style="color: #000000">) {<\/a>\n    <span style="color: #008000">\/\/<span style="color: #008000"> hardcode \/bin\/sh for my busybox tools.<\/a>\n    argv[<span style="color: #800080">0] = (<span style="color: #0000ff">char *)<span style="color: #800000">"<span style="color: #800000">\/bin\/sh<span style="color: #800000">"<span style="color: #000000">;\n    argv[<span style="color: #800080">1] = (<span style="color: #0000ff">char *) <span style="color: #800000">"<span style="color: #800000">-i<span style="color: #800000">"<span style="color: #000000">;\n    argv[<span style="color: #800080">2] =<span style="color: #000000"> NULL;\n} <span style="color: #0000ff">else<span style="color: #000000"> {<\/a>\n    argv += <span style="color: #800080">2<span style="color: #000000">;\n}\nexecvp (argv[<span style="color: #800080">0<span style="color: #000000">],argv);<\/a>\nprintf(<span style="color: #800000">"<span style="color: #800000">chroot: cAnn<\/a>ot run command `%s`n<span style="color: #800000">",*<span style="color: #000000">argv);<\/a>\nreturn <span style="color: #800080">0<span style="color: #000000">;<\/code><\/pre>\n<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

\u628a\u91cc\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5230\u6587\u4ef6mychroot.c\u6587\u4ef6\u4e2d\uff0c\u5e76\u6267\u884c\u4e0b\u8fb9\u7684\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n

\n

$ gcc<\/span> -Wall mychroot.c -o mychroot<\/pre>\n<\/p>\n

mychroot\u7684\u7528\u6cd5\u548cchroot\u57fa\u672c\u76f8\u540c\uff1a<\/p>\n

\n

$ sudo<\/span> .\/mychroot .\/rootfs<\/pre>\n<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u975e\u5e38\u4e4b\u5904\u662f\u6211\u4eec\u7684mychroot\u5728\u6ca1\u6709\u4f20\u9012\u547d\u4ee4\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u4e86\/bin\/sh\uff0c\u7f18\u7531\u5176\u5b9e\u662f\u4e3a\u4e86\u652f\u6301\u6211\u4eec\u7684busybox\u5de5\u5177\u96c6\uff0c\u7b14\u8005\u5728\u4ee3\u7801\u4e2dhardcode\u4e86\u9ed8\u8ba4\u7684sHell\uff1a<\/p>\n

\n

argv[0<\/span>] = (char<\/span> *)\"<\/span>\/bin\/sh<\/span>\"<\/span>;<\/pre>\n<\/p>\n

\u4ece\u4ee3\u7801\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u89c1\u5230\uff0c\u5b9e\u73b0chroot\u547d\u4ee4\u7684\u6838\u5fc3\u903b\u8f91\u867d\u7136\u5e76\u4e0d\u590d\u6742\u3002<\/p>\n

\u5b9e\u4f8b\uff1a\u901a\u8fc7chroot\u91cd\u65b0\u8bbe\u7f6eroot\u5bc6\u7801<\/p>\n

\u5fd8\u6389\u4e86root\u5bc6\u7801\u8be5\u5982\u4f55\u529e\uff1f\u63a5\u51fa\u6765\u7684demo\u5c06\u6f14\u793a\u600e\u4e48\u901a\u8fc7chroot\u547d\u4ee4\u91cd\u65b0\u8bbe\u7f6ecentos7\u4e2d\u88ab\u5fd8\u6389\u4e86\u7684root\u5bc6\u7801\u3002systemd\u7684\u7ba1\u7406\u673a\u5236\u4e2d\uff0crescure\u6a21\u5f0f\u548cemeryency\u6a21\u5f0f\u662f\u96be\u4ee5\u76f4\u63a5\u53d6\u5f97root\u6743\u9650\u7684\uff0c\u987b\u8981\u4f7f\u7528root\u5bc6\u7801\u80fd\u591f\u6b65\u5165rescure\u548cemeryency\u73af\u5883\u3002\u6240\u4ee5\u6211\u4eec\u987b\u8981\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u6765\u8bbe\u7f6eroot\u5bc6\u7801\u3002\u6211\u4eec\u53ef\u4ee5\u4e3a\u5185\u6838\u7684\u542f\u52a8\u6307\u5b9a\"rd.break\"\u53c2\u6570\uff0c\u8fdb\u800c\u8ba9\u7cfb\u7edf\u5728\u542f\u52a8\u7684\u521d\u671f\u505c\u51fa\u6765\uff0c\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528root\u6743\u9650\u5e76\u7ed3\u5408chroot\u547d\u4ee4\u5b8c\u6210\u8bbe\u7f6eroot\u5bc6\u7801\u7684\u64cd\u4f5c\u3002\u4e0b\u8fb9\u6211\u4eec\u4e00\u4e0a\u53bb\u770b\u5177\u4f53\u7684\u64cd\u4f5c\u8fc7\u7a0b\u3002<\/p>\n

\u5728\u7cfb\u7edf\u542f\u52a8\u8fc7\u7a0b\u4e2d\u6b65\u5165\u5f00\u673a\u83dc\u5355\u65f6\u6309\u4e0b\u5b57\u6bcd\u952ee\u8fdb\u7a0b\u5f00\u673a\u83dc\u5355\u7684\u7f16\u8f91\u6a21\u5f0f\uff1a<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u8fd9\u5c31\u662f\u7cfb\u7edf\u7684\u5f00\u673a\u83dc\u5355\uff0c\u6309\u4e0be\u540e\u6b65\u5165\u7f16\u8f91\u754c\u9762\uff1a<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u627e\u5230\u4ee5\"linux16\/vmlinuz-\"\u5f00\u5934\u7684\u884c\u3002\u5047\u5982\u9ed8\u8ba4\u6ca1\u6709\u770b\u89c1\u8be5\u884c\uff0c\u987b\u8981\u6309\u5411\u4e0a\u952e\u628a\u5b83\u6eda\u52a8\u4e0b\u6765\u3002\u4e4b\u540e\u5b9a\u4f4d\u5230\u8be5\u884c\u7ed3\u5c3e\u5904\uff0c\u8f93\u5165\u4e00\u4e2a\u7a7a\u683c\u548c\u5b57\u7b26\u4e32\"rd.break\"\uff0c\u5982\u53f3\u56fe\u6240\u793a\uff1a<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u63a5\u7740\u6309\u4e0bctrl+x\u4ee5\u8be5\u8bbe\u7f6e\u7ee7\u7eed\u542f\u52a8\uff0c\u542f\u52a8\u8fc7\u7a0b\u4e2d\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u505c\u51fa\u6765\uff0c\u8fd9\u662f\u7cfb\u7edf\u542f\u52a8\u8fc7\u7a0b\u4e2d\u7684\u4e00\u4e2a\u7279\u522b\u65e9\u7684\u65f6\u95f4\u70b9\uff1a<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u6240\u4ee5\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u8fd8\u6302\u8f7d\u5728RAMdisk\u4e0a(\u5c31\u662f\u663e\u5b58\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf)\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7mount\u547d\u4ee4\u68c0\u6d4b\u7cfb\u7edf\u5f53\u524d\u6302\u8f7d\u7684\u6587\u4ef6\u7cfb\u7edflinux chroot<\/strong>\uff0c\u4e0b\u8fb9\u662f\u6211\u4eec\u6bd4\u8f83\u5173\u5fc3\u7684\u4e24\u6761\uff1a<\/p>\n

title=\"linuxchroot\u547d\u4ee4\"alt=\"linuxchroot\u547d\u4ee4\"src=\"\"><\/p>\n

\u4e0a\u56fe\u4e2dmount\u547d\u4ee4\u8f93\u51fa\u7684\u7b2c\u4e00\u884c\u8bf4\u660e\u6b64\u65f6\u7684\u6839\u76ee\u5f55\u5728\u4e00\u4e2aRAMdisk\u4e2d,\u5373rootfs\u3002\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\u4f46\u662f\u662f\u53ea\u8bfb\u7684\u6a21\u5f0f\uff1a<\/p>\n

\n

\/dev\/mapper\/centos-root on \/sysroot type xfs (ro,relatime,attr2,inode64,noquota)<\/pre>\n<\/p>\n

\u800c\u5728\u6211\u4eec\u6b63\u5e38\u767b\u5f55\u7cfb\u7edf\u7684\u60c5\u51b5\u4e0b\uff0c\u7cfb\u7edf\u6839\u76ee\u5f55\u7684\u6302\u8f7d\u60c5\u51b5\u5982\u4e0b\uff1a<\/p>\n

\n

\/dev\/mapper\/centos-root on \/ type xfs (rw,seclabel,noquota)<\/pre>\n<\/p>\n

linux chroot_linux chroot_linux chroot<\/p>\n

\u8be5\u65f6\u95f4\u70b9\u7684\u6700\u5927\u4f18\u52bf\u662f\u6211\u4eec\u5177\u6709root\u6743\u9650\uff01\u6240\u4ee5\u8ba9\u6211\u4eec\u5f00\u59cb\u8bbe\u7f6e\u65b0\u7684root\u5bc6\u7801\u5427\u3002<\/p>\n

\u5148\u901a\u8fc7\u4e0b\u8fb9\u7684\u547d\u4ee4\u628a\/sysroot\u91cd\u65b0\u6302\u8f7d\u4e3a\u53ef\u8bfb\u5199\u7684\u6a21\u5f0f\uff1a<\/p>\n

\n

switch_root:\/# mount<\/span> -o remount,rw \/sysroot<\/pre>\n<\/p>\n

\u4e4b\u540e\u7528\u4e0b\u8fb9chroot\u547d\u4ee4\u628a\u6839\u76ee\u5f55\u5207\u6362\u5230\u6211\u4eec\u539f\u5148\u7684\u73af\u5883\u4e2d\uff1a<\/p>\n

\n

switch_root:\/# chroot<\/span> \/sysroot<\/pre>\n<\/p>\n

\u6b64\u65f6\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1a\u6211\u4eec\u4ee5root\u6743\u9650\u767b\u5165\u4e86\u539f\u5148\u7684\u7cfb\u7edf\uff0c\u66f4\u6539\u5bc6\u7801\u5c31\u5f88\u5bb9\u6613\u4e86\uff01\u7528\u4e0b\u8fb9\u7684\u547d\u4ee4\u4e3aroot\u7528\u6237\u8bbe\u7f6e\u65b0\u7684\u5bc6\u7801\uff1a<\/p>\n

\n

sh<\/span>-4.2<\/span># echo<\/span> \"<\/span>new_root_pw<\/span>\"<\/span> | passwd<\/span> --stdin root<\/pre>\n<\/p>\n

\u63a5\u51fa\u6765\u8fd8\u8981\u5904\u7406SELinux\u76f8\u5173\u7684\u95ee\u9898\u3002\u56e0\u4e3a\u5f53\u524d\u7684\u73af\u5883\u4e2dSELinux\u5e76\u672a\u542f\u52a8\uff0c\u6240\u4ee5\u6211\u4eec\u5bf9\u6587\u4ef6\u7684\u66f4\u6539\u53ef\u80fd\u5bfc\u81f4\u6587\u4ef6\u7684context\u4e0d\u6b63\u786e\u3002\u4e3a\u4e86\u786e\u4fdd\u5f00\u673a\u65f6\u91cd\u65b0\u8bbe\u5b9aSELinuxcontext\uff0c\u5fc5\u9808\u5728\u6839\u76ee\u5f55\u4e0b\u6dfb\u52a0\u9690\u85cf\u6587\u4ef6.autorelabel\uff1a<\/p>\n

\n

sh<\/span>-4.2<\/span># touch<\/span> \/.autorelabel<\/pre>\n<\/p>\n

\u6700\u540e\u4ecechroot\u4e2d\u9000\u51fa\uff0c\u5e76\u91cd\u542f\u7cfb\u7edf\uff1a<\/p>\n

\n

sh<\/span>-4.2<\/span># exit\nswitch_root:<\/span>\/# reboot<\/pre>\n<\/p>\n

\u91cd\u65b0\u6b65\u5165\u767b\u5f55\u754c\u9762\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u521a\u521a\u8bbe\u7f6e\u7684\u5bc6\u7801\u4ee5root\u767b\u5f55\u4e86\uff01<\/p>\n

\u603b\u7ed3<\/p>\n

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\u7528chroot\u6765\u8fdb\u884c\u8d44\u6e90\u7684\u9694\u79bb\u5b9e\u5728\u662flow\u4e86\u70b9\u3002\u6240\u4ee5chroot\u7684\u4e3b\u8981\u7528\u9014\u8fd8\u662f\u96c6\u4e2d\u5728\u7cfb\u7edf\u641c\u6551\u3001\u7ef4\u62a4\u7b49\u4e00\u4e9b\u7279\u6b8a\u7684\u573a\u666f\u4e2d\u3002<\/p>\n

\u53c2\u8003\uff1a\u7406\u89e3chrootLinux\u2013RedHat7\/CentOS7\u5fd8\u6389root\u5bc6\u7801\u66f4\u6539<\/p>\n

\u5927\u9cc4\u603b\u7ed3<\/p>\n

\u4ee5\u4e0a\u662f\u5927\u9cc4\u6559\u7a0b\u4e3a\u4f60\u641c\u96c6\u6574\u7406\u7684linuxchroot\u547d\u4ee4\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u6587\u7ae0\u624d\u80fd\u5e2e\u4f60\u89e3\u51b3linuxchroot\u547d\u4ee4\u6240\u9047\u89c1\u7684\u7a0b\u5e8f\u5f00\u53d1\u95ee\u9898\u3002<\/p>\n

\u5047\u5982\u8ba4\u4e3a\u5927\u9cc4\u6559\u7a0b\u7f51\u7ad9\u5185\u5bb9\u8fd8\u4e0d\u9519\uff0c\u6b22\u8fce\u5c06\u5927\u9cc4\u6559\u7a0b\u63a8\u8350\u7ed9\u7a0b\u5e8f\u5458\u597d\u53cb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u547d\u4ee4chroot\uff0c\u628a\u7528\u6237\u7684\u8bbf\u95ee\u80fd\u529b\u63a7\u5236\u5728\u4e00\u5b9a\u7684\u8303\u56f4\u4e4b\u5185\u3002\u955c\u50cf\u6253\u5305\u7684\u6587\u4ef6\u7cfb\u7edf\u3002rootfs\uff1a\u547d\u4ee4rootfs\/bin\/ls\u3002\u547d\u4ee4chroot\/bin\/bash\u3002\u547d\u4ee4\u5462\uff1f\/bin\/bash\u3002\u547d\u4ee4\u547d\u4ee4\u4e5f\u662f\u901a\u8fc7\u8c03\u7528\u5b83\u4eec\u5b9e\u73b0\u7684\uff1a\u6587\u4ef6\u4e2d\uff0c\u5e76\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1arootfs\u3002\u5fd8\u8bb0root\u5bc6\u7801\u4fee\u6539<\/p>\n","protected":false},"author":1,"featured_media":290375,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[988],"tags":[999],"class_list":["post-290374","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\/290374","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=290374"}],"version-history":[{"count":0,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/290374\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/290375"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=290374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=290374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=290374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}