Why is my repeated SSH connection so slow, and how do I fix it?

Quick Linux Tip #16:

Try:

cat >> ~/.ssh/config << 'EOF'
Host *
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m
EOF

Info: SSH multiplexing reuses the first connection for subsequent sessions. ControlMaster creates a master socket, while ControlPersist keeps it alive for 10 minutes.

Examples:

  • $ ssh -O check user@server  # Verify master exists
  • $ ssh -O stop user@server  # Close master connection
  • $ ssh -O exit user@server  # Force close

Note: Massive speedup for scripts/CI/CD that use SSH repeatedly. The second connection was 48x faster in the example above.




LinuxTeck.com
linuxteck@ubuntu:~$ cat >> ~/.ssh/config << 'EOF'
Host *
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m
EOF

linuxteck@ubuntu:~$ time ssh server 'exit'
real    0m2.345s
user    0m0.023s
sys     0m0.012s

linuxteck@ubuntu:~$ time ssh server 'exit'
real    0m0.048s
user    0m0.008s
sys     0m0.004s

linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #15: How do I delete 100,000 tiny cache files quickly? NEXT ARTICLE Quick Linux Tip #17: Are my source and backup directories truly identical?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.