I would like to connect from my WSL 2 Debian System to a SQL Server instance. This is because I want to develop an cossplatform app in Python and make some tests.
WSL 1
As far as I know with WSL 1 I just have one adapter that is share between host and WSL, so I can just connect to something that is in a port of my host by writing the local address. For example to connect a Jupyter Lab server that is running in the host machine I can do this directly without opening any port and it works fine
wget http://localhost:8888/lab/tree/example_notebook.ipynb
WSL 2 + Jupyter + Firewall Rules
WSL 2 has an open issue where developers claim to make the WSL2 connection "Private" instead of "Public", and make the Firewall rules work in a secure way.
To make the connection work between my Debian WSL 2 system and Jupyter on my Windows host system I had to add an "Inbound rule" in the Firewall
- Protocol: TCP
- Port: 8888
- Public Networks
- Allowed IP addresses: [My WSL 2 Debian system IP address] In this case the whole subnet would be: 172.22.160.0/20
If I make some ping to the Windows adapter works fine. I also can check with nmap that the port is correctly open
$ nmap -p 8888 172.22.160.1 -Pn
Starting Nmap 7.80 ( https://nmap.org ) at 2022-09-22 14:40 CEST
Nmap scan report for [PC-NAME] (172.22.160.1)
Host is up (0.00040s latency).
PORT STATE SERVICE
8888/tcp open sun-answerbook
But I try to download a notebook as in the WSL1 system, it does not work :/
wget http://172.22.160.1:8888/lab/tree/example_notebook.ipynb
I have tried by disabling the firewall as well, but I got the same result.
Is there a way to make this work? I have tried other things stated in the GH thread, but I didn't find the solution.
WSL 2 + SQL Server + Firewall Rules
SQL Server works fine from cmd from my Windows host and this command:
sqlcmd -S [PC-NAME]\SQLEXPRESS -U user_name -P password -d db_name
But in my WSL2 Debian system I cannot even ping the instance, because the port is always "filtered", even if I disable the firewall I get this:
$ nmap -p 1433 172.22.160.1 -Pn
Starting Nmap 7.80 ( https://nmap.org ) at 2022-09-22 15:19 CEST
Nmap scan report for APD-PC (172.22.160.1)
Host is up.
PORT STATE SERVICE
1433/tcp filtered ms-sql-s
My SQL Server instance is listening to remote connections.
I don't know why this port is different from the 8888 that I can open without any issue.
SQL Server
My SQL Server instance running. I can connect on my Windows host machine with this command in cmd console:
sqlcmd -S 172.22.160.1\SQLEXPRESS -U username -P password -d database_name
It works Okay in local, I can run some queries without any issue. But if I try from my WSL2 Debian system, it does not work even without firewall disabled. The main reason is that there is no real connection. I don't know if I need to
sqlcmd -S PCNAME\SQLEXPRESS -U username -P password -d database_name
What I have tried
- Disable the Windows Firewall and VPN software I had installed. Surprisingly, disabling the firewall converts the state of some ports from filtered to closed instead of open, even if Jupyter is running in them. The port 8888 work as expected, but other ports such as 80, or 8081 remain closed even without firewall.
- Adding the right firewall rules work different for different ports. I don't find a pattern, it's kind of random.
- I have enable the TCP/IP protocol in the SQL Server Configuration Manager and I tried several configurations.
- I enabled the firewall setting name is Virtual Machine Monitoring
- There is another open GH issue asking for the same access