Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 538
mariocatch Avatar asked Nov 21 '20 03:11

mariocatch


People also ask

How do I use Windows authentication in Sqlcmd?

Answers. You can use the runas command from the command line. Either start a new Command Prompt as that user or start a sqlcmd directly. Use the SUSER_NAME() function to verify you are logged in as right user.

How do I connect to mssql remotely from Linux?

Connect to SQL Server on LinuxStart SSMS by typing Microsoft SQL Server Management Studio in the Windows search box, and then select the desktop app. The default is database engine; don't change this value. Enter the name of the target Linux SQL Server machine, or its IP address and port in the format IP,port .


3 Answers

  1. In your connection string, use the IP address from cat /etc/resolv.conf
  2. open the 1433 port in Windows Firewall. Go to New rule - Port - Next - Specific ports: 1433 - Next - Allow - Next - Next - Name "WSL2 sql"
  3. Disable VPN software in Windows (if any) - it might brake WSL2/Windows network connectivity

NOTE: You just opened the port to the entire world, so either enable-disable this rule only when needed, or 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)

like image 151
Alex from Jitbit Avatar answered Oct 21 '22 03:10

Alex from Jitbit


Have you try looking on the IP on /etc/resolv.conf?

Try connecting using that one, example: sqlcmd -S 192.168.202.65 -U sa -P pass

This is because of this

like image 23
QiQe Garbi Avatar answered Oct 21 '22 04:10

QiQe Garbi


Enabling the "Virtual Machine Monitoring" worked for me. These four firewall rules are located in the inbound list, names starting with "Virtual Machine Monitoring" for:

  • Echo Requests (ICMPv4/v6)
  • RPC
  • DCOM-In
  • NB-Session-In

They appear to be disabled by default and once I enabled them, I was able to ping the host as well connect to my host sql server service using the IP address from the WSL generated resolv.conf file --

cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 1xx.1xx.1xx.1xx
like image 1
RominNoodleSamurai Avatar answered Oct 21 '22 04:10

RominNoodleSamurai