Speed Up SSH Connections in Linux
In this article
To speed up SSH connections in Linux, you can reduce connection latency and authentication overhead by enabling SSH connection multiplexing, tweaking client-side keepalive settings, and switching to a faster authentication method like SSH keys. These changes cut both the time to establish a new connection and the delay caused by network or DNS lookups.
Why SSH Connections Feel Slow
SSH connections can feel slow for two main reasons: the time it takes to establish the encrypted session and the time it takes to authenticate. When you run ssh user@host, your client and the server perform a TCP handshake, exchange encryption keys, and then authenticate. Each step adds a round trip, and if you are connecting to a server across the internet, each round trip can take 100–200 ms. If you connect frequently, those delays add up.
Another common culprit is DNS. By default, the SSH server on Linux tries to resolve the client's IP address to a hostname using reverse DNS. If the DNS server is slow or unreachable, this can add a noticeable delay before you even see the password prompt. The client can also suffer from DNS delays if you use a hostname that requires a lookup.
Enable SSH Connection Multiplexing
Connection multiplexing is the single biggest win for people who open multiple SSH sessions to the same server. It reuses an existing TCP connection for new sessions, so you skip the TCP handshake, key exchange, and authentication entirely for subsequent connections. The effect is dramatic: after the first connection, new sessions open almost instantly.
To enable it, add these lines to ~/.ssh/config:
Host *
ControlMaster auto
ControlPath ~/.ssh/controlmasters/%r@%h:%p
ControlPersist 10mThen create the directory with mkdir -p ~/.ssh/controlmasters. The ControlPersist 10m option keeps the master connection alive for 10 minutes after the last session closes, so even a new connection within that window reuses the socket. You can adjust the timeout to your liking; a longer time uses a bit of memory but speeds up repeated connections.
One caveat: if you use SSH for port forwarding or tunneling, multiplexing can sometimes interfere with forwarding rules. In that case, you can disable it for specific hosts by adding a separate Host block with ControlMaster no.
Use SSH Keys Instead of Passwords
Password authentication is slow because it requires a round trip to verify the password, and it is also less secure. SSH keys use public-key cryptography and are both faster and safer. To set up key-based authentication, generate a key pair on your local machine with ssh-keygen -t ed25519 (or rsa -b 4096 if you need compatibility with older servers), then copy the public key to the server with ssh-copy-id user@host. After that, SSH will authenticate using the key without prompting for a password.
If you use a passphrase on your key, you can add the key to an SSH agent with ssh-add so you only enter the passphrase once per login session. This avoids typing your password every time and also makes multiplexing more effective because the agent caches the key.
Disable DNS Lookups on the Server
If the server is slow to show the password prompt or the login banner, the server might be doing a reverse DNS lookup on your IP address. You can disable this in the SSH server configuration file /etc/ssh/sshd_config by setting:
UseDNS no
After changing the file, restart the SSH service with sudo systemctl restart sshd (or sudo service ssh restart on older systems). This is safe for most setups; the only downside is that the server will not resolve your hostname for logs, which is rarely important.
On the client side, you can also tell SSH not to use DNS for hostname resolution by using an IP address directly, but that is less convenient. If you must use a hostname, ensure your local DNS is fast.
Tune Keepalive and Timeout Settings
If your SSH connections frequently hang or drop after a period of inactivity, the issue is often a firewall or NAT device that kills idle connections. SSH has built-in keepalive messages that can prevent this. Add these lines to ~/.ssh/config:
Host *
ServerAliveInterval 60
ServerAliveCountMax 3This sends a keepalive packet every 60 seconds and gives up after 3 unanswered packets. It keeps the connection alive and also detects dead connections faster. On the server side, you can set ClientAliveInterval and ClientAliveCountMax in sshd_config to achieve a similar effect for all clients.
Another useful setting is ConnectTimeout, which limits how long the client waits for the TCP connection to be established. For example, ConnectTimeout 10 will abort after 10 seconds instead of hanging for minutes.
Choose a Faster Cipher and Compression
Modern SSH versions use the ChaCha20-Poly1305 or AES-GCM ciphers, which are fast on most hardware. You can force a specific cipher in your client config with Ciphers chacha20-poly1305@openssh.com or Ciphers aes128-gcm@openssh.com. This rarely makes a noticeable difference on modern CPUs, but it can help on very slow or embedded devices.
Compression can speed up connections if you are transferring highly compressible text, but it can slow things down on fast networks because the CPU overhead outweighs the bandwidth savings. By default, compression is off. You can enable it per connection with ssh -C user@host or in the config with Compression yes. Test both ways; for most interactive use, compression is not worth it.
Common Mistakes and Misconceptions
- Multiplexing is not a security risk by itself, but it does mean that if an attacker gains access to your Unix socket, they could hijack sessions. Keep your home directory permissions correct (700 on
.ssh) and only enable multiplexing on machines you trust. - Disabling DNS on the server does not affect the client's ability to resolve the server's hostname; it only stops the server from resolving the client's IP.
- Changing ciphers can break compatibility with very old servers. Stick to the defaults unless you know both ends support the cipher.
- Keepalive settings do not speed up the initial connection; they only prevent drops during idle periods.
Frequently Asked Questions
Why is my SSH connection slow even with keys?
If you already use keys and still see delays, the bottleneck is likely network latency or the server's reverse DNS lookup. Check if the delay happens before or after the password prompt. If it is before, try UseDNS no on the server. If it is after, the server may be slow to start your shell or load your profile.
Can I speed up SSH for a single command?
Yes, you can use connection sharing (multiplexing) or simply run the command with ssh -o ControlMaster=auto -o ControlPath=... user@host 'command'. For one-off commands, the overhead is mostly the TCP handshake and key exchange, which multiplexing eliminates if you already have an open session.
Does using a different port speed up SSH?
No. The port number does not affect performance. Changing the default port is a security measure to reduce automated attacks, not a speed improvement.
Is it safe to disable DNS lookups?
Yes, in most cases. The only downside is that the server logs will show IP addresses instead of hostnames, and some features like AllowUsers with hostname patterns may not work as expected. For a typical home or small business server, it is safe.
What is the best SSH key type for speed?
Ed25519 keys are fast to generate and verify, and they are supported by OpenSSH 6.5 and later. If you need to connect to very old servers, RSA with 4096 bits is a safe fallback, but it is slower to generate. The key type does not significantly affect connection speed after the key is loaded.