{"id":9382,"date":"2024-07-25T02:54:15","date_gmt":"2024-07-24T18:54:15","guid":{"rendered":"\/?p=9382"},"modified":"2024-07-25T02:54:15","modified_gmt":"2024-07-24T18:54:15","slug":"wsl%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"\/?p=9382","title":{"rendered":"WSL\u914d\u7f6e"},"content":{"rendered":"<h2>\u5b98\u65b9\u6559\u7a0b<\/h2>\n<p><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/windows\/wsl\/networking\">https:\/\/learn.microsoft.com\/zh-cn\/windows\/wsl\/networking<\/a><\/p>\n<p>\u8fd8\u662f\u5efa\u8bae\u8bfb\u5b98\u65b9\u7684\uff0c\u5f88\u5168\u5f88\u65b0\uff0c\u4e0d\u5751\u4eba<\/p>\n<ul>\n<li>\n<p>WSL \u4e2d\u7684\u9ad8\u7ea7\u8bbe\u7f6e\u914d\u7f6e<\/p>\n<ul>\n<li>wsl.conf \u548c .wslconfig \u4e4b\u95f4\u6709\u4ec0\u4e48\u5dee\u522b\uff1f<\/li>\n<li>systemd \u652f\u6301<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7f51\u7edc\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/li>\n<li>\n<p>...<\/p>\n<\/li>\n<\/ul>\n<h2>WSL\u7f51\u7edc<\/h2>\n<p>\u8981\u4f7f Windows \u5c40\u57df\u7f51 IP\uff08192.168.191.52\uff09\u80fd\u591f\u8bbf\u95ee WSL \u7684\u670d\u52a1\u7aef\u53e3\uff08\u4f8b\u5982 8000 \u7aef\u53e3\uff09\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<h3>\u65b9\u6cd5\u4e00\uff1a\u914d\u7f6e WSL \u7f51\u7edc<\/h3>\n<p>\u901a\u8fc7\u914d\u7f6e WSL \u4f7f\u5176\u7ed1\u5b9a\u5230 Windows \u5c40\u57df\u7f51 IP \u4e0a\u3002<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00 WSL \u7ec8\u7aef\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u7f16\u8f91 WSL \u7684\u914d\u7f6e\u6587\u4ef6 <code>\/etc\/wsl.conf<\/code>\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo nano \/etc\/wsl.conf<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-ini\">[network]\ngenerateResolvConf = false<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\uff0c\u7136\u540e\u91cd\u542f WSL\uff1a<\/p>\n<pre><code class=\"language-bash\">wsl --shutdown\nwsl<\/code><\/pre>\n<\/li>\n<li>\n<p>\u7f16\u8f91 <code>\/etc\/resolv.conf<\/code> \u6587\u4ef6\uff0c\u5c06 DNS \u670d\u52a1\u5668\u5730\u5740\u4fee\u6539\u4e3a Windows \u7684 DNS \u670d\u52a1\u5668\uff08\u4f8b\u5982 192.168.191.1\uff09\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo nano \/etc\/resolv.conf<\/code><\/pre>\n<p>\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-ini\">nameserver 192.168.191.1<\/code><\/pre>\n<\/li>\n<li>\n<p>\u518d\u6b21\u91cd\u542f WSL\uff0c\u786e\u4fdd\u66f4\u6539\u751f\u6548\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>\u65b9\u6cd5\u4e8c\uff1a\u5728 Windows \u4e0a\u521b\u5efa\u7aef\u53e3\u8f6c\u53d1<\/h3>\n<p>\u901a\u8fc7\u521b\u5efa Windows \u9632\u706b\u5899\u89c4\u5219\u6765\u8f6c\u53d1\u7aef\u53e3\u3002<\/p>\n<ol>\n<li>\n<p>\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u6253\u5f00 PowerShell\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u542f\u7528\u7aef\u53e3\u8f6c\u53d1\uff1a<\/p>\n<pre><code class=\"language-powershell\">netsh interface portproxy add v4tov4 listenport=8000 listenaddress=192.168.191.52 connectport=8000 connectaddress=192.168.252.26<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5141\u8bb8\u7aef\u53e3\u901a\u8fc7\u9632\u706b\u5899\uff1a<\/p>\n<pre><code class=\"language-powershell\">netsh advfirewall firewall add rule name=\"WSL 8000\" dir=in action=allow protocol=TCP localport=8000<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u65b9\u6cd5\u4e09\uff1a\u914d\u7f6e WSL2 \u5b50\u7cfb\u7edf<\/h3>\n<p>\u5982\u679c\u4f7f\u7528\u7684\u662f WSL2\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8ba9\u5176\u5728\u5c40\u57df\u7f51\u5185\u53ef\u8bbf\u95ee\uff1a<\/p>\n<ol>\n<li>\n<p>\u5728 WSL \u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u83b7\u53d6 WSL IP \u5730\u5740\uff1a<\/p>\n<pre><code class=\"language-bash\">ip addr show eth0<\/code><\/pre>\n<p>\u627e\u5230\u7c7b\u4f3c\u4e8e <code>inet 192.168.252.26\/24<\/code> \u7684\u884c\uff0c\u8bb0\u4e0b IP \u5730\u5740\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728 Windows \u4e0a\u521b\u5efa\u7aef\u53e3\u8f6c\u53d1\u89c4\u5219\uff0c\u5141\u8bb8\u5916\u90e8\u8bbe\u5907\u8bbf\u95ee\uff1a<\/p>\n<pre><code class=\"language-powershell\">netsh interface portproxy add v4tov4 listenport=8000 listenaddress=192.168.191.52 connectport=8000 connectaddress=192.168.252.26<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5141\u8bb8\u9632\u706b\u5899\u89c4\u5219\uff1a<\/p>\n<pre><code class=\"language-powershell\">netsh advfirewall firewall add rule name=\"WSL 8000\" dir=in action=allow protocol=TCP localport=8000<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u5e94\u8be5\u53ef\u4ee5\u4f7f WSL \u4e2d\u7684\u670d\u52a1\u5728 Windows \u5c40\u57df\u7f51 IP \u4e0a\u53ef\u8bbf\u95ee\u3002<\/p>\n<h2>\u7aef\u53e3\u8f6c\u53d1<\/h2>\n<pre><code># \u8bbe\u7f6e\u7aef\u53e3\u8f6c\u53d1\nnetsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8080 connectaddress=&lt;wsl ip&gt; connectport=8080\n# \u5220\u9664\u7aef\u53e3\u8f6c\u53d1\nnetsh interface portproxy delete v4tov4 listenaddress=192.168.0.101 listenport=7001\n# \u67e5\u770b\u6240\u6709\u8f6c\u53d1\u4fe1\u606f\nnetsh interface portproxy show all\n# \u6e05\u7a7a\u6240\u6709\u8f6c\u53d1\u4fe1\u606f\nnetsh interface portproxy reset<\/code><\/pre>\n<h2>\u914d\u7f6e\u4fe1\u606f<\/h2>\n<h3><code>\\\\wsl\\etc\\wsl\\wsl.conf<\/code><\/h3>\n<pre><code class=\"language-yaml\">[boot]\n# Set a command to run when a new WSL instance launches. This example starts the Docker container service.\nsystemd=true\n# command = service docker start\n# Automatically mount Windows drive when the distribution is launched\n[automount]\n\n# Set to true will automount fixed drives (C:\/ or D:\/) with DrvFs under the root directory set above. Set to false means drives won&#039;t be mounted automatically, but need to be mounted manually or with fstab.\nenabled = true\n\n# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be \/c, rather than the default \/mnt\/c. \nroot = \/\n\n# DrvFs-specific options can be specified.  \n# options = &quot;metadata,uid=1003,gid=1003,umask=077,fmask=11,case=off&quot;\n\n# Sets the `\/etc\/fstab` file to be processed when a WSL distribution is launched.\nmountFsTab = true\n\n# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating \/etc\/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating \/etc\/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).\n[network]\nhostname = LemonUbuntu\ngenerateHosts = true\ngenerateResolvConf = false\n\n# Set whether WSL supports interop processes like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.\n[interop]\nenabled = true\nappendWindowsPath = true\n\n# Set the user when launching a distribution with WSL.\n[user]\ndefault = root<\/code><\/pre>\n<h3><code>\\c\\User\\&lt;username&gt;\\.wslconfig<\/code><\/h3>\n<pre><code class=\"language-yaml\"># Settings apply across all Linux distros running on WSL 2\n[wsl2]\n\n# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB\nmemory=8GB \n; networkingMode=mirrored\n# Sets the VM to use two virtual processors\nprocessors=10\n\n# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https:\/\/github.com\/microsoft\/WSL2-Linux-Kernel\n# kernel=C:\\\\temp\\\\myCustomKernel\n\n# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6\nkernelCommandLine = vsyscall=emulate\n\n# Sets amount of swap storage space to 8GB, default is 25% of available RAM\nswap=8GB\n\n# Sets swapfile path location, default is %USERPROFILE%\\AppData\\Local\\Temp\\swap.vhdx\n# swapfile=D:\\\\WSL\\\\ubuntu20\\\\wsl-swap.vhdx\n\n# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free\n# pageReporting=false\n\n# Turn on default connection to bind WSL 2 localhost to Windows localhost. Setting is ignored when networkingMode=mirrored\nlocalhostforwarding=true\ndnsProxy=false\nfirewall=false\nautoProxy=true\n\n# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging\n# debugConsole=true\n\n# Enable experimental features\n; [experimental]\n; hostAddressLoopback=true<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b98\u65b9\u6559\u7a0b https:\/\/learn.microsoft.com\/zh-cn\/windows\/wsl\/networking \u8fd8\u662f\u5efa\u8bae\u8bfb\u5b98   \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":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9382"}],"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=9382"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9382\/revisions"}],"predecessor-version":[{"id":9383,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9382\/revisions\/9383"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9382"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}