Thursday, October 28, 2021

[SOLVED] Connect to SQL Server running on Windows host from a WSL 2/Ubuntu sqlcmd

Issue

I have a host running Windows 10, WSL 2. My guest is Ubuntu.

I'm trying to use sqlcmd to connect to the SQL Server running on my host machine, but I'm not sure what IP to use in the connection?

I've exposed/enabled basically everything from the SQL Configuration Manager on the host Windows SQL Server, and am using commands like this to try to connect:

sqlcmd -S 127.0.0.1 -U sa -P pass

Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.


Solution

In addition to using the IP from /etc/resolv.conf, you also have to open the 1433 port in Windows Firewall.

New rule - Port - Next - Specific ports: 1433 - Next - Allow - Next - Next - Name "WSL2 sql"


NOTE: You just opened the port to the entire world, so preferably limit this rule to your WSL2 IP address only. Double click the rule - Scope - Remote IP ads - Add your WSL2 address

To find your wsl host address type wsl hostname -I in windows cmd shell.



Answered By - Alex from Jitbit