Skip to content

What Changes When stunnel Wraps SSH in TLS? Linux and Windows Examples

Interactive Diagram

새 탭 ↗

SSH is still SSH when it listens on port 443. In the article on accepting SSH on port 443, OpenSSH accepts the connection directly and starts speaking SSH. Put stunnel in front of it and two things change: which process accepts the external connection, and which protocol runs on that leg.

The example uses ssh.example.com:8443 as the external TLS endpoint on a pre-approved test path. The focus is on which process handles each connection on Linux and Windows, and where to look when authentication fails.

A port helps select the socket that accepts a connection. The program that accepts it determines what protocol comes next. In a direct connection, OpenSSH starts speaking SSH on the incoming socket. With stunnel, the server-side stunnel terminates TLS and relays SSH bytes to OpenSSH behind it.[1][2]

Direct SSH connects to OpenSSH. With stunnel, the outer TLS session exists only between the two stunnel processes.

Illustration by noisonnoiton. Blue marks legs that carry SSH; green marks the outer TLS leg.

QuestionDirect OpenSSHOpenSSH behind stunnel
Which process accepts the external connection?OpenSSHServer-side stunnel
What is exchanged first on that connection?SSH version stringsTLS handshake messages
Which connection does OpenSSH see?The TCP connection opened by the SSH clientA separate TCP connection opened by server-side stunnel

The data inside TLS is still SSH. stunnel does not construct or handle HTTP requests. Calling this path HTTPS or an HTTP proxy just because it uses TLS misidentifies the protocol being carried.[1][2]

The SSH client connects to the client-side stunnel listener on its own host’s loopback interface. That stunnel opens a TLS session to its server-side peer at ssh.example.com:8443. The server-side stunnel passes the SSH bytes to OpenSSH on the server’s loopback interface. The SSH client and OpenSSH, not the stunnel processes, are the endpoints of SSH key exchange and user authentication.[1][2]

The two stunnel processes negotiate TLS. The SSH client and OpenSSH check the host key and authenticate the user over the relayed stream.

Diagram by noisonnoiton. This PlantUML diagram shows the logical flow, not a packet capture.

  • TLS server authentication: The client-side stunnel uses verifyChain and the configured trust anchors to validate the certificate chain. checkHost separately verifies that the certificate covers the expected name, ssh.example.com. The private key for the TLS certificate stays on the server that runs server-side stunnel.[1][3]
  • SSH host authentication: OpenSSH proves its server identity with its SSH host key. The SSH client checks that key against the key it expects in known_hosts. When connecting to a local listener, it can use HostKeyAlias to look up the key for ssh.example.com. The SSH host private key also stays on the server.[8][9]
  • SSH user authentication: OpenSSH checks that the user holds the private key corresponding to a public key authorized for the account. The user’s private key stays on the client side. A valid TLS server certificate does not grant access to an SSH account.[5][6][9]

Both examples put a TLS listener at ssh.example.com:8443 and an SSH listener on the server’s loopback interface. What changes across platforms is how the services are managed and where the OpenSSH configuration lives, not the protocols.[2][5][6]

ItemLinux (Ubuntu example)Windows
External TLS listenerstunnel runs in server mode. accept is the external listener; connect points to OpenSSH on loopback. cert and key supply the TLS credentials.[1][2]The same server-mode settings apply. stunnel runs as a separate Windows service from OpenSSH.[1][2][4]
Internal SSH listenerWhen sshd listens directly, ListenAddress and Port in sshd_config specify the binding.[9] If a systemd socket owns the listener instead, inspect the socket for the effective binding.[10]OpenSSH Server reads %ProgramData%\ssh\sshd_config by default. sshd accepts the internal SSH connection.[5][7][9]
Service managementUbuntu documents the OpenSSH unit as ssh.service. This example uses a package-provided systemd unit for stunnel.[2][6]Microsoft documents sshd as the OpenSSH Server service name. The stunnel manual distinguishes -install and -start for its separate Windows service.[1][7]

The client-side settings follow the same design in both examples. In client mode, accept stays on loopback and connect points to the external TLS endpoint. The SSH client dials the local listener’s port, not the server’s external port 8443.[1][2]

Last observed stageWhere to look
External TCP does not connectCheck the approved path and confirm that server-side stunnel is listening on 8443. Without a TCP connection, the TLS handshake cannot start.
TCP connects, but the TLS certificate is rejectedCheck the client-side stunnel log. A CA chain error is different from a name mismatch for ssh.example.com. Fix the cause rather than disabling verification.[3]
TLS succeeds, but the SSH host key is rejectedCheck which name the SSH client looked up in known_hosts. Do not accept an unexpected key before verifying it through a trusted channel.[8]
The host key checks out, but public-key authentication is rejectedCheck which user key the SSH client offered and which public keys OpenSSH authorizes for the target account.[5][6][8]

If TLS succeeds but there is no SSH version exchange, first check whether server-side stunnel reached the internal OpenSSH listener. TLS success does not establish that the back-end SSH listener is healthy.[1][2]

An independent stunnel listener cannot simply bind to the same IP address and TCP port already occupied by an HTTPS listener. Running both roles requires separate bindings or an approved listener design. Get approval for the path, listener, and intended use before operating it.

A Linux loopback test with stunnel 5.72 and OpenSSH 9.6p1 confirmed SSH public-key authentication and command execution after TLS verification. The test rejected an untrusted CA, a mismatched TLS hostname, an unexpected SSH host key, and an unauthorized user key in separate cases. It did not exercise installed systemd services or an external network path.

The Windows example maps the server roles from official stunnel and Microsoft documentation. Windows service startup and connections were not tested. The official documentation was checked on 2026-11-30.

[1] stunnel Manual

[2] stunnel HOWTO

[3] stunnel Authentication

[4] stunnel Windows Configuration

[5] Microsoft: OpenSSH Server configuration

[6] Ubuntu Server: OpenSSH server

[7] Microsoft: Get started with OpenSSH for Windows

[8] OpenSSH: ssh_config

[9] OpenSSH: sshd_config

[10] Ubuntu Server: OpenSSH socket activation