SSH Encryption & Connection Process Explained
In this article
SSH (Secure Shell) lets you securely connect to a remote computer over an unsecured network, encrypting all data between your client and the server. This guide walks through the exact handshake, key exchange, and encryption steps that happen when you run an SSH command, so you understand what protects your connection and how to troubleshoot it.
What You'll Need
- A computer with an SSH client (OpenSSH is preinstalled on macOS, Linux, and Windows 10/11).
- A remote server with an SSH server running (e.g., OpenSSH daemon).
- Network access to the server's IP address or hostname, and port 22 (unless changed).
- Optional: a key pair (public and private) if you're using key-based authentication.
Step-by-Step
- Initiate the connection – Run
ssh user@hostnamein your terminal. Your client sends a TCP connection request to port 22 on the server. - Server sends its identification – The server responds with its SSH protocol version and a list of supported algorithms (key exchange, encryption, MAC, compression). The client checks this against its own supported list and picks the most secure common option.
- Key exchange (KEX) begins – Both sides generate ephemeral key pairs. The client sends its ephemeral public key and a random number; the server does the same. Using a method like Diffie-Hellman or Elliptic Curve Diffie-Hellman, they independently compute a shared secret — this becomes the session key.
- Server authentication – The server signs the exchange hash (a digest of all messages so far) with its host private key. The client verifies the signature using the server's public key, which it either has stored (in
known_hosts) or is shown for the first time with a fingerprint prompt. - Derive session keys – From the shared secret and the exchange hash, both sides derive symmetric encryption keys, an integrity key, and an IV. These are used for the rest of the session.
- Client authentication – The server asks the client to prove its identity. You can use a password (encrypted now), a public key (the client proves possession of the private key), or another method like keyboard-interactive. With key-based auth, the client signs a challenge with its private key, and the server verifies it against the public key in
authorized_keys. - Secure channel established – Once authenticated, the client and server start an encrypted session. All subsequent data (commands, output, file transfers) is encrypted with the negotiated symmetric cipher (e.g., AES-256-GCM) and protected with a MAC.
- Connection lifecycle – The session stays open until you type
exitor the connection drops. The session key is discarded when the connection closes; a new one is created for each connection.
Common Mistakes
- Ignoring the host key prompt – On first connection, SSH asks you to verify the server's fingerprint. Accepting without checking can expose you to a man-in-the-middle attack if the fingerprint doesn't match the server's actual key.
- Using weak ciphers – Some older servers default to legacy algorithms like
aes128-cbcorhmac-sha1. These are vulnerable; modern OpenSSH disables them, but if you're connecting to an old system, you may need to update it. - Leaving passwords enabled – Relying on passwords alone makes brute-force attacks easier. Use key-based authentication and disable password login if possible.
- Incorrect permissions on key files – If your private key (
~/.ssh/id_rsa) has permissions that are too open (e.g., 0644), SSH will refuse to use it. It must be 0600. - Mixing up public and private keys – The private key stays on your local machine; the public key goes on the server. Putting the private key on the server defeats the purpose.
Tips
- Use SSH agent forwarding sparingly — it can expose your private key to the remote server if that server is compromised.
- Enable two-factor authentication (2FA) on your SSH server for an extra layer of security.
- Keep your SSH software updated; both client and server should be recent versions to support the latest algorithms.
- Use
ssh -vto see verbose output of the handshake — it helps you understand which algorithms are being used and where a failure occurs. - For automated connections, use a dedicated key with a passphrase and an agent, rather than a passwordless key.
Troubleshooting
Connection refused – The server isn't listening on port 22, or a firewall is blocking it. Check that the SSH daemon is running (sudo systemctl status ssh on Linux) and that port 22 is open in your firewall.
Host key verification failed – The server's key has changed (reinstalled OS, new key) or you're being attacked. If you're sure the server is legit, remove the old key from ~/.ssh/known_hosts using ssh-keygen -R hostname, then reconnect and verify the new fingerprint.
Permission denied (publickey) – The server doesn't have your public key, or your private key isn't accessible. Check that your public key is in ~/.ssh/authorized_keys on the server and that your private key has correct permissions (0600).
Connection hangs or times out – Network issues, DNS problems, or a firewall dropping packets. Try connecting via IP address, check your network, and ensure the server is reachable (ping hostname).
Algorithm negotiation failed – Client and server can't agree on a common algorithm. This often happens with very old or very new versions. Update both sides, or explicitly specify algorithms with -o options (e.g., ssh -o KexAlgorithms=diffie-hellman-group14-sha1).
Frequently Asked Questions
What is the difference between SSH and SSL/TLS?
SSH is designed for secure remote login and command execution, while SSL/TLS is used to secure web traffic (HTTPS) and other protocols. They use similar cryptographic principles but different handshakes and purposes.
Is SSH safe to use over public Wi-Fi?
Yes, because SSH encrypts all data, including passwords and commands. However, you should still verify the host key to prevent man-in-the-middle attacks.
How does SSH protect against eavesdropping?
All data is encrypted with a symmetric cipher (like AES) using a session key that is derived during the key exchange. Only the client and server know this key.
What is a host key fingerprint?
It's a hash of the server's public key, displayed as a short string (e.g., SHA256:...). You compare it with the server's actual fingerprint to confirm you're connecting to the right machine.
Can SSH be used for more than just shell access?
Yes, SSH can tunnel other protocols (port forwarding), securely copy files (SCP, SFTP), and even mount remote filesystems (SSHFS).
Why does SSH sometimes ask for a passphrase for my key?
If your private key is encrypted with a passphrase, SSH asks for it each time you use the key. You can use an SSH agent to cache the passphrase for the session.