Recently, when trying to set the port of a local app to 80 in WSL 2, which is a Windows Subsystem for Linux, an error occurred.
Upon investigation, it was found that WSL blocks the use of ports below 1000 by default. This is probably for security reasons, and it is important to be cautious when using ports.
Below, we will briefly look at how to allow ports below 1000 in WSL.
# How to use port numbers below 1000 in WSL
The method is to change the setting for the default allowed ports in WSL. The process is as follows:
- Find and modify the /etc/sysctl.conf file.
- Add code to allow ports below 1000 to be used.
- Save and restart or reload system files to confirm.
Now, let's take a closer look at how to make these changes.
1. Find and modify the /etc/sysctl.conf fileYou need to find and modify the sysctl.conf file, which is the system configuration file for WSL.
sudo vim /etc/sysctl.conf
2. Add code to allow ports below 1000 (or your desired port number)Add the following code to the configuration file. This is the most important part of this article. Let's say you want to allow port 80, which is below 1000:
net.ipv4.ip_unprivileged_port_start=80
3. Restart or reload system files after making changesThat's all the necessary steps. To reflect the modified file, reload the system configuration file or restart the system.
sudo sysctl --system
Or restart WSL in the cmd.
wsl --shutdown
Once you're done, run WSL again to confirm that the port is now available. Previously, when trying to use port 80 in an existing Nuxt app, an error occurred, as shown in the screenshot.
Screenshot: Error message when trying to use port 80 in an existing Nuxt app.However, now the app runs smoothly on port 80 without any errors.
That's all about how to use ports below 1000 in WSL.