What Is SSH? A Step-by-Step Walkthrough of a Remote Login
This article explains the SSH protocol by tracing a remote login, covering the three protocol layers, Curve25519 key exchange, server host-key verification, encryption onset, forward secrecy, public-key authentication, channel multiplexing, and real-world debugging with ssh -v.
Reading font
What Does SSH Do Between Two Machines?
SSH stands for Secure Shell, used for secure remote login, command execution, and file transfer over untrusted networks. The common OpenSSH implementation on Linux uses the local ssh client to connect to the remote sshd service. In the example, ops is the remote account; the server listens on TCP port 22 by default, and a different port can be specified with -p.
SSH operates in three layers: the Transport Layer negotiates keys, verifies the server, and protects packets; the User Authentication Layer confirms which account may log in; and the Connection Layer opens multiple logical channels of different purposes within the same encrypted connection.
SSH public-key login with Curve25519. Authentication and commands below the blue line are transmitted inside the encrypted connection.
How Do Both Sides Derive the Same Secret?
After TCP connects, both sides exchange version identifiers in the form SSH-2.0-…, then use SSH_MSG_KEXINIT to exchange their supported algorithm lists, from which they select the key-exchange, host-signature, encryption, and integrity-protection schemes for this session. During the initial handshake, an onlooker on the network can see the version identifiers and algorithm lists.
Taking curve25519-sha256 as an example, the client and server each generate an ephemeral key pair and send their ephemeral public keys to each other. Let the client's ephemeral private and public keys be a and A, and the server's be b and B. Each side computes using its own ephemeral private key and the peer's ephemeral public key.
Client X25519(a, B) = K
Server X25519(b, A) = KFrom the curve arithmetic perspective, both sides perform two scalar multiplications on the same base point; swapping the order yields the same result. This produces the shared secret K. Even if an eavesdropper obtains A and B, it is computationally infeasible to derive K from those two public values.
How Does the Client Verify the Server's Identity?
Both sides compute an exchange hash H from the version identifiers, algorithm-negotiation messages, host public key, exchanged values, and shared secret. The server signs H with its long-term host private key and sends the host public key and signature to the client. Because the signature covers the handshake data, the client can confirm that the handshake was indeed performed by the server possessing that host private key.
Verification requires a prerequisite: the client must trust the received host public key. Otherwise, a man-in-the-middle could establish separate encrypted connections with both ends and sign with its own key. OpenSSH typically stores trusted host public keys in the local ~/.ssh/known_hosts. On first connection, the fingerprint shown is a hash of the host public key, which can be verified via a trusted channel such as a cloud console. Later, if you see REMOTE HOST IDENTIFICATION HAS CHANGED!, first confirm whether the server was reinstalled or its host key rotated, then decide whether to update the record.
From Which Moment Does Encryption Begin?
Both sides derive transport keys from K, H, and the session identifier, using a separate key set for each direction. The H from the first exchange also serves as the session identifier for the entire connection. After each side sends SSH_MSG_NEWKEYS, the next message it sends uses the new keys.
Subsequent packets are typically protected by symmetric algorithms. For example, both AES-GCM and ChaCha20-Poly1305 encrypt data and verify integrity simultaneously. After encryption, a packet capture still reveals the two endpoints' IPs, ports, and traffic volume. Long-lived connections can also re-key and update transport keys. Like the ephemeral key exchange above, when ephemeral private keys are destroyed after use, even if the long-term host private key is later compromised, past sessions remain confidential — a property called forward secrecy.
How Does the Server Confirm You Are Authorized to Log In?
Once the encrypted connection is established, the server verifies the ops account. With password login, the client places the password inside the already-encrypted packet; the server decrypts and checks it. With public-key login, the server usually records your user public key in the target account's ~/.ssh/authorized_keys.
The client uses the corresponding user private key to sign authentication data that includes the session identifier, username, and other information. The server checks whether that public key is authorized for login, then verifies the signature. The private key never leaves the client; only the signature is sent. Because the signature binds the session identifier, a new connection requires a fresh signature.
After Login Succeeds, Where Does the Command Execute?
For an interactive login, the client typically requests a remote pseudo-terminal (PTY) and then asks to start the remote account's shell. When you press Enter, the remote shell interprets and executes the command, and the output travels back through the SSH channel. If you only want to check the hostname once, you can run this command directly. ssh [email protected] 'hostname' A single SSH connection can simultaneously carry multiple logical channels. Interactive terminals, SFTP file transfers, and TCP port forwarding can all use SSH channels, each sending and receiving its own data.
Watch a Real Handshake with ssh -v
Adding -v to the login command shows each step of the connection. In this test run, kex: algorithm shows the key exchange used mlkem768x25519-sha256, which combines ML-KEM and X25519. The next line, ssh-ed25519, is the host signature algorithm. After receiving NEWKEYS, the client uses the new keys to receive data. Only when the log shows Authenticated does the client send the hostname command to execute.
OpenSSH local loopback test log excerpt. ssh-demo is the host alias used in the experiment.
Troubleshooting login failures can follow the same trail. If you see Connection refused, first check whether the target port and sshd are listening. If you see Permission denied (publickey), check the remote username, whether the public key is recorded for that account, and the permissions of the authorized_keys file.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.