How to Test SQL Server Connectivity from Linux Using Various Tools and Commands


2 views

Before testing connectivity, ensure:

  • Network connectivity between Linux and SQL Server (ping test)
  • SQL Server TCP/IP protocol enabled (default port 1433)
  • Firewall rules configured on both ends
  • Authentication credentials with proper permissions

The quickest way to verify basic network connectivity:

telnet sqlserver.domain.com 1433

If the connection is successful, you'll see a blank screen (press Ctrl+] to exit). For Ubuntu/Debian systems without telnet:

sudo apt-get install telnet
sudo apt-get install freetds-bin freetds-dev

Configure FreeTDS in /etc/freetds/freetds.conf:

[MyServer]
    host = sqlserver.domain.com
    port = 1433
    tds version = 7.4

Test connection using tsql:

tsql -S MyServer -U username -P password

Successful connection will display:

locale is "en_US.UTF-8"
locale charset is "UTF-8"
1>

For a more interactive approach similar to SSMS:

sudo apt-get install mssql-tools unixodbc-dev
export PATH="$PATH:/opt/mssql-tools/bin"
sqlcmd -S sqlserver.domain.com -U sa -P your_password

Example query execution:

SELECT @@VERSION;
GO

For automated testing in scripts:

import pyodbc
conn = pyodbc.connect(
    'DRIVER={ODBC Driver 17 for SQL Server};'
    'SERVER=sqlserver.domain.com;'
    'DATABASE=master;'
    'UID=username;'
    'PWD=password'
)
cursor = conn.cursor()
cursor.execute("SELECT name FROM sys.databases")
for row in cursor:
    print(row)

Prerequisites:

sudo apt-get install python3-pip
pip3 install pyodbc
  • Connection timeout: Verify network routes and firewall rules
  • Login failed: Check SQL Server authentication mode (might need SQL auth instead of Windows auth)
  • Protocol not found: Install proper ODBC driver version
  • Encryption error: Add "Encrypt=no" to connection string if using self-signed certificates

For ongoing monitoring, create a shell script:

#!/bin/bash
if echo "" | telnet sqlserver.domain.com 1433 2>&1 | grep -q "Connected"; then
    echo "SQL Server connection OK"
    exit 0
else
    echo "Connection FAILED"
    exit 1
fi

Set up cron job to run this periodically.


Before testing the connection, ensure you have:

  • A Linux server (Ubuntu 18.04/Debian/CentOS)
  • Network access to the SQL Server instance (firewall rules configured)
  • Valid SQL Server credentials (username/password)
  • SQL Server instance name or IP address
  • Port number (default is 1433)

Install the necessary packages:

sudo apt-get update
sudo apt-get install freetds-bin freetds-dev -y

Basic connection test command:

tsql -S your_server_name -U your_username -P your_password

Example with parameters:

tsql -S 192.168.1.100,1433 -U sa -P 'ComplexP@ssw0rd!'

For Ubuntu/Debian systems:

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt-get update 
sudo apt-get install mssql-tools unixodbc-dev

Test connection with a simple query:

sqlcmd -S tcp:your_server_name,1433 -U your_username -P your_password -Q "SELECT @@VERSION"

Check if the SQL Server port is accessible:

telnet your_server_name 1433

If telnet isn't installed:

sudo apt-get install telnet

Connection timeout - Verify network connectivity and firewall rules

Login failed - Check credentials and SQL Server authentication mode

Protocol not supported - Ensure SQL Server is configured for TCP/IP connections

Create a bash script for regular testing:

#!/bin/bash
# sql_connection_test.sh
SERVER="your_server"
PORT="1433"
USER="your_user"
PASS="your_pass"

if sqlcmd -S tcp:$SERVER,$PORT -U $USER -P $PASS -Q "SELECT 1" &> /dev/null
then
    echo "$(date) - Connection successful" >> /var/log/sql_connection.log
else
    echo "$(date) - Connection failed" >> /var/log/sql_connection.log
fi

Add to cron for periodic testing:

crontab -e
# Add this line to run every 15 minutes
*/15 * * * * /path/to/sql_connection_test.sh