Disable SSH agent and X11 forwarding
/etc/ssh/sshd_configAllowAgentForwarding noX11Forwarding noThen reload the daemon:
sudo systemctl reload ssh # Debian, Ubuntusudo systemctl reload sshd # RHEL, Rocky, Alma, FedoraWhy it matters
Section titled “Why it matters”Agent forwarding is the one that bites. When you connect with ssh -A, your
local SSH agent is exposed to the remote host through a socket. Anyone who can
read that socket — which includes root on that host, and any process running as
your user — can ask your agent to sign authentication challenges. They cannot
steal the key itself, but they don’t need to: for as long as your session is
open, they can authenticate as you to any host your key opens.
So a single compromised server turns into every server you can reach from it. That is a lateral-movement path created entirely by a convenience feature.
X11 forwarding matters less on a server, mostly because servers rarely run X clients — but an untrusted remote X client can interact with your local display, including reading keystrokes. If nothing on the host needs a GUI, there’s no reason to leave it on.
Use ProxyJump instead
Section titled “Use ProxyJump instead”Agent forwarding usually exists to solve “I need to reach host B, and only host
A can see it.” ProxyJump solves that properly: the authentication happens on
your machine, and the jump host only ever relays an encrypted stream. Your
agent is never exposed to it.
ssh -J bastion.example.com internal-hostOr make it permanent, client-side:
Host internal-host ProxyJump bastion.example.comThis is strictly better than -A, and it is why agent forwarding can usually be
turned off with nothing lost.
What about AllowTcpForwarding?
Section titled “What about AllowTcpForwarding?”AllowTcpForwarding no is often bundled into hardening lists. Be careful: it
breaks port forwarding and tunnels, which is exactly what ProxyJump and
database tunnels rely on. Turn it off only if you know nothing depends on it —
it is not a free win, and disabling it on a bastion defeats the bastion.
Related
Section titled “Related”- Require key-only SSH authentication — the key this protects.