Last active
November 14, 2024 12:14
-
-
Save shigenobuokamoto/b565d468541fc8be7d7d76a0434496a0 to your computer and use it in GitHub Desktop.
/etc/systemd/system/network-mirrored.service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Wants=network-pre.target | |
Before=network-pre.target shutdown.target | |
[Service] | |
User=root | |
ExecStart=/bin/sh -ec '\ | |
[ -x /usr/bin/wslinfo ] && [ "$(/usr/bin/wslinfo --networking-mode)" = "mirrored" ] || exit 0;\ | |
echo "\ | |
add chain ip nat WSLPREROUTING { type nat hook prerouting priority dstnat - 1; policy accept; };\ | |
insert rule ip nat WSLPREROUTING iif loopback0 ip daddr 127.0.0.1 counter dnat to 127.0.0.1 comment mirrored;\ | |
"|nft -f -\ | |
' | |
ExecStop=/bin/sh -ec '\ | |
[ -x /usr/bin/wslinfo ] && [ "$(/usr/bin/wslinfo --networking-mode)" = "mirrored" ] || exit 0;\ | |
for chain in "ip nat WSLPREROUTING";\ | |
do\ | |
handle=$(nft -a list chain $chain | sed -En "s/^.*comment \\"mirrored\\" # handle ([0-9]+)$/\\1/p");\ | |
for n in $handle; do echo "delete rule $chain handle $n"; done;\ | |
done|nft -f -\ | |
' | |
RemainAfterExit=yes | |
[Install] | |
WantedBy=multi-user.target |
@KingOfGitH
when configuring network settings such as NetworkManager on Linux, wsl may not be able to communicate, but i do not see anything wrong with the information provided.
send logs to https://github.com/microsoft/WSL/issues and microsoft may be able to provide a solution.
you can try it.
$ sudo systemctl --now enable network-mirrored
Very grateful for this, spent countless hours trying to figure it out, no amount of gpt could save me.
this script is no longer needed.
please use Docker 27.3.0 or later.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have turned off all firewalls, including Hyper-V. Then I found that the problem should be that the port between wsl2 and the host in mirrored network mode cannot communicate. I looked for some related network configuration. I'm not sure if there's something wrong with that.
Anyway, thank you very much. It is indeed not a problem with network-mirrored.service. I think I need to find other related issues