This article walks you through setting up your Flask application running in Ubuntu on WSL (Windows Subsystem for Linux) so that it’s accessible from other devices on your local network. It covers configuring Flask, finding your WSL IP address, setting up port forwarding using Windows’ netsh
utility, and adding a firewall rule from the command line.
Prerequisites
- Flask Application: A ready-to-run Flask app.
- WSL Installed: Ubuntu (or another Linux distribution) is installed in WSL.
- Administrator Access: You have administrator privileges on your Windows machine.
- Network Access: Your Windows machine is connected to your local network.
Step 1: Configure Your Flask Application
By default, Flask binds to 127.0.0.1
, meaning it only accepts connections from the same machine. Change the host binding to 0.0.0.0
to listen on all interfaces.
Example Flask App:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello from WSL!"
if __name__ == '__main__':
# Bind to all interfaces so that external devices can connect
app.run(host='0.0.0.0', port=5000)
When you run your Flask app, you should see output similar to:
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://172.20.73.225:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 616-091-304
The line showing http://172.20.73.225:5000
is the IP address of your WSL instance.
Step 2: Find Your WSL IP Address
To know where your Flask app is running inside WSL, you need to identify its IP address. Use the following command in your WSL terminal:
ip addr show eth0
Interpreting the Output
You might see a line similar to:
inet 172.20.73.225/20 brd 172.20.79.255 scope global eth0
inet 172.20.73.225/20
:172.20.73.225
is your WSL IP address. This is the address that your Flask app is bound to./20
indicates the subnet mask in CIDR notation. It tells you that the first 20 bits of the address denote the network portion.
brd 172.20.79.255
:172.20.79.255
is the broadcast address for your subnet, used to send data to all devices in the network.
scope global eth0
:- Indicates that this IP is globally routable on the
eth0
interface.
- Indicates that this IP is globally routable on the
Summary:
- WSL IP Address:
172.20.73.225
- Broadcast Address:
172.20.79.255
- Subnet Mask (CIDR):
/20
You’ll use the WSL IP (172.20.73.225
in this example) for setting up port forwarding.
Step 3: Set Up Port Forwarding with netsh
WSL 2 runs in a virtualized environment with its own IP address, which external devices cannot access directly. Use Windows’ netsh
utility to forward traffic from your Windows host to the WSL instance.
Create the Port Proxy Rule
-
Open an Elevated Command Prompt:
- Press the Start button, type
cmd
, right-click on Command Prompt, and select Run as administrator.
- Press the Start button, type
-
Add the Port Proxy Rule:
Run the following command (replace
172.20.73.225
with your WSL IP if different):netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=5000 connectaddress=172.20.73.225 connectport=5000
-
Verify the Rule:
Run:
netsh interface portproxy show all
You should see an entry like:
Listen on IPv4: 0.0.0.0:5000 Connect to IPv4: 172.20.73.225:5000
Step 4: Add a Windows Firewall Inbound Rule from the Command Line
If Windows Firewall blocks incoming connections, you can add a rule from the command line.
-
Open an Elevated Command Prompt:
(As before, run Command Prompt as Administrator.)
-
Add the Firewall Rule:
Use the
netsh advfirewall
command to allow TCP traffic on port 5000:netsh advfirewall firewall add rule name="FlaskAppPort5000" protocol=TCP dir=in localport=5000 action=allow
This creates a rule named “FlaskAppPort5000” to allow incoming TCP connections on port 5000.
Step 5: Test Your Configuration
Testing on the Windows Machine
-
Using a Web Browser:
- Open your browser and visit:
http://localhost:5000
(Since Windows forwards localhost requests to WSL.)
- Alternatively, use your Windows network IP (find it with
ipconfig
), for example:
http://192.168.1.10:5000
- Open your browser and visit:
-
Using Command-Line Tools:
-
Curl:
curl http://localhost:5000
-
PowerShell:
Test-NetConnection -ComputerName 192.168.1.10 -Port 5000
-
Testing from Another Device on Your Network
-
Open a web browser on another device and visit:
http://<Windows_Host_IP>:5000
Replace
<Windows_Host_IP>
with your Windows machine’s IP (e.g.,192.168.1.10
).
Troubleshooting Tips
-
WSL IP Changes:
The WSL IP may change after a restart. If it does, update the port proxy rule:netsh interface portproxy delete v4tov4 listenport=5000 listenaddress=0.0.0.0 netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=5000 connectaddress=<new_WSL_IP> connectport=5000
-
Firewall Settings:
Ensure that no other firewall or security software is blocking port 5000. You can temporarily disable the firewall to test connectivity, but remember to re-enable it afterward. -
Network Profile:
Make sure your network is set to “Private” rather than “Public” in Windows settings, as private networks tend to allow more inbound connections.
Conclusion
By following these steps, you can expose your Flask application running in WSL to your local network. The process involves:
- Configuring Flask to listen on all interfaces.
- Finding your WSL IP address and understanding the output of
ip addr show eth0
(identifying the actual IP and broadcast addresses). - Setting up port forwarding with Windows’
netsh
to route traffic from your host to WSL. - Adding a Windows Firewall rule from the command line to allow incoming traffic on the desired port.
This guide ensures that your development environment is ready to be accessed from any device on your local network. Happy coding!