I think many WSL2 user might encounter this bug.
This debug tutorial is from XX-net.
The tutorial is written in Chinese, and I translated it into English via Chatgpt.
BUG
English: An attempt was made to access a socket in a way forbidden by its access permissions
中文: 你的主机中的软件中止了一个已建立的连接
Reason
This is because port 54321 is occupied. However, using netstat -aon | findstr cannot locate the port.
The reason is that the Hyper-V feature is enabled on this machine. This feature is related to Docker or virtual machines, and WSL2 also requires this feature to be enabled. Windows by default assigns certain port ranges to Hyper-V, making them reserved ports, so we cannot use this port.
Viewing the Default Dynamic TCP Port Range in Windows
You can view it using the command:
netsh int ipv4 show dynamicport tcp
Viewing the Reserved Port Range
You can view the reserved port range using the command:
netsh interface ipv4 show excludedportrange protocol=tcp
Sometimes ports like 1080, 8087, 8085 might be included in this range.
Resolving the Issue
Run CMD as an administrator, and set the dynamic port range so that the reserved port range avoids the ports we need.
netsh int ipv4 set dynamicport tcp start=54322 num=12214
netsh int ipv4 set dynamicport udp start=54322 num=12214
BUG
English: An attempt was made to access a socket in a way forbidden by its access permissions 中文: 你的主机中的软件中止了一个已建立的连接
Reason
This is because port 54321 is occupied. However, using
netstat -aon | findstr
cannot locate the port.The reason is that the Hyper-V feature is enabled on this machine. This feature is related to Docker or virtual machines, and WSL2 also requires this feature to be enabled. Windows by default assigns certain port ranges to Hyper-V, making them reserved ports, so we cannot use this port.
Viewing the Default Dynamic TCP Port Range in Windows
You can view it using the command:
Viewing the Reserved Port Range
You can view the reserved port range using the command:
Sometimes ports like 1080, 8087, 8085 might be included in this range.
Resolving the Issue
Run CMD as an administrator, and set the dynamic port range so that the reserved port range avoids the ports we need.
Then restart your computer.