← Writing
DevOps

Systematic Analysis and Engineering Solution for Slow SSH Connections (Terminal + VSCode)

Publication Date: 15 April 2026
Latest Update Date: 15 April 2026
Author: Ailven LIU 刘翔
All Rights Reserved

An SSH connection that takes four to six seconds to open, then behaves perfectly once it is open, is a particular kind of problem. It rules out most of what people reach for first. The delay is not throughput, not the remote machine's load, and not the login shell. It is the cost of establishing a session, paid again on every connection.

The symptom

From a terminal, ssh user@host sits for four to six seconds before the prompt appears. A command that does no work at all costs the same:

time ssh host 'true'

That still returns in roughly five seconds. Once an interactive shell is up, however, everything inside it is responsive. That is the first real clue, because it separates session setup from session use — and only the former is broken.

VSCode Remote-SSH shows the same cause with worse symptoms. The initial connection is slow; the status bar lingers on Installing VSCode Server and Starting VSCode Server; extension loading and file-tree initialisation drag; and reconnection after a drop is unreliable.

Ruling out the usual suspects

Four explanations get offered first, and each can be tested in a few seconds.

Assumption Reality
Insufficient network bandwidth False
Poor remote server performance False
Heavy shell configuration (.zshrc) False
Git or proxy-related issues Irrelevant

Bandwidth is ruled out by the shape of the problem: a true command transfers no payload, so throughput cannot be what costs five seconds. Server load is ruled out by the interactive shell being responsive. The shell configuration is ruled out directly:

time ssh host '/bin/sh -c "true"'
time ssh host '/usr/bin/env -i /bin/sh -c "true"'

The second form strips the environment entirely. If the delay survives that, no amount of .zshrc tuning will touch it. It does survive it.

Where the time actually goes

A connection can be decomposed into five stages, and each has to be measured on its own before anything can be concluded:

[1] TCP connection establishment
[2] SSH handshake (KEX, cipher negotiation, authentication)
[3] PAM and session initialisation
[4] Shell startup
[5] Remote command execution

Stage 1 is healthy. A bare TCP probe returns in under ten milliseconds:

time nc -vz <host> 22

Stage 2 is healthy too. Under ssh -vvv <host>, the three checkpoints that matter — Connection established, SSH2_MSG_KEXINIT, Authenticated to ... — pass without any visible gap. Protocol negotiation is not the problem.

Stages 3 and 4 are cheap. Checking /etc/update-motd.d, /etc/pam.d/sshd and /etc/environment accounts for under 0.1 seconds in total, and the environment-stripped test above already excluded shell startup.

That leaves stage 5, and the measurement there is the one that reframes the problem:

time ssh host 'true'

Roughly five seconds for a command whose remote side does nothing. The cost is not in any stage of the work. It is in reaching the stage where work could begin.

Connection multiplexing

The fix is to stop paying that setup cost per connection. SSH ControlMaster lets the first connection become a master, with later connections riding on its transport:

ssh -M -S ~/.ssh/cm-host -fnNT host
ssh -S ~/.ssh/cm-host host 'true'

The first command opens the master in the background. The second reuses it, and the difference is immediate:

Operation Time
Initial master connection 2 to 3 seconds
Subsequent commands Around 1 second

Made permanent in ~/.ssh/config:

Host ali_server
  HostName <your_host>
  User <your_user>

  ControlMaster auto
  ControlPath ~/.ssh/cm-%r@%h:%p
  ControlPersist 10m

ControlMaster auto reuses an existing master when there is one and creates it otherwise, so the same ssh ali_server invocation works whether or not a master is already up. ControlPersist 10m keeps the master alive for ten minutes after the last session closes, which is what makes the reuse useful across a working session rather than only within a single command. Verification is a matter of running three commands in sequence and watching the first one pay the cost:

time ssh ali_server 'true'
time ssh ali_server 'echo OK'
time ssh ali_server 'echo AGAIN'

Two further settings cut the initial negotiation:

Host ali_server
  Compression no
  GSSAPIAuthentication no
  PreferredAuthentications publickey

Compression buys nothing on a link whose problem is round-trip count rather than volume, and GSSAPI authentication is a negotiation round trip that a public-key setup never uses. Restricting PreferredAuthentications to publickey skips the methods that would otherwise be tried and rejected first.

For VSCode, the important part is making the editor use the same client and therefore the same master:

{
  "remote.SSH.useLocalServer": true,
  "remote.SSH.enableDynamicForwarding": false,
  "remote.SSH.path": "/usr/bin/ssh"
}

useLocalServer routes Remote-SSH through the local SSH binary rather than a bundled one, which is what lets it find ControlPath. enableDynamicForwarding is disabled because VSCode opens a forwarding channel per connection by default, and those channels are exactly the short-lived connections the master exists to avoid.

Results

Scenario Before After
Single SSH command 4 to 6 seconds 1 to 2 seconds
Multiple SSH commands Each 4 to 6 seconds Approximately 1 second
VSCode Remote-SSH Very slow Significantly improved

The generalisation worth keeping is about diagnosis order. When a connection is slow to establish but responsive afterwards, the fault is in setup, and setup cost is multiplied by connection count. Multiplexing attacks that product directly, whereas tuning the network or the shell attacks factors that were never the problem. If more is needed, the same logic points at reducing connection frequency — long-lived sessions under tmux, or a persistent path such as WireGuard — rather than at making each connection marginally faster.