- Tue 01 December 2026
- 21 min read
- Unix
- #ssh, #openssh, #unix, #sysadmin, #bastion, #controlmaster, #port-forwarding, #certificates, #openbsd, #freebsd, #linux, #security

Table of Contents
In March I wrote about shell tricks that save keystrokes. This is the same idea, one layer down. Most of us use SSH every day and use about a tenth of it: ssh host, a key, maybe a Host alias. The rest sits in two man pages that nobody reads past the first screen.
I am skipping the basics here. No ssh-copy-id, no “use Ed25519 keys”, no Host blocks. You have read that article a dozen times. This one is about the features that change how you work once you have more than a handful of machines, and about what breaks when you turn them on, because every one of them has a catch.
The running example is my bastion host. It is a small OpenBSD VM that is the only way into my infrastructure from outside, and while writing this I found a mistake in my own setup on it, and then the same mistake in my client configuration. Both are in here too.
Everything was checked against OpenSSH 10.6 on Fedora 45 (client) and OpenBSD 8.0 (server). Most of it works on anything from the last decade, but a few things have minimum versions: ssh -O conninfo, ssh -O channels and the ~I escape need OpenSSH 10.3, the post-quantum key exchange mlkem768x25519-sha256 needs 9.9, and per-source penalties in sshd arrived with 9.8. Check ssh -V first. The public addresses in the examples are from the documentation ranges, and in the screenshots the real one is redacted.
The Setup: One Bastion, Two Kinds of Users
The bastion does exactly two things. It lets me open a SOCKS proxy to reach internal web interfaces:
ssh -D 1080 -N jumpuser@bastion
And it lets me jump through to internal hosts:
ssh -J jumpuser@bastion admin@10.0.10.208
The jumpuser account cannot do anything else. This is the relevant part of the bastion’s sshd_config:
AllowAgentForwarding no
AllowTcpForwarding yes
PermitTunnel no
X11Forwarding no
Match Group jumpers
AllowTcpForwarding local
PermitOpen any
PermitTTY no
ChrootDirectory /var/empty
ForceCommand internal-sftp
No terminal, no shell, an empty chroot, and a forced command that has nothing to serve. What remains is local forwarding, which is what both -D and -J use on the server side.
The second kind of user is me. My own admin account is outside that Match block and gets a normal shell. It is the one behind the screenshots and the session tests further down.
In front of that sits pf with a default-deny ruleset and a rate limit on new connections:
pass in on $ext_if proto tcp from any to { $bastion_v4, $bastion_v6 } port 2222 \
keep state \
(max-src-conn 15, max-src-conn-rate 5/10, \
overload <bruteforce> flush global)
More than five new connections in ten seconds from one address, and that address goes into a block table. flush global then kills every existing state from it as well.
Keep that rule in mind. It is the reason the next section is not optional for me.
Multiplexing: One Connection, Many Sessions
By default, every ssh, scp, sftp and git fetch opens its own TCP connection and runs its own key exchange and authentication. With connection multiplexing, the first connection becomes a master, and everything after it rides along as an additional channel inside the same connection.
# ~/.ssh/config
Host *
ControlMaster auto
ControlPath ~/.ssh/controlmasters/%C
ControlPersist 4h
mkdir -m 700 ~/.ssh/controlmasters
ControlMaster auto means: use a master if there is one, become one if there is not. ControlPersist 4h keeps the master alive in the background for four hours after the last session closes. %C is a hash of local host, remote host, port, user and jump host. Use it instead of spelling out %r@%h:%p: Unix socket paths are limited to roughly a hundred bytes, and a long hostname will run into that limit.
The effect is immediate. The second ssh to the same host has no key exchange and no authentication. It is a prompt before your finger leaves the Enter key. If your key lives on a hardware token that wants a touch, you touch it once per master, not once per scp.
For the bastion it matters for a second reason. ssh -J starts a separate ssh process for the jump host, and that process reads the same configuration. With multiplexing, every jump through the bastion reuses the one existing connection to it. Without it, every jump is a new TCP connection to port 2222. Run anything that touches six internal hosts in a row, and the pf rule above counts six new connections in well under ten seconds. The rule does not know they were all mine.
Talking to the Master
The master is controlled with ssh -O:
| Command | What it does |
|---|---|
ssh -O check host |
Is a master running? |
ssh -O conninfo host |
Negotiated algorithms, addresses, traffic counters |
ssh -O channels host |
Open channels inside the connection |
ssh -O forward -L ... host |
Add a forwarding (see below) |
ssh -O cancel -L ... host |
Remove a forwarding |
ssh -O stop host |
Accept no new sessions, keep existing ones |
ssh -O exit host |
Close the master and everything in it |
conninfo and channels are recent, they were added in OpenSSH 10.3. The rest has been there for years. conninfo is the one I did not know I needed:
$ ssh -O conninfo bastion
Connection information for laptop pid 33038
versions OpenSSH_10.6p1 -> OpenSSH_10.6
tcp 192.0.2.44:37576 -> 198.51.100.81:2222
duration 00:01:45
kexalgorithm curve25519-sha256
hostkeyalgorithm ssh-ed25519
cipher chacha20-poly1305@openssh.com
mac <implicit>
compression none
That kexalgorithm line is how I found the mistake. More on that further down.
What Multiplexing Changes Without Telling You
Multiplexing is the single biggest quality-of-life improvement in SSH. It also changes four things that are easy to miss.
Authentication happens once. A session that joins an existing master is not authenticated again. That is the point, but think it through. If your access was revoked ten minutes ago, your running master still works, and so does every new session you open through it, for as long as ControlPersist keeps it alive. The same goes the other way round: if you were just added to a group on the server, sessions through an old master may still show the old membership, because the server-side process was set up before the change. Close the master with ssh -O exit when you need a fresh login.
A dead master does not look dead. Close the laptop lid, change networks, open it again. The TCP connection behind the master is gone, but the master process is still there and still listening on its socket. New sessions attach to it and hang. The man page says ssh falls back to a normal connection “if the control socket does not exist, or is not listening”. A master with a dead TCP connection does exist and does listen, so there is no fallback.
The fix is two lines in the same Host * block:
ServerAliveInterval 60
ServerAliveCountMax 3
Now the master notices after three unanswered probes and exits on its own. Until then, ssh -O exit host gets you out. If even that hangs, delete the socket file. I have a line in an earlier article where I do exactly that, rm ~/.ssh/controlmasters/*, and at the time I did not explain why. This is why.
Everything shares one TCP connection. Start a large scp through the master, and your interactive session in the same connection competes with it. There is one congestion window and one stream, and your keystrokes are queued behind file data. For bulk transfers, bypass the master:
scp -o ControlPath=none bigfile.tar host:/tmp/
The server limits sessions per connection. MaxSessions defaults to 10. It counts shells, commands and subsystems such as SFTP. I tried it against the bastion with one shell already open and twelve more commands started in parallel. Nine ran. The other three got this:
mux_client_request_session: session request failed: Session open refused by peer
After that message, ssh did not give up. Each of the three went on to attempt a normal connection of its own, with a full key exchange and authentication. That is friendly on most hosts and exactly what you do not want in front of a connection rate limit.
Forwardings are different. According to sshd_config(5) they are not sessions and are not counted, and MaxSessions 0 even exists as a way to allow forwarding and nothing else. The test agrees: fourteen parallel forwarded connections through the same master all went through. This is why jumping through a bastion is not affected by the limit.
Adding a Forwarding to a Running Session
You are logged in, three levels deep in something, and realise you need a port forwarded. The usual answer is to log out and reconnect with -L. There are two better ones.
The Escape Command Line, and Why It Stopped Working
SSH has an escape character, the tilde, which is only recognised directly after a newline. Press Enter, then:
| Keys | Effect |
|---|---|
~. |
Disconnect. Works when the remote end is frozen |
~# |
List forwarded connections |
~I |
Show information about the connection (OpenSSH 10.3 and later) |
~C |
Open a command line to add or remove forwardings |
~? |
List all escapes |
~C gives you an ssh> prompt where you can type -L 8443:10.0.10.20:8443 and carry on. Every older tutorial recommends it.
On a current OpenSSH it does nothing useful. Since OpenSSH 9.2, released in February 2023, the command line is disabled by default. You have to opt in:
Host *
EnableEscapeCommandline yes
And even that only helps in a session that is not multiplexed. A session that runs through a master never gets the command line, whatever the option says. With ControlPersist, that is every session, including the first one, because the master moves to the background and your terminal becomes its first client. Type ~C there and the answer is escape not available to multiplexed sessions.
My own client had the option switched off and runs everything through a master, so ~C was gone twice over. I had not noticed, because I had stopped using it for the method below. Look at the screenshot at the top of this article: that is ~? on my session to the bastion, and ~C is not even on the list.
Through the Master Instead
If you use multiplexing, you cannot use the escape, and you do not need it. Ask the master, from any other terminal:
ssh -O forward -L 8443:10.0.10.20:8443 bastion
The command returns immediately and the port is open. Remove it again with the same specification:
ssh -O cancel -L 8443:10.0.10.20:8443 bastion
This works for -L, -R and -D. It needs no terminal on the session, so it also works against a master that was started with -N, which is the only kind my jumpuser is allowed to have. And if the port cannot be bound, you are told so and get a non-zero exit code:
$ ssh -O forward -L 22:10.0.10.20:22 bastion
mux_client_forward: forwarding request failed: Port forwarding failed
muxclient: master forward request failed
Forwardings Belong to the Master
Here is the catch, and it surprised me when I tested it. Request a forwarding on an ordinary command line while a master is running:
ssh -L 8443:10.0.10.20:8443 bastion true
The command runs true and exits. The forwarding is still there:
$ ss -ltn | grep 8443
LISTEN 0 128 127.0.0.1:8443 0.0.0.0:*
The session only asked for the forwarding. The master holds it, and keeps holding it until you cancel it or the master exits. With ControlPersist 4h, that is a port open on your laptop for four hours after you thought you had closed it. Ask the same master for the identical forwarding again, and it quietly answers that it already has it. Ask for port 8443 with a different destination, or from an ssh that does not go through this master, and it fails because the port is taken.
Note also that ssh -O channels does not show idle listeners. It lists channels, and a forwarding only becomes a channel when something connects to it. Check with ss -ltn or netstat -an.
More Ways to Get Bitten
Pitfall: The Algorithm List You Froze
My bastion’s sshd_config contains this line, and has for a long time:
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
It came from a hardening guide. It removes the old Diffie-Hellman groups and the NIST curves, and for years that was a sensible thing to do. My client configuration has the same line for the same reason.
Then OpenSSH gained post-quantum key exchange. Version 9.0 made the hybrid sntrup761x25519-sha512 the default in 2022. Version 9.9 added mlkem768x25519-sha256, and 10.0 made that one the default in 2025. My pinned list has none of it. Both ends are new enough, ssh -Q kex on the bastion lists mlkem768x25519-sha256 and sntrup761x25519-sha512, and both ends were configured by me to never use them. Since 10.6 the server can warn about this as well as the client, and mine did, in a log I was not reading:
sshd-session[96015]: WARNING: connection from 192.0.2.44 port 6002 is not using a post-quantum key exchange algorithm: "curve25519-sha256" [preauth]

The irony is that the same host has a post-quantum host key. I added an ML-DSA host key in October, right after OpenSSH 10.6 enabled ssh-mldsa44-ed25519, and felt good about it. The output above says hostkeyalgorithm ssh-ed25519, so this connection did not even use it. And the host key would only protect against someone impersonating the server in the future. The key exchange is what protects today’s traffic against being recorded now and decrypted later, and that is the half I had locked to 2014.
The fix is one line on each side:
KexAlgorithms mlkem768x25519-sha256,sntrup761x25519-sha512,curve25519-sha256
Both names need OpenSSH 9.9 or later on the side where you write them. An older ssh or sshd refuses to start with an algorithm name it does not know.
The general lesson is less about quantum computers. A pinned algorithm list is a snapshot of what was good on the day you wrote it. OpenSSH’s defaults move forward with every release. Your list does not. If you pin, write the date next to it and put a reminder in your calendar. Or use the - prefix to remove what you distrust instead of listing what you trust, so that new algorithms still arrive:
KexAlgorithms -diffie-hellman-*,ecdh-sha2-nistp*
Pitfall: Agent Forwarding Where a Jump Would Do
ssh -A bastion, then ssh internal from there, is still the pattern many people use. It puts a socket on the bastion that leads back to your agent. As ssh_config(5) puts it, anyone who can bypass file permissions on the remote host can use that socket to authenticate as you for as long as you are connected.
ProxyJump does not need it. With -J, the connection to the internal host is established from your laptop, tunnelled through the bastion. The bastion forwards encrypted bytes and never holds anything it could use. This is why my bastion can say AllowAgentForwarding no and give the jump account no shell at all, and jumping still works.
If you have ForwardAgent yes under Host * anywhere, that is the line to delete today.
Pitfall: The Tunnel That Came Up Without Its Port
ssh -N -L 5432:db.internal:5432 bastion
If local port 5432 is already in use, ssh prints a warning and then stays connected anyway, forwarding nothing. In a terminal you see the warning. In a systemd unit or an autossh wrapper you see a healthy process and a tunnel that does not work.
Host *
ExitOnForwardFailure yes
With this set, ssh exits if it cannot set up a requested forwarding. Note the limit that the man page spells out: it only covers binding the listener. If the destination behind the tunnel is unreachable, the tunnel still counts as up.
Pitfall: First Match Wins
In ssh_config, the first value found for an option is the one that is used. This is the opposite of what most configuration formats do, and it means Host * belongs at the bottom of the file, not the top. Put it first, and nothing below can override it.
When the result is not what you expect, do not read the file. Ask ssh what it resolved:
ssh -G bastion | grep -i -E 'controlpath|kexalgorithms|proxyjump'
ssh -G prints the final configuration for a host after all Host, Match and Include processing, without connecting. It is how I found that my client had its own pinned KexAlgorithms line.
Pitfall: Your Rate Limit Does Not Know It Is You
Back to the pf rule from the beginning. Since OpenSSH 9.8, sshd also has its own per-source penalties, on by default. My bastion’s log had forty of these in one week:
sshd[92723]: srclimit_penalise: ipv4: new 203.0.113.9/32 deferred penalty of 1.000 seconds for penalty: connections without attempting authentication
Both mechanisms are good, and they work differently. sshd penalises misbehaviour: failed logins, connections that never try to authenticate, a login that runs into the grace timeout. A script that logs in successfully does not trip it. The pf rule counts every new connection, and it cannot tell a scanner from your own script looping over hosts through a jump host without multiplexing. With flush global in the pf rule, tripping the limit does not only block the next connection. It cuts the SOCKS tunnel you already had open.
So for a rate-limited bastion, multiplexing is not a convenience. It is what keeps your own tooling under the limit.
Certificates in Ten Lines
Two things about plain SSH keys do not scale. The first time you connect to a host, you are asked to trust a fingerprint you have no way of checking, and you type yes. And every server carries a list of every public key that may log in, which someone has to keep current.
SSH certificates solve both, and they need nothing but ssh-keygen. A certificate is a public key plus a statement signed by a CA key: who this is, which names it may use, and until when.
Create a CA and sign a user key for sixteen hours:
ssh-keygen -t ed25519 -f user_ca
ssh-keygen -s user_ca -I "chris@laptop" -n admin,chris -V +16h ~/.ssh/id_ed25519.pub
That writes ~/.ssh/id_ed25519-cert.pub next to the key, and ssh offers it automatically. Look inside with ssh-keygen -L:
Type: ssh-ed25519-cert-v01@openssh.com user certificate
Key ID: "chris@laptop"
Valid: from 2026-11-30T09:56:00 to 2026-12-01T01:57:14
Principals:
admin
chris
Critical Options: (none)
Extensions:
permit-X11-forwarding
permit-agent-forwarding
permit-port-forwarding
permit-pty
permit-user-rc
On every server, one line replaces all the authorized_keys files:
# /etc/ssh/sshd_config
TrustedUserCAKeys /etc/ssh/user_ca.pub
The other direction works the same way. Sign the server’s host key with -h:
ssh-keygen -s host_ca -I web01 -h -n web01.example.com -V +30d /etc/ssh/ssh_host_ed25519_key.pub
# /etc/ssh/sshd_config
HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub
And tell your client to trust the CA for the whole domain, in ~/.ssh/known_hosts:
@cert-authority *.example.com ssh-ed25519 AAAAC3Nza...
From then on, a new host in that domain never asks you to confirm a fingerprint, and a rebuilt host never triggers the “REMOTE HOST IDENTIFICATION HAS CHANGED” wall of text.
Where This Bites
Principals are not optional. The -n admin,chris part lists the account names the certificate is valid for. A certificate without principals is rejected when it is checked against TrustedUserCAKeys. This is the most common reason a first certificate login fails, and the error on the client is the same unhelpful Permission denied (publickey) as always.
The extensions are permissions. Look at the list above: a default user certificate permits X11 forwarding, agent forwarding, port forwarding, a terminal and the user’s ~/.ssh/rc. If the account should only forward ports, say so when signing, with -O clear -O permit-port-forwarding.
The CA key is now the key to everything. Whoever holds user_ca can mint a login for any account on any server that trusts it. A file in a home directory is the wrong place for that.
Revocation exists, and distribution is your problem. sshd has RevokedKeys, and ssh-keygen can build revocation lists. Getting that file onto every server promptly is the part nobody has solved for you. The practical answer is the -V +16h above: issue certificates that expire before you would have got around to revoking them.
And that is where ssh-keygen runs out. Signing a fresh certificate by hand every morning is not a workflow. You want something that issues short-lived certificates on request, after checking who is asking, and you want the CA key somewhere it cannot be copied. I already run step-ca with its key on a SmartCard-HSM for TLS. Teaching it to sign SSH certificates, with this bastion as the first customer, is the next article.
The Short Version
| Problem | Answer |
|---|---|
| Every connection re-authenticates | ControlMaster auto, ControlPath ~/.ssh/controlmasters/%C, ControlPersist |
| Sessions hang after a network change | ServerAliveInterval 60, or ssh -O exit host |
| Need a forwarding in a running session | ssh -O forward -L ... host |
| Port still open after the session closed | ssh -O cancel -L ... host, forwardings belong to the master |
~C does nothing |
EnableEscapeCommandline yes, and only outside multiplexed sessions |
| Tunnel is up but the port is not | ExitOnForwardFailure yes |
| Config option is ignored | First match wins. Check with ssh -G host |
| What did this connection negotiate? | ssh -O conninfo host |
| Agent forwarding to reach internal hosts | ssh -J bastion internal instead |
| Trust-on-first-use prompts | Host certificates and @cert-authority |
authorized_keys on every server |
User certificates and TrustedUserCAKeys |
Conclusion
None of this is new. Multiplexing has been in OpenSSH since 3.9 in 2004, certificates since 5.4 in 2010. They are simply documented in the kind of man page you search, not the kind you read.
What I took from writing this is less about any single option. My bastion was, by most checklists, a well hardened host: no passwords, no shell for the jump account, a default-deny firewall, a rate limit. And it had a key exchange frozen in the past by the very hardening guide I had followed, while I was busy adding a post-quantum host key next to it. Hardening is not something a host has. It is something you keep checking. ssh -O conninfo and ssh -G take five seconds each.
If you take one thing from this article, turn on multiplexing. If you take two, run ssh -G against your most important host afterwards and read what it actually says.
Further Reading
- ssh(1), especially the sections on escape characters and
-O - ssh_config(5), for
ControlMaster,ControlPath,ControlPersist,EnableEscapeCommandlineandExitOnForwardFailure - sshd_config(5), for
MaxSessions,PerSourcePenalties,TrustedUserCAKeysandHostCertificate - ssh-keygen(1), the CERTIFICATES section
- OpenSSH release notes
- Shell Tricks That Actually Make Life Easier (And Save Your Sanity), the first part of this series
- A CA Key You Cannot Copy: step-ca, a SmartCard-HSM and a FreeBSD Jail
- An IdM Replica at Home, where the stale control sockets first showed up
Comments
You can use your Mastodon or other ActivityPub account to comment on this article by replying to the associated post.
Search for the copied link on your Mastodon instance to reply.
Loading comments...