SSH: Keys, Bastion Hosts and Tunnels
SecuritySSH (Secure Shell) is an encrypted way to log in to another machine and run commands, or to carry other traffic through it. The server proves who it is with a host key, you prove who you are with a key pair instead of a password, and in a real system you reach private servers through one hardened, audited entry point called a bastion host.
A locked office building with one front desk. Your public key is a lock you install on every door you may open, and your private key is the only key that fits it and never leaves your pocket. The building has one guarded entrance (the bastion host) that everyone passes, so there is one door to watch instead of hundreds, and the guard walks you to the one room you asked for.
What SSH is. SSH is a network protocol for logging in to a remote machine and running commands on it over a network you do not trust. It replaced telnet and rlogin, which sent everything, passwords included, as readable text. A server listens on TCP port 22 by default. One SSH connection is built from three layers: a transport layer that sets up the encryption and proves the server's identity, a user authentication layer, and a connection layer that carries many channels (a shell, a single command, a forwarded port) over that one connection. File copying with scp and sftp rides on the same connection.
The server proves who it is first. Every server has a host key pair, and during the handshake it signs the exchange with it. The first time you connect, your client shows the host key's fingerprint and asks you to confirm it, then saves it in ~/.ssh/known_hosts. This is called trust on first use. On every later connection the saved key must match. If it does not, SSH stops with a loud REMOTE HOST IDENTIFICATION HAS CHANGED warning, because either the server was rebuilt or someone is pretending to be it. Only once the server is trusted do you prove who you are.
Keys instead of passwords. You create a key pair with ssh-keygen -t ed25519. The private key stays on your machine, ideally protected by a passphrase or held on a hardware token. The public key can be copied anywhere, and you add it to ~/.ssh/authorized_keys on each server you may enter. At login your client signs a message that includes the unique id of this session, and the server checks the signature against your public key. The private key never crosses the network, so there is no password to guess, steal in transit or reuse on another site.
import base64, hashlib
# one line of a public key file: the type, the key itself in base64, a comment
line = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH2hrWRFXI4vsM2G+QUvOufl5OY2xR+VdFdmQVT0ttDx demo@laptop"
kind, b64, comment = line.split()
blob = base64.b64decode(b64) # the raw bytes of the public key
digest = hashlib.sha256(blob).digest() # a fingerprint is just a hash of those bytes
print("SHA256:" + base64.b64encode(digest).decode().rstrip("="))
# SHA256:53xBPEPIOHauG3Ro+GdJ+91p1ANu7FMqvV1S7oYyTPI the same text ssh-keygen -lf printsWhy the fingerprint matters. The fingerprint above is the short, readable name of a key. It is what SSH shows you the first time you meet a server, and what you compare when you check that the key you are trusting is the one the server owner published. A fingerprint is a hash of the key, so two different keys will not share one in practice, and you never have to read out the whole key.
Bastion hosts. Production servers should not each expose port 22 to the internet. Put them on a private network and allow SSH only from one hardened machine in the public part of the network, the bastion host (also called a jump host). Now there is one place to patch, one place that logs who connected and when, and one firewall rule to review. With ProxyJump, your client connects to the bastion first and then through it to the private server. The bastion only relays the encrypted stream, and the session itself is still between you and the private server. The private name is looked up by the bastion, so your laptop needs no route to the private network.
# ~/.ssh/config
Host bastion
HostName bastion.example.com
User deploy
IdentityFile ~/.ssh/id_ed25519
Host app-*
ProxyJump bastion
User deploy
# now `ssh app-3` goes through the bastion on its own.
# the same thing on one line: ssh -J bastion.example.com app-3.internalTunnels (port forwarding). SSH can carry other traffic too. Local forwarding (-L) opens a port on your laptop and delivers whatever you send to it to a host that the SSH server can reach. For example ssh -L 5432:db.internal:5432 bastion lets a database tool on your laptop connect to localhost:5432 and reach a database that has no public address. Remote forwarding (-R) does the opposite, and dynamic forwarding (-D) turns the connection into a SOCKS proxy. A tunnel is a hole through the firewall that you chose to open, so a design should say who is allowed to open one and log it.
Agent forwarding is the trap to avoid. An SSH agent keeps your unlocked keys in memory so you type the passphrase once. Agent forwarding (-A) lets the remote machine use your agent. That is convenient for hopping between servers, but anyone with administrator rights on that machine can use your keys for as long as you stay connected. Your client does not do it unless you ask, but a server allows it unless its owner turns it off with AllowAgentForwarding no. Use ProxyJump instead, which never hands your agent to the bastion.
Past a handful of engineers. A server that keeps every engineer's public key in authorized_keys does not scale. When someone leaves, you must find every file that holds their key. One better design is SSH certificates: a certificate authority signs each person's public key with their name and an expiry such as eight hours, and every server trusts that authority once (the TrustedUserCAKeys setting in sshd_config). Nothing needs removing when a person leaves, because their certificate simply expires. The other direction is to stop exposing SSH at all. A service such as AWS Systems Manager Session Manager, or a company's own access proxy, gives a shell through an authenticated and logged channel with no inbound port 22. Both follow the zero trust idea: short-lived access, tied to identity, with a record of who did what.
The settings that remove most of the risk. Turn off password login (PasswordAuthentication no) so there is nothing to brute force; it is on by default. Forbid direct root login (PermitRootLogin no; the default still lets root in with a key), allow only named users or groups, keep the SSH software patched, and block addresses that fail repeatedly. Record every login with who, from where and when. In a design interview, say that engineers reach servers through one audited entry point, and that no machine is reachable from the internet just because it exists.
- SSH encrypts a login and can tunnel other traffic. The server proves itself with a host key, and you prove yourself with a key pair.
- The private key never leaves your machine. Servers hold only the public key, and a fingerprint is a short hash of it.
- A bastion host is the one audited door into a private network. Reach servers through it with ProxyJump.
- Prefer ProxyJump to agent forwarding, and short-lived certificates (or no open port at all) to long lists of authorized keys.
- Turn off password login and root login, and log every connection.
Curated Curation & Deep Insights
ScaleDojo CertifiedOur system architects are vetting high-quality, authorized video guides for SSH: Keys, Bastion Hosts and Tunnels with zero third-party platform links.
We are preparing premium, zero-competitor deep-dives for SSH: Keys, Bastion Hosts and Tunnels. Authorized reading references will appear automatically.