skip to content

SSH

SSH-2's transport, user-auth and connection layers give an encrypted shell, forwarded ports and the channel SFTP rides in. Nearly every infrastructure interview asks it: daily tool, prime target.

on this pageshow

explore

questions

page 1 of 2

What does OpenSSH's ssh-agent hold for you, and how do ssh and ssh-add find it through SSH_AUTH_SOCK?

level: juniorimportance: must knowfreq 55%

answer

  1. passphrase typed once
  2. signatures out, keys never
  3. a Unix-domain socket path
  4. eval output exports two variables
  5. socket directory moved in 10.1

basics

~20 s

ssh-agent keeps private keys decrypted in memory, so each passphrase is typed once, and signs authentication data on request without ever handing a key out. ssh and ssh-add reach it through the Unix-domain socket named in SSH_AUTH_SOCK.

solid answer

~40 s

`ssh-agent` is a small long-running process that holds unwrapped private keys. `ssh-add` asks for a key's passphrase once and loads it; from then on `ssh` sends the agent the data that public key authentication must sign and gets back only a signature, so the private key never leaves the agent. Programs find the agent through the Unix-domain socket whose path is in `SSH_AUTH_SOCK`: `eval "$(ssh-agent -s)"` starts it and exports that variable together with `SSH_AGENT_PID`. Since OpenSSH 10.1 the socket is created under `~/.ssh/agent/` rather than in `/tmp`. The socket is accessible only to its owner, but root on the same machine can still connect to it, so the agent contains the key material, not every use of it.

code

bash · 6 lines
bash
eval "$(ssh-agent -s)"          # start; exports SSH_AUTH_SOCK and SSH_AGENT_PID
echo "$SSH_AUTH_SOCK"            # a path under ~/.ssh/agent/ on OpenSSH 10.1+
ssh-add ~/.ssh/id_ed25519        # passphrase asked once
ssh-add -l                       # fingerprints of loaded keys
ssh [email protected]  # signs through the agent, no prompt
ssh-add -D                       # remove every key

go deeper

for a junior

Recall the split: ssh-add loads a key once, ssh-agent keeps it in memory and signs, and SSH_AUTH_SOCK tells every tool where the agent's socket is.

for a middle

Explain that only signatures cross the socket, how eval of ssh-agent -s sets the environment, and what ssh does when the variable points at a dead socket.

for a senior

Show you know what the agent does not protect: anyone who reaches the socket, root included, can use the keys, and lifetimes are off unless you set them.

for a principal

Frame the agent as a trade between passphrase fatigue and standing signing power, and say which keys belong in a long-lived agent at all.

## What the agent is for An SSH **private key** on disk is normally encrypted with a **passphrase**. Without help, every `ssh` login would ask for that passphrase again. **`ssh-agent`** solves this by holding the decrypted ("unwrapped") key in its own memory for the length of a desktop or login session. It is deliberately a simple program: it can add keys, delete keys, list the public halves of the keys it holds and, the operation that matters, **produce a signature** with one of them. That last point is the whole design. During public key authentication the SSH client must prove it holds the private key by signing data supplied by the protocol. With an agent, `ssh` passes that data to the agent and receives the signature back. The private key itself is never sent to `ssh`, never written back to disk and never crosses the network. ## The life of a key in the agent 1. **Start the agent.** `eval "$(ssh-agent -s)"` launches it and evaluates the shell commands it prints, which set `SSH_AUTH_SOCK` (the socket path) and `SSH_AGENT_PID` (its process ID). Many desktop sessions start one for you. 2. **Load keys.** `ssh-add` with no arguments loads the default identity files from `~/.ssh/`; `ssh-add <file>` loads a specific one. The passphrase is asked for once. Alternatively the client directive `AddKeysToAgent` (default `no`) makes `ssh` add a key the first time it is used. 3. **Use keys.** Every later `ssh`, `scp` or `sftp` login asks the agent to sign; no prompt appears. 4. **Inspect and remove.** `ssh-add -l` lists fingerprints, `ssh-add -L` the public keys, `ssh-add -d <file>` removes one key and `ssh-add -D` removes all of them. 5. **Stop.** `ssh-agent -k` kills the agent named by `SSH_AGENT_PID`; the keys vanish with the process. ## How clients find the agent The agent listens on a **Unix-domain socket**, a file-system object that local processes connect to. Nothing about it is network-facing. Three names decide how clients find it: | Mechanism | Where it is set | Effect | |---|---|---| | `IdentityAgent` | the client configuration file | overrides the environment; `none` disables agent use | | `SSH_AUTH_SOCK` | environment, exported by `ssh-agent` | the path every OpenSSH tool tries by default | | `SSH_AGENT_PID` | environment, exported by `ssh-agent` | identifies the process for `ssh-agent -k`; not used to connect | If `SSH_AUTH_SOCK` is empty or points at a dead socket, `ssh-add` exits with status 2 (unable to contact the agent) and `ssh` falls back to reading key files, prompting for passphrases again. ## Where the socket lives For most of OpenSSH's history the agent created its socket in a randomly named directory under `/tmp`. **OpenSSH 10.1** moved agent listener sockets, for both `ssh-agent` and the forwarded sockets `sshd` creates, to **`~/.ssh/agent/`**, with names matching `s.*`. The release notes give the reason: a process whose sandbox still allows `/tmp` should not, by that fact alone, be able to reach an agent and use its keys. The move has side effects worth knowing: - the operating system no longer sweeps stale sockets out of `/tmp`, so `ssh-agent` now cleans up old sockets itself (`-U` suppresses that, `-u` only cleans and exits); - the path includes a truncated hash of the host name, so a home directory shared over NFS does not mix sockets from different machines; - `ssh-agent -T` restores the old behaviour, a random directory under `$TMPDIR`; - scripts that hunted for agents by globbing `/tmp` stop finding them. ## What the agent protects, and what it does not - **Protected:** the key material. A program that can talk to the agent gets signatures, not keys. - **Not protected:** use of the keys. Anyone who can connect to the socket can ask for signatures. The manual is blunt: the socket is accessible only to the current user, "but is easily abused by root or another instance of the same user". - **Not protected:** keys after the agent's lifetime controls, which are off by default; a key added without a lifetime stays until it is removed or the agent exits. That gap between holding a key and using it is why lending the socket to another machine, agent forwarding, is the subject interviewers press on next.

  • Why did OpenSSH 10.1 move agent sockets out of /tmp?
    Processes with restricted file-system access usually keep `/tmp`, so a socket there was reachable by them and its keys usable by default. Putting `ssh-agent`'s socket, and the forwarded sockets `sshd` creates, under `~/.ssh/agent/` removes that ambient access. Because the OS no longer cleans that directory, `ssh-agent` now removes stale sockets itself; `-T` puts the socket back under `$TMPDIR`.
  • Can root on the same machine use your agent?
    Yes. The socket's permissions stop other ordinary users, but root bypasses file permissions and can connect to it, list your keys and request signatures. The `ssh-agent` manual says the socket is "easily abused by root or another instance of the same user". The agent keeps the key material inside its process; it cannot stop a privileged local user from asking it to sign.
  • How do you point ssh at a different agent than the one in SSH_AUTH_SOCK?
    Set `IdentityAgent` in the client configuration: it overrides `SSH_AUTH_SOCK`, accepts a socket path or a `$VARIABLE`, and `none` disables agent use for that host. That lets one host use a second agent that holds only the key it needs.

  • ssh-agent hands the decrypted private key to ssh at login time.
  • ssh-agent writes the unlocked keys to a file on disk.
  • SSH_AUTH_SOCK contains the passphrase or the key itself.
  • Agent sockets always live under /tmp on current OpenSSH.
  • The socket's file permissions stop root from using your agent.

Assumes OpenSSH 10.5; since 10.1 ssh-agent and sshd create agent sockets under ~/.ssh/agent instead of /tmp.

open as a page

In SSH, what does the server receive and keep for password authentication compared with public-key authentication, and why is the key login safer?

level: juniorimportance: must knowfreq 68%

basics

~20 s

With SSH password authentication the client sends the password itself, hidden only by the transport encryption, and the server checks it. With public-key authentication the server holds just a public key and verifies a signature, so nothing reusable reaches it.

open as a page

In OpenSSH's sshd_config, what does the default PermitRootLogin prohibit-password still allow, and why do hardening guides set it to no?

level: juniorimportance: must knowfreq 68%

basics

~20 s

OpenSSH's default PermitRootLogin prohibit-password still lets root log in with a public key; it only refuses password and keyboard-interactive. Hardening sets no so people log in as themselves and escalate with sudo, leaving a per-person trail.

open as a page

What is an SSH client's first-connection host-key fingerprint prompt asking you to confirm, and what does known_hosts protect against afterwards?

level: juniorimportance: must knowfreq 62%

basics

~20 s

The prompt asks whether the presented host key really belongs to that server. Once accepted, the key is pinned in known_hosts, and a later server presenting a different key, possibly a man-in-the-middle, is refused by default.

open as a page

With OpenSSH, what do local (-L) and remote (-R) port forwarding each do, and which side opens the listening port?

level: juniorimportance: must knowfreq 64%

basics

~20 s

Local forwarding (-L) opens a listener on the client and carries each connection to a destination the SSH server connects to; remote forwarding (-R) makes the server listen and carries each connection back to a destination the client connects to.

open as a page

Root on a shared SSH bastion logged in to production as an SRE who had used ssh -A; what had agent forwarding lent it?

level: middleimportance: must knowfreq 42%

basics

~20 s

Agent forwarding lent root on the bastion a socket into the SRE's laptop agent: while her session was open, root could have the agent sign logins to any host her keys open. It could not copy the private keys.

open as a page

In SSH's publickey authentication method, what does the client sign, and why can't that signature be replayed to log in on another connection?

level: middleimportance: must knowfreq 38%

basics

~20 s

The client signs the session identifier, the exchange hash of that connection's first key exchange, followed by the user name, service, method, algorithm and public key. Each connection has its own session identifier, so the signature verifies only where it was made.

open as a page

In OpenSSH, how do ControlMaster, ControlPath and ControlPersist make hundreds of repeat ssh sessions to the same host fast?

level: middleimportance: must knowfreq 34%

basics

~20 s

The first ssh becomes a master that keeps the authenticated connection and listens on a local socket at ControlPath; later sessions to the same destination open channels through it, skipping TCP setup, key exchange and authentication. ControlPersist keeps the master alive afterwards.

open as a page

Why does OpenSSH's client ignore `User alice` in a Host block placed after a `Host *` block, and how do you prove which value won?

level: middleimportance: must knowfreq 46%

basics

~20 s

ssh keeps the first value it obtains for each directive, reading every matching block top to bottom, so a User set earlier under Host * cannot be overridden later. Put specific blocks first, Host * last, and check with ssh -G.

open as a page

An OpenSSH server has PasswordAuthentication no, yet clients still get a password prompt; which sshd_config settings explain it, and what do you set?

level: middleimportance: must knowfreq 52%

basics

~20 s

PasswordAuthentication covers only the password method; keyboard-interactive is separate, defaults to yes, and with UsePAM yes PAM asks for the password through it. Set KbdInteractiveAuthentication no too, then check Include files and Match blocks for an overriding yes.

open as a page

In SSH's transport protocol, how do client and server pick each algorithm from their SSH_MSG_KEXINIT lists, and whose preference order wins?

level: middleimportance: must knowfreq 40%

basics

~20 s

Both sides send SSH_MSG_KEXINIT with ordered name-lists per category. For each category SSH takes the first algorithm on the client's list that the server also supports, so the client's order wins and the server's list only limits the choice.

open as a page

How does OpenSSH's ProxyJump (ssh -J) carry a connection through a bastion, and how does it differ from a ProxyCommand?

level: middleimportance: must knowfreq 41%

basics

~20 s

ssh -J logs in to the bastion, asks it to open a TCP forwarding to the target's SSH port, and runs a second, end-to-end SSH session through it. ProxyCommand instead runs any command whose stdin and stdout reach the server.

open as a page

In OpenSSH's ~/.ssh/config, how does a Host block with HostName, User, Port and IdentityFile turn a long ssh command into `ssh web1`?

level: juniorimportance: should knowfreq 55%

basics

~20 s

A Host block names an alias pattern; when the name typed after ssh matches it, the client applies that block's HostName (the real address), User, Port and IdentityFile, so ssh web1 expands into the full connection.

open as a page

Why is OpenSSH's ProxyJump (ssh -J) safer than agent forwarding for reaching hosts behind an SSH bastion?

level: middleimportance: should knowfreq 36%

basics

~20 s

With ProxyJump the bastion only relays an encrypted connection; the laptop runs the inner SSH session and authenticates to the target itself, so neither keys nor an agent socket reach the bastion. Agent forwarding lends that socket to the bastion.

open as a page

What do OpenSSH ssh-add's -t, -c and -x options each limit about agent-held keys, and what does each leave exposed?

level: middleimportance: should knowfreq 22%

basics

~20 s

ssh-add -t deletes a key after a lifetime, -c makes each use wait for an ssh-askpass confirmation, and -x locks the whole agent until -X. Each narrows when keys can be used, not who may ask or where.

open as a page

In OpenSSH's authorized_keys file, how do the command=, no-pty and expiry-time= options limit what one key can do on a shell host?

level: middleimportance: should knowfreq 26%

basics

~20 s

Options written before a key in authorized_keys apply only to logins made with that key: command= runs a fixed command instead of whatever the client asked for, no-pty refuses a terminal, and expiry-time= makes sshd refuse the key after a date.

open as a page

An OpenSSH client whose agent holds six keys is disconnected with 'Too many authentication failures'; why, and which ssh_config lines fix it?

level: middleimportance: should knowfreq 38%

basics

~20 s

Without IdentitiesOnly the client offers every key the agent holds as well as its configured files, and each key the server rejects counts toward sshd's MaxAuthTries (6 by default). Set IdentityFile and IdentitiesOnly yes in that host's block.

open as a page

Why do idle OpenSSH sessions through a NAT gateway or firewall freeze and drop, and how does ServerAliveInterval differ from TCPKeepAlive?

level: middleimportance: should knowfreq 27%

basics

~20 s

Stateful middleboxes drop idle TCP flows, so the next keystroke goes nowhere. ServerAliveInterval sends an encrypted request inside the SSH connection every N idle seconds and disconnects after ServerAliveCountMax (3) unanswered; TCPKeepAlive uses spoofable kernel probes.

open as a page

On an OpenSSH server, how do AllowUsers and AllowGroups limit who may log in, and how do DenyUsers and DenyGroups interact with them?

level: middleimportance: should knowfreq 38%

basics

~20 s

AllowUsers and AllowGroups turn sshd_config into an allow list: once set, only matching user names, or members of matching groups, may log in, whatever method they use. A user matched by DenyUsers or DenyGroups is refused without consulting the allow lists.

open as a page

Why can't an OpenSSH 10.5 client reach an old switch offering only diffie-hellman-group14-sha1 and ssh-rsa, and what is the narrowest fix?

level: middleimportance: should knowfreq 28%

basics

~20 s

Neither algorithm is in the 10.5 client's defaults: diffie-hellman-group14-sha1 left the default key exchange list in OpenSSH 8.2, and SHA-1 ssh-rsa signatures were disabled by default in 8.8. Append both with '+' for that one host.

open as a page

What does OpenSSH's dynamic forwarding (ssh -D) provide, and how does it differ from a fixed -L forward?

level: middleimportance: should knowfreq 33%

basics

~10 s

ssh -D turns the client into a SOCKS4/SOCKS5 proxy: each application connection names its own destination, which the SSH server then connects to. A -L forward fixes one destination per listening port.

open as a page

An OpenSSH remote forward (ssh -R 8080:localhost:3000) answers on the server itself but not from other hosts; why, and what changes that?

level: middleimportance: should knowfreq 24%

basics

~20 s

sshd's GatewayPorts defaults to no, which holds every remote forward to the server's loopback address. GatewayPorts clientspecified honours the bind address the client asks for; GatewayPorts yes binds every remote forward to the wildcard address.

open as a page

With OpenSSH's sshd, how does AuthenticationMethods publickey,keyboard-interactive require both a key and a one-time code, and why keyboard-interactive rather than password?

level: seniorimportance: should knowfreq 14%

basics

~20 s

AuthenticationMethods publickey,keyboard-interactive makes sshd accept a valid key first, answer it with partial success, then require a keyboard-interactive round. Keyboard-interactive (RFC 4256) carries prompts the server chooses, so a PAM module can ask for a one-time code.

open as a page

With OpenSSH user certificates, what does sshd check before accepting a CA-signed key, and how do principals map a certificate to an account?

level: seniorimportance: should knowfreq 20%

basics

~20 s

sshd accepts an OpenSSH user certificate when a trusted CA signed it, the current time is inside its validity window, a principal fits the account, its critical options are understood and allowed, and the client signs the session with the certified key.

open as a page

An internet-facing OpenSSH 10.5 server logs tens of thousands of failed logins a day; what do PerSourcePenalties, MaxStartups and MaxAuthTries each limit, and when is a log-scraping ban tool still worth adding?

level: seniorimportance: should knowfreq 26%

basics

~20 s

Since 9.8, PerSourcePenalties makes sshd refuse an address that keeps failing, for an accrued period up to 10 minutes by default; MaxStartups randomly drops excess unauthenticated connections; MaxAuthTries caps attempts per connection. A ban tool adds longer, firewall-level, multi-service bans.

open as a page

An auditor asks that an OpenSSH 10.5 server drop every non-post-quantum key exchange and every SHA-1 algorithm; which sshd_config lines achieve that, and what can break?

level: seniorimportance: should knowfreq 16%

basics

~20 s

Set KexAlgorithms -curve25519-sha256*,ecdh-sha2-* and MACs -hmac-sha1*, leaving mlkem768x25519-sha256, the sntrup761 hybrids and the SHA-2 and UMAC MACs. The default ciphers and host-key algorithms already exclude SHA-1. Clients offering no post-quantum hybrid then fail key exchange.

open as a page

A security review asks whether SSH sessions recorded today could be decrypted by a future quantum computer; what does OpenSSH 10.x's mlkem768x25519-sha256 default change?

level: seniorimportance: should knowfreq 16%

basics

~20 s

Since OpenSSH 10.0 the default key exchange, mlkem768x25519-sha256, hashes an ML-KEM-768 secret with an X25519 one, so breaking X25519 later no longer decrypts a recording. Sessions still negotiating classical ECDH or DH remain 'store now, decrypt later' targets.

open as a page

With OpenSSH's sshd, how do you stop one group of users tunnelling through a server, and why does ForceCommand alone not do it?

level: seniorimportance: should knowfreq 17%

basics

~20 s

Put DisableForwarding yes in a Match Group block: it turns off TCP, Unix-socket, agent and X11 forwarding and, since OpenSSH 10.4, overrides PermitTunnel. ForceCommand only replaces the command a session runs; forwarding needs no command.

open as a page

How would you replace SSH trust-on-first-use for a fleet of servers rebuilt daily, weighing @cert-authority host certificates against SSHFP records in DNS?

level: principalimportance: should knowfreq 14%

basics

~20 s

Where you control the clients, host certificates usually win: clients pin one CA key with @cert-authority, each rebuilt host gets a freshly signed certificate, and prompts disappear. SSHFP helps only with validated DNSSEC; certificates cost guarding and rotating the CA.

open as a page

How do OpenSSH ssh-add -h destination constraints limit where a forwarded agent key can be used, and what can they not prevent?

level: seniorimportance: nice to knowfreq 12%

basics

~20 s

Since OpenSSH 8.9, ssh-add -h names, by host key, where a key may authenticate and through which hops; the agent checks each request's bound session path and refuses others. Root on a permitted hop can still use it.

open as a page

showing 1–30 of 34