Tutorial

Connect to Microsoft SQL Server through an SSH tunnel

Forward a local port to SQL Server's TCP port with ssh -L, then connect to 127.0.0.1,port with a comma. Named instances, certificate checks and Windows Authentication each need a decision before it works.

Level
Intermediate
Reading time
9 min
Applies to
Windows, macOS, Linux, OpenSSH command line
Transputty steps checked against
Transputty 0.1 (development build, not released)

Before you start

  • An SSH login on a host that can reach the SQL Server, for example deploy@bastion.example.com
  • The SQL Server's host name and its TCP port as the SSH host sees them, for example sql.internal and 1433
  • A SQL Server login that uses SQL authentication, for example app_ro
  • sqlcmd, SQL Server Management Studio or another SQL Server client

Short answer

To connect to Microsoft SQL Server through an SSH tunnel, run ssh -N -L 11433:sql.internal:1433 deploy@bastion.example.com, then connect to 127.0.0.1,11433 (a comma, not a colon) with a SQL login: sqlcmd -S 127.0.0.1,11433 -U app_ro. Forward a fixed TCP port; instance names such as SQL01\REPORTING cannot be resolved through SSH.

The Transputty section near the end describes an SSH tunnel manager for Windows 11 that is in development. There is no public build yet; see release status. The rest of this page uses plain ssh and Microsoft's own tools. Never made a tunnel before? Do your first SSH tunnel on Windows first.

How far this page was tested. No SQL Server instance was available when this was written. The tunnel, the 127.0.0.1,port syntax and three error messages were run with sqlcmd against a real SSH server. Logins, encryption and authentication are written from Microsoft's documentation and were not run.

What is different about SQL Server?

A tunnel to SQL Server is ordinary SSH local port forwarding, and SQL Server speaks its protocol (TDS) over plain TCP, so it forwards cleanly. Three things differ from other databases: named instances use ports chosen at startup, current Microsoft drivers validate the server certificate by default, and Windows Authentication depends on the server's real name.

Why named instances need a fixed TCP port

When a client is given SQL01\REPORTING, it sends a UDP datagram to port 1434 on SQL01, and the SQL Server Browser service replies with the TCP port of the REPORTING instance. SSH port forwarding carries TCP connections only (RFC 4254 defines direct-tcpip channels and nothing for UDP), so that lookup cannot pass through the tunnel.

Skip the lookup and connect by port:

  1. Find the instance's TCP port. In SQL Server Configuration Manager open SQL Server Network Configuration, then Protocols for your instance, then TCP/IP, then the IP Addresses tab, and scroll to IPAll. TCP Dynamic Ports shows the current dynamic port; TCP Port shows a fixed one.
  2. Prefer a fixed port. Clear TCP Dynamic Ports, enter a number in TCP Port (for example 1433 or 14330) and restart the instance. A dynamic port can change after a restart and silently break your forward.
  3. Make sure TCP/IP is enabled for the instance and the server's firewall allows that port from the SSH host.

The command

ssh -N -L 11433:sql.internal:1433 deploy@bastion.example.com

sql.internal:1433 is resolved and dialled by the SSH host. -N starts no shell, so the command stays silent while the tunnel runs. If an SSH server runs on the SQL Server machine itself, the destination is 127.0.0.1:1433 and nothing crosses the network unencrypted.

Choosing the local port

Do not use local port 1433 if SQL Server (including Express or Developer edition) is installed on your own computer, because it is taken. Port 1434 looks tempting, but a local default instance uses TCP 1434 for its dedicated administrator connection, so it can be taken too. This page uses 11433.

Connect

With sqlcmd:

sqlcmd -S 127.0.0.1,11433 -U app_ro

The comma matters. SQL Server tools write a TCP endpoint as host,port. With a colon, the Microsoft ODBC driver did not treat the value as a TCP port in our test and failed with a Named Pipes error instead.

In SQL Server Management Studio, type 127.0.0.1,11433 in Server name, choose SQL Server Authentication and enter the login. The MSSQL extension for Visual Studio Code and Azure Data Studio take the same text in their server field. For other clients (SQLPro Studio, DBeaver, DataGrip) the host is 127.0.0.1 and the port is 11433 in separate boxes. Several of them have their own SSH tunnel option, which is simpler for one client; an external tunnel can be shared by several tools and can use jump hosts.

A connection string looks like this:

Server=tcp:127.0.0.1,11433;Database=app;User ID=app_ro;Password=...;Encrypt=True;HostNameInCertificate=sql.internal

Encryption and the certificate name

ODBC Driver 18, Microsoft.Data.SqlClient 4.0 and later, and SQL Server Management Studio 20 and later encrypt by default and validate the server's certificate. Through a tunnel two checks can fail:

  • Trust. Many servers still use the self-signed certificate SQL Server generates for itself. The error says the certificate chain was issued by an authority that is not trusted.
  • Name. The certificate names the server (sql.internal), and you connected to 127.0.0.1. The error says the target principal name is incorrect.

In order of preference:

  1. Tell the driver which name to expect. Set HostNameInCertificate=sql.internal in the connection string, or fill in Host name in certificate in the SSMS connection dialog. The driver still validates the chain and checks that name. This needs a certificate from a CA your computer trusts and a recent driver.
  2. Make the name resolve to the tunnel. Add 127.0.0.1 sql.internal to your hosts file and connect to sql.internal,11433. The name then matches without any driver option.
  3. TrustServerCertificate=True (the Trust server certificate box in SSMS, -C in sqlcmd). The connection is still encrypted, but the certificate is not validated at all. With a tunnel, the unvalidated stretch is the hop from the SSH host to SQL Server.

Encrypt=False (or Optional) turns off the requirement entirely; the login packet is still protected, the rest of the session is not. Avoid it when the SQL Server is on a different machine from the SSH host.

Windows Authentication through a tunnel

Expect to use SQL authentication. Integrated (Windows) authentication prefers Kerberos, and the client asks for a ticket for a service name built from the host and port it connected to. 127.0.0.1 with port 11433 matches no registered service name, so Kerberos does not happen. The driver may fall back to NTLM, which can succeed from a domain-joined PC with a valid session and fails from machines outside the domain. Treat integrated authentication as something to test in your own environment, not a default. The server must also have mixed mode ("SQL Server and Windows Authentication mode") enabled for a SQL login to work.

The same tunnel in Transputty

Transputty saves the forward as a connection and keeps the local port bound through reconnects. The steps follow version 0.1, a development build.

  1. Press Ctrl+N or click +. Under Start from a preset pick SQL Server. The connection is named sqlserver-tunnel with one "Local (-L)" rule, sql-server, from 127.0.0.1:1433 to localhost:1433.
  2. If 1433 is in use on your PC, Transputty moves the rule to the next free port and says so in a note, for example "1433 is in use by sqlservr.exe (PID 3312), so Transputty picked 1434."
  3. Fill in SSH server (Host, Port, User) and choose a method under Authentication.
  4. Under Forwarding rules set the Port next to Listen address to 11433 to match this page. Set Destination host to sql.internal (or 127.0.0.1 if SQL Server runs on the SSH server) and the destination Port to the instance's TCP port. Leave Listen address as 127.0.0.1.
  5. Click Save & connect and, on the first connection, compare the host key fingerprint and choose Trust and connect. If you chose Password, Transputty then asks for it.
  6. Connect your client to 127.0.0.1,11433.
The 'New connection' dialog. It opens with PostgreSQL selected, as in this capture; SQL Server (:1433) is the third preset. The blue note is the kind shown when a preset's usual port is taken. The app behind the dialog is connected to local test servers with fictional host names.

The preset only fills in port 1433. Transputty forwards TCP only and does not resolve instance names, so the Browser service limit above applies in the same way.

Troubleshooting

Error Meaning through a tunnel What to do
Error 26, Error Locating Server/Instance Specified The client was given host\instance and tried the Browser service over UDP 1434 Connect by port: 127.0.0.1,11433
Error 53, Named Pipes Provider: Could not open a connection to SQL Server [53] The client did not use TCP: a colon instead of a comma, or a bare name it could not resolve Use tcp:127.0.0.1,11433
Error 40, Could not open a connection to SQL Server Generic "could not reach the server". Nothing listens on the local port, or the wrong protocol was tried Check the tunnel is running and the port matches; see SSH tunnel connection refused
Error 10054, An existing connection was forcibly closed by the remote host, during pre-login The tunnel accepted the connection but the SSH host could not open the destination: wrong host or port, TCP/IP disabled for the instance, a firewall, or forwarding disabled on the SSH server Look for channel N: open failed in the ssh window; test the port from the SSH host
Error 18456, Login failed for user 'app_ro' The tunnel works and SQL Server answered. Wrong password, a disabled login, or mixed mode is off. The server's error log records a state number with the exact reason Fix the login; this is not a tunnel problem
Certificate errors See the encryption section above Set HostNameInCertificate, or accept the risk with TrustServerCertificate

If the connection drops after idle time, see SSH tunnel keeps disconnecting. If ssh cannot open the local port, see Local port already in use.

Common questions

How do I write the server name for SQL Server through an SSH tunnel?

Use the loopback address, a comma and the local port: 127.0.0.1,11433. SQL Server clients separate host and port with a comma, not a colon. In sqlcmd that is sqlcmd -S 127.0.0.1,11433 -U app_ro; in SQL Server Management Studio type the same text in the Server name box.

Can I connect to a SQL Server named instance through an SSH tunnel?

Yes, but by port, not by name. A name such as SQL01\REPORTING is resolved by asking the SQL Server Browser service over UDP port 1434, and SSH forwards only TCP. Find the instance's TCP port, ideally set it to a fixed port in SQL Server Configuration Manager, forward that port, and connect to 127.0.0.1,localport.

Why do I get a certificate error connecting to SQL Server on 127.0.0.1?

Recent Microsoft drivers encrypt by default and check that the server certificate matches the name you connected to. Through a tunnel that name is 127.0.0.1, which the certificate does not contain. Set HostNameInCertificate to the server's real name, or, only where you accept the risk, set TrustServerCertificate to skip validation.

Does Windows Authentication work through an SSH tunnel to SQL Server?

Usually not. Kerberos needs a service name built from the server's real host name and port, and a connection to 127.0.0.1 does not provide one. NTLM fallback may work from a domain-joined PC but often fails. Use a SQL Server login with SQL authentication for tunnelled connections unless you have tested integrated authentication in your environment.

How this tutorial was checked

2026-11-26: Only partly tested. No SQL Server was available, so no login, query, certificate check or authentication behaviour on this page was run; those parts are written from Microsoft's documentation. What was run, on Windows 11 with sqlcmd 15.0 (ODBC Driver 17) and the built-in ssh client (OpenSSH_for_Windows 9.5p2) against a real OpenSSH server on the same PC: sqlcmd reaching an ssh -L listener with the 127.0.0.1,port form, the 10054 'forcibly closed' error when the forward's destination was closed, the Named Pipes error 53 when a colon was used instead of a comma, and 'Error Locating Server/Instance Specified' for a server and instance name. The Transputty steps were written from the 0.1 source code and were not clicked through in the app.

Sources and further reading

Transputty keeps SSH tunnels running on Windows

Local, remote and SOCKS5 forwards in one window, automatic reconnection with ports that stay bound, and a clear reason when something fails. In development; no public build yet.