With OpenSSH's sshd, how do you stop one group of users tunnelling through a server, and why does ForceCommand alone not do it?
answer
- forwarding needs no command at all
- one switch overrides the rest
- narrow with destinations and listeners
- a shell can always relay
- fixes in 10.0, 10.4 and 10.5
basics
~20 sPut 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.
solid answer
~40 sForwarding is requested on the SSH connection itself (`direct-tcpip`, `tcpip-forward`, socket and tun channels), so a client running `ssh -N` never executes a command and `ForceCommand` never fires; its man page entry says it does not limit TCP, agent, socket or X11 forwarding. Scope the policy with `Match Group contractors` and `DisableForwarding yes`, which overrides every other forwarding option. To allow one tunnel instead, combine `AllowTcpForwarding local`, `PermitOpen analytics-db.internal.example:5432`, `PermitListen none` and `AllowStreamLocalForwarding no`. Two caveats: these switches matter only for accounts without a shell, because a shell user can run their own relay; and older servers had gaps, since `DisableForwarding` covered agent and X11 only from OpenSSH 10.0 and `PermitTunnel` only from 10.4.
code
ini · 9 lines# /etc/ssh/sshd_config: Match blocks after the global section
Match Group contractors
DisableForwarding yes
Match Group analysts
AllowTcpForwarding local
PermitOpen analytics-db.internal.example:5432
PermitListen none
AllowStreamLocalForwarding nogo deeper
Recall that sshd can switch forwarding off per user or group in a Match block, and that DisableForwarding yes is the one-line way to do it.
Explain why forced commands do not touch forwarding requests, and which directive covers -L and -D, which covers -R, and which covers sockets and tun devices.
Write the policy and its limits: exact-match PermitOpen, the shell caveat, and the 10.0, 10.4 and 10.5 fixes that decide whether an older server really has everything off.
Decide where tunnelling is a sanctioned access path and where it is a bypass, and pair sshd policy with account design so the switches are not the only control.
## Why forwarding is a policy problem A user who may forward through a server can reach **whatever that server can reach**: `-L` and `-D` open connections from the server into its networks, `-R` publishes services on the server, and `-w` can move whole packets. When contractors only need one database, or nothing but file transfer, every other path is a bypass of the network's own controls. ## Why ForceCommand is not the answer `ForceCommand` replaces the command a session channel runs: shell, command or subsystem. Forwarding does not use a session channel's command at all: - `-L` and `-D` send `direct-tcpip` channel opens; - `-R` sends a `tcpip-forward` global request; - Unix-socket forwarding uses OpenSSH's `streamlocal` messages, and `-w` opens `[email protected]` channels. A client running `ssh -N -L ...` asks for no command, so the forced command never runs while the forward works. `sshd_config(5)` says so explicitly: `ForceCommand` does not limit TCP, agent, socket or X11 forwarding, and they must be disabled individually or all together with `DisableForwarding`. (The warning was added to the manual page in OpenSSH 10.3.) ## The directives | Directive | Default | What it controls | |---|---|---| | `DisableForwarding` | `no` | all forwarding: X11, agent, TCP, StreamLocal; overrides the others | | `AllowTcpForwarding` | `yes` | TCP forwarding: `yes`/`all`, `no`, `local` (`-L`, `-D`) or `remote` (`-R`) | | `PermitOpen` | any | the host:port destinations a TCP forward may connect to | | `PermitListen` | any | the ports or host:ports a remote forward may listen on | | `AllowStreamLocalForwarding` | `yes` | Unix-domain socket forwarding, with the same `local`/`remote` values | | `PermitTunnel` | `no` | tun-device forwarding: `yes`, `point-to-point`, `ethernet` or `no` | All of them are accepted in a **`Match`** block, whose lines override the global section for connections that meet its criteria; when several satisfied blocks set one keyword, the first instance applies. ## Two policies **No tunnels at all** for a group: ```ini Match Group contractors DisableForwarding yes ``` **One sanctioned tunnel** for another group: ```ini Match Group analysts AllowTcpForwarding local PermitOpen analytics-db.internal.example:5432 PermitListen none AllowStreamLocalForwarding no ``` Points to get right in the second form: 1. `PermitOpen` matches the destination **exactly as the client names it**; no pattern matching or address lookups are done. A client that asks for the database's IP address instead of its name is refused, so list every form your users are told to use. 2. `AllowTcpForwarding local` admits `-D` as well as `-L`, since both arrive as `direct-tcpip`; `PermitOpen` is what keeps a SOCKS proxy from reaching anything else. 3. `PermitListen none` stops remote forwards, and `AllowTcpForwarding local` already does as well; keeping both makes the intent survive a later edit. Per-key options in `authorized_keys` (`permitopen=`, `permitlisten=`, `restrict`) can apply similar limits to a single key rather than a group. ## Choosing between the two - If the group needs **no tunnel at all** (file transfer only, or one forced command), `DisableForwarding yes` is the whole answer and survives future additions to the forwarding family. - If the group needs **one or two services**, list them in `PermitOpen` and keep `remote` forwarding, sockets and tun devices off; a missing entry fails closed. - If the group needs **broad access to a network**, per-destination tunnels are the wrong tool: that is a routed access design, and an sshd policy that tries to emulate it becomes a long `PermitOpen` list nobody reviews. ## The shell caveat The man page's own warning: disabling TCP or StreamLocal forwarding **does not improve security unless users are also denied shell access**, because a user with a shell can install their own forwarder and relay traffic through the session's standard input and output. Forwarding switches are real controls for accounts confined to one forced command or to SFTP, and a speed bump for shell accounts. ## Release history that changes the answer - **OpenSSH 10.0** (2025-04-09): `DisableForwarding` was fixed; before it, the directive failed to disable **X11 and agent forwarding** as documented. - **OpenSSH 10.3** (2026-04-02): the `ForceCommand` warning above was added to `sshd_config(5)`. - **OpenSSH 10.4** (2026-07-06): `DisableForwarding yes` now overrides `PermitTunnel yes`; earlier, a server that had enabled tun forwarding kept it despite `DisableForwarding`. - **OpenSSH 10.5** (2026-08-11): the `authorized_keys` `restrict` keyword now applies to tunnel forwarding too. So a policy written as `DisableForwarding yes` means "everything off" only on a current server. On older releases, check `PermitTunnel` and the agent and X11 settings separately.
- An analyst's `-L` forward to the database's IP address is refused under `PermitOpen analytics-db.internal.example:5432`. Why?`PermitOpen` compares the destination exactly as the client sends it in the `direct-tcpip` request; the man page says no pattern matching or address lookups are performed. An IP address is not the name in the list, so the open is refused. Add the address form too, or tell users which name to use.
- Does `AllowTcpForwarding no` also stop agent and X11 forwarding?No. It covers TCP forwarding only; agent, X11, Unix-socket and tun forwarding each have their own switch. `DisableForwarding yes` is the single directive that covers them all, and on OpenSSH 10.5 it does so completely, since agent and X11 were fixed in 10.0 and tun forwarding in 10.4.
- Thinks ForceCommand blocks port forwarding because no shell is given.
- Believes AllowTcpForwarding no stops agent and X11 forwarding too.
- Says forwarding switches secure accounts that still have a full shell.
- Expects PermitOpen to resolve names and match the address the client used.
- Assumes DisableForwarding has always covered tun forwarding.
Assumes OpenSSH 10.5: DisableForwarding covers agent and X11 forwarding since 10.0 and overrides PermitTunnel since 10.4; authorized_keys restrict covers tun forwarding since 10.5.