SSH, IDEs & port forwarding

Everything terminal- and IDE-related: wiring plain ssh, JetBrains and VS Code through Teleport, optional zsh tab-completion for node names, and port forwarding.

1. tsh config — wiring ssh and IDEs to Teleport

tsh config emits an OpenSSH-style ssh_config snippet that uses tsh proxy ssh as a ProxyCommand. Once that snippet is wired into your SSH configuration, plain ssh user@host.cluster — and anything that speaks SSH: JetBrains, VS Code Remote-SSH, scp, rsync, ansible — routes through Teleport using your short-lived cert.

1.1. One-time setup

Keep the generated block in its own file, ~/.ssh/teleport_config, and reference it from ~/.ssh/config with an Include — that way you can regenerate it any time without touching the rest of your SSH config:

tsh config > ~/.ssh/teleport_config
grep -qF 'Include ~/.ssh/teleport_config' ~/.ssh/config 2>/dev/null || \
  printf 'Include ~/.ssh/teleport_config\n%s\n' "$(cat ~/.ssh/config 2>/dev/null)" > ~/.ssh/config

(The second command inserts the Include line once, at the top of ~/.ssh/config — important, because an Include placed after a Host block would inherit that block’s context, and ssh options are first-match-wins.)

The generated file contains blocks like:

# Begin generated Teleport configuration for teleport.better.care by tsh
Host *.teleport.better.care !teleport.better.care
    Port 3022
    ProxyCommand "/usr/local/bin/tsh" proxy ssh --cluster=teleport.better.care --proxy=teleport.better.care:443 %r@%h:%p
# End generated Teleport configuration

Re-run tsh config > ~/.ssh/teleport_config whenever the cluster topology changes (new leaf clusters appear, etc.) — and re-apply the optional customisation below if you use it, since regeneration overwrites the file. The certificate refresh itself is automatic — tsh proxy ssh re-uses whatever’s in ~/.tsh/.

1.2. Optional — ssh <node>.teleport with the login predefined

Out of the box you type the full form with an explicit user:

ssh better@internal-tools.teleport.better.care

With two small additions to ~/.ssh/teleport_config this shrinks to:

ssh internal-tools.teleport

What the additions are (manual edit):

  1. A User better line inside the Host *.teleport.better.care !teleport.better.care block — makes the login optional for the full form. better is the shared service account used on better.care-managed servers; on a leaf cluster substitute the predefined account for that estate.

  2. A new alias block at the end of the file. HostName %h.better.care rewrites the short name you typed (%h) to the full form, and the ProxyCommand must use %h (the rewritten name) — not %n (the name as typed), which tsh would not recognise:

    # Short alias: ssh <node>.teleport -> <node>.teleport.better.care as better
    Host *.teleport
        HostName %h.better.care
        User better
        UserKnownHostsFile "<copy from the generated block above>"
        IdentityFile "<copy from the generated block above>"
        CertificateFile "<copy from the generated block above>"
        Port 3022
        ProxyCommand "/usr/local/bin/tsh" proxy ssh --cluster=teleport.better.care --proxy=teleport.better.care:443 %r@%h:%p

An explicit user still wins when you need one: ssh root@node.teleport overrides the configured User.

Or run this instead (does both edits for you). Set TUSER to your Teleport username and paste the whole block into a terminal — it regenerates the file, injects the User line, and appends the alias block, copying the credential paths and tsh location from the generated content (so it works regardless of where your home directory or tsh binary lives):

TUSER=better
CFG=~/.ssh/teleport_config

tsh config > "$CFG"

# 1) default user for the full *.teleport.better.care form
perl -i -pe 's/^(Host \*\.teleport\.better\.care !teleport\.better\.care)$/$1\n    User '"$TUSER"'/' "$CFG"

# 2) short alias block (capture first, then append — grep refuses to
#    write into the same file it is reading)
CREDS=$(grep -m3 -E 'UserKnownHostsFile|IdentityFile|CertificateFile' "$CFG")
PROXY=$(grep -m1 -E 'ProxyCommand.*--cluster=teleport\.better\.care' "$CFG" | sed 's/%r@%n:%p/%r@%h:%p/')

cat >> "$CFG" <<EOF

# Short alias: ssh <node>.teleport -> <node>.teleport.better.care as $TUSER
Host *.teleport
    HostName %h.better.care
    User $TUSER
$CREDS
    Port 3022
$PROXY
EOF

(The sed in step 2 also fixes configs generated by older tsh versions, which emitted %n instead of %h in the ProxyCommand.)

Verify without connecting, then for real:

ssh -G internal-tools.teleport | grep -E '^(user|hostname|port) '
# expect: user better / hostname internal-tools.teleport.better.care / port 3022
ssh internal-tools.teleport
Re-running the snippet is safe — it always starts from a fresh tsh config — and it’s also what you do after any regeneration. The leaf-cluster short forms (.malta, .somerset) already exist in the generated file and are not affected.

1.3. Use cases

Plain SSH:

ssh better@<node>.teleport.better.care
ssh <node>.teleport        # with the optional short alias from <<short-alias>>

JetBrains IDEs (Remote Development / Connect via SSH): enter the host as <node>.<cluster-name> — the IDE will use your ~/.ssh/config and pick up the Teleport ProxyCommand.

VS Code Remote-SSH: add the same host string in the Remote Explorer panel; the extension reads ~/.ssh/config.

File transfer through the same ssh_config block (works because scp and rsync shell out to OpenSSH):

scp local.txt better@<node>.teleport.better.care:/tmp/
rsync -av ./src/ better@<node>.teleport.better.care:/srv/app/

Teleport-native alternative — no ssh_config required:

tsh scp local.txt better@<node>:/tmp/
tsh scp better@<node>:/var/log/app.log .

1.4. Gotchas

  • "permission denied" usually means your cert expired. Check with tsh status and re-run tsh login.

  • If ssh complains about the host pattern not matching, re-run tsh config > ~/.ssh/teleport_config — the cluster name may have changed. Re-apply the Optional — ssh <node>.teleport with the login predefined snippet if you use it.

2. Tab-completion of node names in tsh ssh — zsh (optional)

This is an optional convenience for zsh users on macOS who use tsh ssh from the terminal frequently. Skip this section if you live in Teleport Connect, JetBrains, or VS Code. The script is zsh-specific (compdef, compadd, ${(f)…​}) — bash is not supported.

What it does: caches the full node list from every online (root
leaf) cluster into ~/.cache/teleport_nodes, and adds those names as <TAB> completions for tsh ssh and tsh scp on top of the default tsh completion. Handles user@<TAB> correctly (only completes the hostname portion).

Dependency: jq. Most macOS engineers already have it; if not, install it separately (e.g. via your usual package manager).

Install: append the script below to ~/.zshrc (or to a file under ~/.config/zsh/ that you source), then exec zsh (or open a new terminal).

Use: run tsh-refresh after tsh login, or whenever the cluster’s node list changes. Then tsh ssh <TAB> completes node names, and tsh ssh user@<TAB> completes after the @.

# Teleport node completion for `tsh ssh`
# Run `tsh-refresh` after login or when new nodes are added
function tsh-refresh() {
  mkdir -p ~/.cache
  local -a clusters
  clusters=($(tsh clusters --format=json 2>/dev/null | jq -r '.[] | select(.status=="online") | .cluster_name'))
  {
    for cluster in "${clusters[@]}"; do
      tsh ls --cluster="$cluster" --format=names 2>/dev/null
    done
  } | sort -u > ~/.cache/teleport_nodes
  echo "Cached $(wc -l < ~/.cache/teleport_nodes | tr -d ' ') nodes across ${#clusters[@]} clusters"
}

function _tsh_with_nodes() {
  _tsh "$@"
  if [[ ${words[2]} == "ssh" || ${words[2]} == "scp" ]] && [[ -f ~/.cache/teleport_nodes ]]; then
    local -a nodes
    nodes=(${(f)"$(< ~/.cache/teleport_nodes)"})
    # Strip user@ prefix so matching works against just the hostname
    if [[ $PREFIX == *@* ]]; then
      local user_prefix="${PREFIX%%@*}@"
      compadd -p "$user_prefix" -- "${nodes[@]}"
    else
      compadd -- "${nodes[@]}"
    fi
  fi
}
compdef _tsh_with_nodes tsh
The cache is a snapshot — re-run tsh-refresh whenever nodes are added or removed. Stale completion entries mean the cache is out of date, not that Teleport is broken.

3. Port forwarding through Teleport

3.1. TCP local forwarding to an SSH node

Same -L semantics as OpenSSH — forward a local port to a remote address reachable from the node:

tsh ssh -L 5432:localhost:5432 better@<node>

3.2. Dynamic / SOCKS5

tsh ssh -D 1080 better@<node>

Then point a browser or curl --socks5 127.0.0.1:1080 …​ at it.

3.3. Database tunnels

Databases have their own, simpler mechanism — tsh proxy db --tunnel for PostgreSQL/MySQL and tsh proxy app for Oracle/SQL Server. See Databases.

3.4. Kubernetes port-forward

After tsh kube login <cluster>, plain kubectl port-forward works — Teleport is transparent at that point:

tsh kube login <cluster>
kubectl port-forward svc/<svc> 8080:80