Routing SSH traffic with Traefik v2

Is there a document on how to route such ssh traffic with traefik v2?
many thanks!


Thanks Idez! Is there an example for it. And I understand that routing tcp traffic like ssh wont work base on domain. It should be per port routing.

Exactly, yeah. SSH doesn’t use TLS, therefore we cant route SSH based on domain. You have to create an own entrypoint (port) then per SSH you want to proxy basically.

Hi SantoDE,

Do you have any example?

Sadly, not at hand. I'll add that to my ToDo list :slight_smile:

What you will need is a seperate entrypoint and a seperate router attached to each of this entrypoints ( The rule in the router then has to be rule = "Host(`*)"

then it should work :slight_smile:

Here is a "quick-and-dirty" example with Gitea, a ligthweight Git Server,
which exposes both HTTP and SSH using Traefik v2.0.0-beta1.

The SSH part is handle through a TCP router.

version: '3'

    image: traefik:v2.0.0-beta1
      - --providers.docker
      - --entrypoints.http.address=:80
      - --entrypoints.ssh.address=:22
      - --api
      - "80:80"
      - "8080:8080"
      - "22:22"
      - "traefik.enable=false"
      - "/var/run/docker.sock:/var/run/docker.sock"

    image: gitea/gitea:1.9
      - USER_UID=1000
      - USER_GID=1000
    restart: unless-stopped
      - "traefik.enable=true"
      - "traefik.http.routers.gitea-web.rule=Host(`localhost`)"
      - "traefik.http.routers.gitea-web.entrypoints=http"
      - "traefik.http.routers.gitea-web.service=gitea-web-svc"
      - ""
      - "traefik.tcp.routers.gitea-ssh.rule=HostSNI(`*`)"
      - "traefik.tcp.routers.gitea-ssh.entrypoints=ssh"
      - "traefik.tcp.routers.gitea-ssh.service=gitea-ssh-svc"
      - ""

Hope it helps.


Hello, regarding the HostSNI rule on TCP routers, could we use the subdomain? I have tried and see it works with a wildcard (*) rule but failed on domain rule, here is my example

## Static configuration
    address = ":80"
    address = ":2222"

## Dynamic configuration
    entryPoints = ["ssh"]
    rule = "HostSNI(``)" # <~~ this one will terminal any ssh request from putty
    service = "ssh"

      address = ""
1 Like

Hi @nhvu1988, using the rule HostSNI() with something else than the wildcard (*) does not work with SSH.

The reason is that SSH does not support SNI (Server Name Indication):

So the only possibility is to have one SSH service per entrypoint (e.g. per port).

1 Like

If I use the below it gives me a red exclamation mark for servers for the URL. Everything else is green.

      address = "192.168.*.*:22"

Saying that "SSH does not support SNI" is correct but a bit misleading. The thing is, SNI is a TLS protocol term, and SSH has nothing to do with TLS protocol.

Traefik supports routing per host via SNI because it's the only standard way to do that over TCP, but TCP itself does not have SNI, so you have to use TLS.

Now the question, how the heck can you use TLS for an arbitrary TCP connection? If you start googling you will see that TLS is most often associated with protocols HTTPs, IMAP, STMP and others.

SSH is certainly not among them. Hence, the conclusion that you can only have one SSH service per entrypoint.

You can workaround this if you wrap your SSH connection in TLS with stunnel, I tried it and it works. It's just not very practical, because you will have to run stunnel on every SSH client to do the wrapping, and you still will have to use multiple ports, only this time on the client.


Hi! I'm a bit late to the party, but found this via google, trying to do the same thing.
For those who have not found a suitable solution, maybe you could use sslh, which is an ssh/tls multiplexer. I have it working and just wanted to see whether I can skip sslh from now on, now that I found traefik. So the answer is no, I'll be using sslh together with traefik.

sslh can distinguish several protocols and forward connection attempts to the configured handler:
ssh, openvpn, tinc, xmpp, http, tls, ssl, adb, socks5, other.

You can find it at or in the repo of the linux distribution of your choice.

1 Like

HAProxy allows you to do this without requiring an additional tool (sslh). An example can be found here:

Or perhaps easier to digest

Short: This is achieved is by inspecting the TCP stream, if it starts with SSH-2.0 which conform RFC-4253 ...


Ping @juliens on this (peaking first SSH bytes), as food for thought.

Challenge on this, @darkl0rd, is the maintenance effort around the bazilions protocol that could have a specific "bytes peak" and an associated behavior.

I do agree, if you do pursue this path I think you should restrict it to only SSH or make it flexible so that it can be configured by the end user what string to look for when peeking.

Have you a working exemple ?