{"id":9360,"date":"2024-07-02T00:15:20","date_gmt":"2024-07-01T16:15:20","guid":{"rendered":"\/?p=9360"},"modified":"2024-07-02T00:17:52","modified_gmt":"2024-07-01T16:17:52","slug":"%e8%a7%a3%e5%86%b3wsl%e4%b8%8d%e8%83%bd%e8%bf%90%e8%a1%8c-service-systemctl%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"\/?p=9360","title":{"rendered":"\u89e3\u51b3wsl\u4e0d\u80fd\u8fd0\u884c service systemctl\u95ee\u9898"},"content":{"rendered":"<h1>\u89e3\u51b3wsl\u4e0d\u80fd\u8fd0\u884c service systemctl\u95ee\u9898<\/h1>\n<h2>\u811a\u672c\u66ff\u6362<\/h2>\n<p>wsl \u5e76\u4e0d\u652f\u6301,\u4e0d\u8fc7 github \u4e0a\u6709\u4eba\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898, \u8fd9\u91cc<br \/>\n\u5927\u6982\u5c31\u662f\u8fd9\u6837\uff1a\u4f7f\u7528Python\u811a\u672c\u66ff\u6362\u4e86systemctl<\/p>\n<p><a href=\"https:\/\/github.com\/gdraheim\/docker-systemctl-replacement\/blob\/master\/files\/docker\/systemctl.py\">https:\/\/github.com\/gdraheim\/docker-systemctl-replacement\/blob\/master\/files\/docker\/systemctl.py<\/a><\/p>\n<pre><code class=\"language-powershell\">mv \/usr\/bin\/systemctl \/usr\/bin\/systemctl.old\ncurl https:\/\/raw.githubusercontent.com\/gdraheim\/docker-systemctl-replacement\/master\/files\/docker\/systemctl.py &gt; \/usr\/bin\/systemctl\nchmod +x \/usr\/bin\/systemctl<\/code><\/pre>\n<p>\u5b9e\u9645\u4e0a\u5c31\u662f\u5c06\/usr\/bin\/systemctl\u91cd\u5199\u4e3apython\u811a\u672c<\/p>\n<p>\u811a\u672c\u7b2c\u4e00\u884c\u4e3apython2\uff0c\u6309\u9700\u66ff\u6362\u4e3a\u4f60\u7684python\u8def\u5f84<\/p>\n<pre><code>#! \/usr\/bin\/python2<\/code><\/pre>\n<h2>\u5fae\u8f6f\u5b98\u65b9<\/h2>\n<p><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/windows\/wsl\/systemd\">https:\/\/learn.microsoft.com\/zh-cn\/windows\/wsl\/systemd<\/a><\/p>\n<h3>\u80cc\u666f<\/h3>\n<p>\u5f88\u591aLinux\u53d1\u884c\u7248\u90fd\u662f\u4f7f\u7528systemd\u6765\u7ba1\u7406\u7a0b\u5e8f\u8fdb\u7a0b\uff0c\u4f46\u662f\u5728WSL\u4e2d\u9ed8\u8ba4\u662f\u7528init\u6765\u7ba1\u7406\u8fdb\u7a0b\u7684\u3002<\/p>\n<p>\u4e3a\u4e86\u7b26\u5408\u957f\u4e45\u7684\u4f7f\u7528\u4e60\u60ef\uff0c\u4e14\u7701\u53bb\u4e0d\u5fc5\u8981\u7684\u5b66\u4e60\u6210\u672c\uff0c\u5c31\u5728WSL\u7684\u53d1\u884c\u7248\uff08\u6211\u8fd9\u91cc\u5b89\u88c5\u7684\u662fUbuntu20.04\uff09\u4e2d\u652f\u6301systemd\uff0c\u4e5f\u5c31\u53ef\u4ee5\u4f7f\u7528systemctl\u547d\u4ee4\u6765\u542f\u505c\u8fdb\u7a0b\u3002<\/p>\n<h3>\u76f8\u5173\u77e5\u8bc6<\/h3>\n<p>\u80fd\u591f\u652f\u6301\u4f7f\u7528systemctl\u547d\u4ee4\uff0c\u63d0\u4f9b\u5b8c\u6574 systemd \u7684\u9879\u76ee\u6709\u5f88\u591a\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/diddledani\/one-script-wsl2-systemd\">diddledani\/one-script-wsl2-systemd: The one-script variant of the systemd hack for WSL2 (github.com)<\/a><\/li>\n<li><a href=\"https:\/\/logi.im\/go\/aHR0cHM6Ly9naXRodWIuY29tL0RhbWlvbkdhbnMvdWJ1bnR1LXdzbDItc3lzdGVtZC1zY3JpcHQ=\">ubuntu-wsl2-systemd-script<\/a><\/li>\n<li><a href=\"https:\/\/logi.im\/go\/aHR0cHM6Ly9naXRodWIuY29tL3NoYXluZS93c2wyLWhhY2tz\">wsl2-hacks<\/a><\/li>\n<li><a href=\"https:\/\/logi.im\/go\/aHR0cHM6Ly9naXRodWIuY29tL2Fya2FuZS1zeXN0ZW1zL2dlbmll\">genie<\/a><\/li>\n<li><a href=\"https:\/\/logi.im\/go\/aHR0cHM6Ly9naXRodWIuY29tL3NvcmFoL3N1YnN5c3RlbWN0bA==\">subsystemd<\/a><\/li>\n<li><a href=\"https:\/\/logi.im\/go\/aHR0cHM6Ly9naXRodWIuY29tL0luY2lzYWt1cmEvYW5nZWE=\">Angea<\/a><\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u9879\u76ee\u7684\u6838\u5fc3\u539f\u7406\u5e76\u65e0\u5927\u5f02\uff0c\u65e0\u975e\u662f\u7ec6\u8282\u5904\u7406\u7684\u597d\u574f\u548c\u9644\u52a0\u529f\u80fd\u7684\u591a\u5c11\uff0c\u4f46\u6700\u91cd\u8981\u7684\u4e00\u70b9\u53ef\u80fd\u662f\u5de5\u4f5c\u5c42\u6b21\u4e0d\u540c\u3002<\/p>\n<p>\u524d\u4e09\u4e2a\u811a\u672c\u90fd\u5de5\u4f5c\u5728\u56fe\u4e2d\u7684<strong> <\/strong>shell<strong> <\/strong>\u5c42\uff0c\u8981\u4f9d\u8d56\u5916\u7f6e\u547d\u4ee4\u6267\u884c\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<p>genie \u5de5\u4f5c\u5728\u5e94\u7528\u7a0b\u5e8f\u5c42\uff0c\u5b83\u4f7f\u7528 C# \u7f16\u5199\uff0c\u901a\u8fc7 shell \u6267\u884c\u7cfb\u7edf\u8c03\u7528\uff0c\u6bd4\u524d\u4e09\u4e2a\u8fd8\u591a\u4e86\u4e00\u5c42\u3002<\/p>\n<p>\u6700\u540e\u4e24\u4e2a\u867d\u7136\u4e5f\u662f\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f46\u5b83\u4eec\u7531 Rust \u7f16\u5199\uff0c\u4e0d\u7ecf shell\uff0c\u800c\u76f4\u63a5\u7531 C system call wrapper \u6267\u884c\u7cfb\u7edf\u8c03\u7528\uff0c\u4f9d\u8d56\u6700\u5c11\uff0c\u52a8\u6001\u94fe\u63a5\u7f16\u8bd1\u540e\u53ea\u6709\u51e0\u767e K\u3002<\/p>\n<h3>systemd<\/h3>\n<p>\u5728\u8f83\u65b0\u7684Linux\u7cfb\u7edf\u4e0a\uff0c\u90fd\u4f7f\u7528systemd \u7ba1\u7406\u8fdb\u7a0b\uff0c\u6210\u4e3a\u7cfb\u7edf\u7684\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\uff08PID \u7b49\u4e8e 1\uff09\uff0c\u5176\u4ed6\u8fdb\u7a0b\u90fd\u662f\u5b83\u7684\u5b50\u8fdb\u7a0b\u3002<\/p>\n<p>systemd\u4e3a\u7cfb\u7edf\u542f\u52a8\u548c\u7ba1\u7406\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u547d\u4ee4\u3002\u5b57\u6bcdd\u662f\u5b88\u62a4\u8fdb\u7a0b\uff08daemon\uff09\u7684\u7f29\u5199\u3002<\/p>\n<h3>init<\/h3>\n<p>WSL2 \u7684\u53d1\u884c\u7248\u5747\u62e5\u6709\u5fae\u8f6f\u63d0\u4f9b\u7684 init\uff0c\u5b83\u662f\u4e00\u79cd systemd \u7684\u66ff\u4ee3\u65b9\u6848\uff0c\u4e0d\u652f\u6301 systemctl \u547d\u4ee4\uff0c<\/p>\n<p>WSL2 \u672c\u8eab\u662f\u7531 Windows \u8d1f\u8d23\u8fd0\u884c\u7684\uff0c\u56e0\u6b64\u4f7f\u7528 tree \u6216 ps \u547d\u4ee4\u65f6\u4f1a\u770b\u5230\u6839\u8fdb\u7a0b\u4e0d\u662f systemd\uff0c\u8fd9\u5c06\u5bfc\u81f4\u65e0\u6cd5\u542f\u52a8 Linux \u7cfb\u7edf\u670d\u52a1\u7684\u5b88\u62a4\u8fdb\u7a0b (deamon)\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u6267\u884c systemctl \u547d\u4ee4\u7684\u65f6\u5019\uff0c\u4f1a\u663e\u793a\u51fa\u6211\u4eec\u7684 init system (PID 1) \u5e76\u975e systemd\uff0c\u800c\u662f\u5fae\u8f6f\u63d0\u4f9b\u7684 init\u3002<\/p>\n<pre><code class=\"language-bash\">systemctl\nSystem has not been booted with systemd as init system (PID 1). Can&#039;t operate.\nFailed to connect to bus: Host is down<\/code><\/pre>\n<h3>\u5b89\u88c5<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u786e\u8ba4\u4f7f\u7528\u7684\u662fWSL2\u7248\u672c\uff0c\u53ef\u4ee5\u5728PowerShell\u4e2d\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u67e5\u770b\uff1a<\/p>\n<pre><code class=\"language-powershell\">wsl -l -v<\/code><\/pre>\n<p>\u5982\u679c\u663e\u793a\u4e3a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u8fdb\u884c\u914d\u7f6e\uff0c\u5176\u4e2d\u4fee\u6539\u62102\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-powershell\">wsl --set-default-version &lt;Version&gt;<\/code><\/pre>\n<p>\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u65b9\u5f0f\u8bbe\u7f6e\u9ed8\u8ba4\u7684Linux\u53d1\u884c\u7248\uff0c\u5176\u4e2d\u66ff\u6362\u4e3a\u8981\u914d\u7f6e\u7684Linux\u53d1\u884c\u7248\u540d\u79f0\u3002<\/p>\n<p>\u4f8b\u5982\uff0cwsl --set-version Ubuntu-20.04 \u4f1a\u5c06Ubuntu20.04\u53d1\u884c\u7248\u8bbe\u7f6e\u4e3a\u4f7f\u7528WSL2\uff1a<\/p>\n<pre><code class=\"language-powershell\">wsl --set-version &lt;distro name&gt; <\/code><\/pre>\n<blockquote>\n<p>\u5728\u7f51\u4e0a\u627e\u5230\u5f88\u591a\u79cd\u652f\u6301\u7684\u65b9\u6cd5\uff0c\u6700\u7b80\u5355\u7684\u5c31\u662f\u5fae\u8f6f\u5b98\u65b9\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u4ec5\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u5373\u53ef\u3002<\/p>\n<\/blockquote>\n<p>1\u3001\u6253\u5f00PowerShell\u547d\u4ee4\u884c\uff0c\u66f4\u65b0WSL\u3002<\/p>\n<pre><code class=\"language-powershell\">wsl --update<\/code><\/pre>\n<p>2\u3001\u6253\u5f00Ubuntu 22.04\uff0c\u5728 <code>\/etc<\/code>\u76ee\u5f55\u65b0\u5efa<code>wsl.conf<\/code>\u6587\u4ef6\uff0c\u6dfb\u52a0\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-text\">[boot]\nsystemd=true<\/code><\/pre>\n<p>3\u3001\u5173\u95edUbuntu\u7a97\u53e3\u3002<\/p>\n<p>4\u3001\u6253\u5f00PowerShell\u547d\u4ee4\u884c\uff0c\u91cd\u542fWSL\u3002<\/p>\n<pre><code class=\"language-powershell\">wsl --shutdown<\/code><\/pre>\n<p>5\u3001\u91cd\u65b0\u5728Windows Terminal\u4e2d\u6253\u5f00Ubuntu\u53d1\u884c\u7248\uff0c\u8f93\u5165\u5982\u4e0b\u547d\u4ee4\u68c0\u67e5\u662f\u5426\u6210\u529f\uff1a<\/p>\n<pre><code class=\"language-bash\">systemctl list-unit-files<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20240702001355947.png\" alt=\"image-20240702001355947\" \/> <\/p>\n<p>\u51fa\u73b0\u4e0a\u8ff0\u7684\u7ed3\u679c\u5219\u4ee3\u8868\u652f\u6301systemd\u6210\u529f\u3002<\/p>\n<hr \/>\n<blockquote>\n<p>\u53c2\u8003<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/commandline\/systemd-support-is-now-available-in-wsl\/\">Systemd support is now available in WSL!<\/a><\/p>\n<blockquote>\n<p>\u7248\u672c<\/p>\n<\/blockquote>\n<p>Windows 10 \u4e13\u4e1a\u7248 22H2<\/p>\n<p>Ubuntu 20.04<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u51b3wsl\u4e0d\u80fd\u8fd0\u884c service systemctl\u95ee\u9898 \u811a\u672c\u66ff\u6362 wsl \u5e76\u4e0d\u652f\u6301,\u4e0d\u8fc7 github \u4e0a\u6709\u4eba\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898, \u8fd9\u91cc   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[240],"tags":[241],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9360"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9360"}],"version-history":[{"count":3,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9360\/revisions"}],"predecessor-version":[{"id":9363,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9360\/revisions\/9363"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9360"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}