SSH (Secure Shell) is a network protocol for logging into another computer and running commands on it over an encrypted connection. The client checks the server's identity with a host key, you prove your own identity with a key or password, and everything you type and see afterwards travels encrypted, normally over TCP port 22.
ssh [email protected]
That one command opens a shell on server.example.com as the user alex. The OpenBSD manual describes the client simply as "a program for logging into a remote machine and for executing commands on a remote machine".
What SSH is used for
- Remote administration. Managing Linux servers, routers, NAS boxes and cloud VMs from a terminal.
- Running one command remotely.
ssh alex@server uptimerunsuptimeon the server, prints the result and disconnects. - Copying files.
scpandsftpmove files over the same encrypted connection. - Git. Pushing to GitHub, GitLab or a self-hosted server with an
[email protected]:...remote uses SSH underneath. - Tunnelling. Forwarding a port through the encrypted connection, for example to reach a database that only listens on the server's localhost.
How SSH works: the three layers
RFC 4251, the architecture document, splits SSH into three protocols stacked on top of each other:
| Layer | Defined in | What it does |
|---|---|---|
| Transport layer | RFC 4253 | Agrees on encryption keys, authenticates the server, and provides confidentiality and integrity with forward secrecy. |
| User authentication | RFC 4252 | Authenticates the user to the server: public key, password or host-based. |
| Connection protocol | RFC 4254 | Splits the single encrypted tunnel into channels: shell sessions, remote commands, forwarded TCP ports and X11. |
The order matters. The server proves who it is before you send any credentials, and your password or key signature only ever travels inside the already-encrypted tunnel.
Step 1: version exchange and key exchange
After the TCP connection opens, each side sends an identification line in the format SSH-protoversion-softwareversion. You can see a server's line by reading the first bytes it sends. Run on this machine against GitHub's SSH server (output as of October 2026):
$ bash -c 'exec 3<>/dev/tcp/github.com/22; head -1 <&3'
SSH-2.0-a09ebb4f
2.0 is the protocol version; the rest is the server's software string, which each vendor chooses (GitHub's changes between deployments, so you may see a different value). Next comes the key exchange. RFC 4253 describes Diffie-Hellman as providing "a shared secret that cannot be determined by either party alone": both sides contribute a value, and both end up with the same secret without ever sending it. Session encryption keys are derived from that secret. The hash of this first exchange also becomes the session identifier, which comes back in step 3.
Step 2: the server proves its identity (host keys)
During the key exchange, the server signs the exchange hash with its host key, a long-lived key pair that identifies that machine. The client checks the signature against the public host key it expects. If the client doesn't already know the key, OpenSSH shows the fingerprint and asks. GitHub's documentation shows the exact prompt:
The authenticity of host 'github.com (IP ADDRESS)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
Are you sure you want to continue connecting (yes/no)?
Typing yes stores the key in ~/.ssh/known_hosts, and from then on the client checks it silently. To verify a fingerprint before trusting it, compare it with one the operator publishes. Run on this machine:
$ ssh-keyscan -t ed25519 github.com > gh_hostkey.txt
$ ssh-keygen -lf gh_hostkey.txt
256 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU github.com (ED25519)
That matches the Ed25519 fingerprint on GitHub's fingerprints page. RFC 4253 is blunt about skipping this check: a client may accept an unverified key, but "doing so will render the protocol insecure" against active attacks. That is why a changed host key is serious. The ssh manual says that when a stored key no longer matches, ssh warns you and disables password authentication, because the change can mean someone is intercepting the connection. A reinstalled server also changes its key, so confirm with whoever runs it before you remove the old line with ssh-keygen -R hostname.
RFC 4251 describes two trust models: a local database of host keys (the known_hosts approach above) or host keys certified by a trusted certificate authority. Most personal setups use the first.
Step 3: you prove your identity
Once the tunnel is encrypted and the server is verified, the user authentication protocol runs. The common methods:
- Public key. The only method RFC 4252 makes mandatory for every implementation. The client signs data that starts with the session identifier from step 1, using your private key; the server checks the signature with the public key listed in your
~/.ssh/authorized_keyson the server. In the RFC's words, "the possession of a private key serves as authentication." The private key never leaves your machine, and because the signature covers this session's identifier, it can't be replayed on another connection. - Password. Listed as optional in RFC 4252, though it says implementations should support it. The password is sent inside the encrypted tunnel, so it isn't visible on the network, but it can be guessed, and a server that accepts passwords attracts brute-force attempts.
- Host-based. Optional; trusts the client machine rather than the user. Rare outside managed clusters.
Creating a key pair takes one command; see how to generate an Ed25519 SSH key. If the server or GitHub rejects your key, the error is usually Permission denied (publickey), covered step by step in fixing Permission denied (publickey). For why signing with a private key proves identity at all, see public-key cryptography explained.
Step 4: channels, shells and port forwarding
RFC 4254 says the connection protocol provides "interactive login sessions, remote execution of commands, forwarded TCP/IP connections", plus forwarded X11, all "multiplexed into a single encrypted tunnel". That is why one SSH connection can carry a shell and a port forward at the same time. The ssh client exposes forwarding with three flags:
| Flag | Direction | Example |
|---|---|---|
-L | Local port forwards to a host and port reachable from the server | ssh -L 5433:localhost:5432 alex@db-server makes the server's PostgreSQL reachable at localhost:5433 on your machine |
-R | Port on the server forwards back to your side | ssh -R 8080:localhost:3000 alex@server makes your local dev server reachable on port 8080 on the server |
-D | Local SOCKS proxy | ssh -D 1080 alex@server lets a browser route traffic through the server |
To reach a machine that is only accessible through a bastion host, -J connects via one or more jump hosts: ssh -J alex@bastion alex@internal-host. The manual describes it as a shortcut for the ProxyJump setting.
SSH on Windows, macOS and Linux
macOS and most Linux distributions ship the OpenSSH client, so ssh works in Terminal straight away. On Windows, Microsoft's OpenSSH documentation says OpenSSH has been available as a feature on demand since Windows 10 build 1809, and lists the tools it includes: ssh, sshd, ssh-keygen, ssh-agent, ssh-add, ssh-keyscan, sftp and scp. Check what you have:
ssh -V
Example output on this Windows machine's Git Bash: OpenSSH_9.7p1, OpenSSL 3.2.1 30 Jan 2024. Any reasonably current version works for the commands above.
If you connect to the same servers often, stop retyping user names, ports and key paths: an SSH config file turns ssh -p 2222 -i ~/.ssh/work_ed25519 [email protected] into ssh work.
If a connection fails
Add -v to see each stage: ssh -v alex@server. The debug lines follow the layers in this article, so the last stage reached tells you where it broke. Connection refused or a timeout means the TCP connection never opened (wrong host or port, firewall, or no SSH server running). Host key verification failed means step 2 failed: the stored key doesn't match, or you ran with batch mode and the key was unknown. Permission denied means the tunnel worked and step 3 rejected your credentials.
Frequently asked questions
- What port does SSH use?
- SSH servers normally listen on TCP port 22, as stated in RFC 4253. An administrator can move the server to another port; you then connect with ssh -p 2222 user@host, or set Port in your SSH config file.
- Is SSH the same as SSL or TLS?
- No. Both encrypt traffic, but they are separate protocols. TLS secures HTTPS and many other protocols and usually authenticates the server with a certificate from a certificate authority. SSH has its own transport layer, and by default the client trusts server host keys it has stored in its known_hosts file.
- What is the difference between SSH and SFTP?
- SFTP is a file-transfer protocol that runs inside an SSH connection as a subsystem, so it uses the same encryption and login. The sftp and scp commands ship with OpenSSH alongside the ssh client.
- Do I need to install anything to use SSH on Windows?
- Not a separate program. Microsoft documents OpenSSH as a Windows feature on demand since Windows 10 build 1809. Run ssh -V in a terminal; if the command is not found, add OpenSSH Client under Optional features in Settings.
Sources
- RFC 4251: The Secure Shell (SSH) Protocol Architecture
- RFC 4253: The Secure Shell (SSH) Transport Layer Protocol
- RFC 4252: The Secure Shell (SSH) Authentication Protocol
- RFC 4254: The Secure Shell (SSH) Connection Protocol
- OpenBSD manual: ssh(1)
- Microsoft Learn: OpenSSH for Windows overview
- GitHub Docs: GitHub's SSH key fingerprints
- GitHub Docs: Testing your SSH connection