With OpenSSH's sshd, how do you configure a group of partner accounts that can only use SFTP, each confined to its own directory?
answer
- a conditional block per group
- jail the filesystem view
- in-process server, no shell
- root owns the jail, partner owns a subfolder
basics
~10 sPut the partners in one group and add a Match Group block to sshd_config with ChrootDirectory, ForceCommand internal-sftp and DisableForwarding; the chroot must be root-owned, so each partner writes into a subfolder it owns.
solid answer
~40 sIn `sshd_config`, a `Match Group carriers` block placed at the end of the file overrides the global settings for members of that group. Inside it, `ChrootDirectory %h` makes each partner's home directory their `/` after authentication, and `ForceCommand internal-sftp` replaces whatever the client asked for (shell, command or subsystem) with OpenSSH's in-process SFTP server, so the partner's login shell is never started and the jail needs no binaries or libraries. `DisableForwarding yes` and `PermitTTY no` close what `ForceCommand` does not touch: TCP, agent and X11 forwarding and terminal allocation. sshd requires every component of the chroot path to be root-owned and not writable by group or others, so the partner gets a writable `upload` folder one level down, and `internal-sftp -d /upload` starts the session there.
go deeper
Recall the four pieces: a Match Group block, ChrootDirectory, ForceCommand internal-sftp, and turning off forwarding and terminals. Know that the partner writes into a subfolder, not the jail's top.
Explain what each directive does and why internal-sftp needs nothing inside the jail. Show the root-owned chroot with a partner-owned upload folder, and say why Match blocks belong at the end of the file.
Treat the block as a policy you test: log in as the partner over sftp and ssh, try to leave the jail, try a forward. Point out that ForceCommand leaves forwarding open unless DisableForwarding is set.
Frame the Match block as the place where transfer policy diverges from staff policy on a shared server, and weigh that against giving partners a host whose global configuration is the transfer policy.
## What an SFTP-only account has to stop A partner who only needs to drop files does not need most of what an SSH login offers. By default an OpenSSH account that authenticates can ask for a **shell**, run an arbitrary **command**, start the **sftp subsystem**, open **TCP or Unix-socket forwards** through the server and allocate a **terminal**. An SFTP-only account keeps exactly one of those — the file-transfer server — and confines what that server can see to one directory tree. OpenSSH does this with a handful of `sshd_config` directives applied to a group of accounts rather than to the whole server, so staff logins on the same host keep their normal policy. ## The Match block ```ini # global settings above; Match blocks go at the end of sshd_config Match Group carriers ChrootDirectory %h ForceCommand internal-sftp -d /upload DisableForwarding yes PermitTTY no AuthenticationMethods publickey ``` `Match` introduces a **conditional block**: when every criterion on the line is satisfied (here, the user is a member of the group `carriers`), the keywords that follow override the global ones. Only a documented subset of keywords is allowed after `Match`; all of the lines above are in it. ## What each line does | Directive | Effect for the matched account | |---|---| | `ChrootDirectory %h` | After authentication sshd calls `chroot(2)` so the user's home directory (the `%h` token) becomes their `/`; nothing outside it is visible. | | `ForceCommand internal-sftp` | Ignores the command the client asked for and runs OpenSSH's **in-process** SFTP server instead; this applies to shell, command and subsystem requests alike. | | `-d /upload` | An `internal-sftp` option: the session starts in `/upload` inside the jail instead of at its read-only top. | | `DisableForwarding yes` | Turns off X11, agent, TCP and StreamLocal (Unix-socket) forwarding and overrides the individual forwarding options. | | `PermitTTY no` | Refuses terminal (pty) allocation. | | `AuthenticationMethods publickey` | Scopes key-only login to the group; how each method works is the authentication material's subject. | Because `internal-sftp` runs inside sshd, the chroot needs **no shell, no libraries and no device files** for transfers to work; only logging may need a log socket inside the jail on some systems. The account's login shell is never executed. ## The directory layout sshd checks, at session startup, that **every component of the chroot path is a root-owned directory not writable by group or others**. That rule decides the layout: 1. `/srv/drop` — owned by root, mode 755. 2. `/srv/drop/carrier07` — the partner's home and chroot; owned by root, mode 755. The partner can list it but cannot create files in it. 3. `/srv/drop/carrier07/upload` — owned by `carrier07`; this is where the partner writes. If the chroot itself belongs to the partner, or is group-writable, sshd refuses the session right after authentication, and the client simply sees the connection close. ## Traps when writing it - **A Match block has no closing keyword.** It runs until the next `Match` line or the end of the file, so any global directive written below it silently applies only to the group. Put Match blocks last, or end one with `Match all`. - **First value wins.** For most keywords sshd uses the first value it obtains, and if a keyword appears in several satisfied Match blocks only the first instance applies. - **`Subsystem` is not a confinement.** The `Subsystem sftp` line only maps the `sftp` subsystem name to a server; a client that asks for a shell or a command instead is not affected by it. `ForceCommand` is what removes the shell. - **`ForceCommand` does not limit forwarding.** The manual says so explicitly, which is why `DisableForwarding` sits in the same block. - **Group membership decides who is jailed.** `Match Group` matches the account's groups, so adding a staff account to the partner group by mistake jails it too, and a partner left out of the group gets the global policy, possibly a shell. - **Test as the partner.** An `sftp` login should land in `/upload`; a plain `ssh` login should get no prompt; a `cd ..` at the top should stay at `/`. ## What this block does not cover How public-key authentication itself works, how the client verifies the server's host key, and how a server's shell users are hardened are separate subjects. This configuration is about what a transfer account adds: a jail, a forced in-process server and no side channels.
- With this block in place, what happens when the partner runs a plain `ssh [email protected]` instead of `sftp`?`ForceCommand` applies to shell requests too, so sshd starts `internal-sftp` instead of a shell. That server reads whatever arrives as SFTP packets, and `PermitTTY no` refuses the pty, so the partner gets no prompt and nothing they type is executed as a command.
- An admin added `Match Group carriers` in the middle of `sshd_config`, and later global directives stopped applying to other users. Why?A Match block has no end marker: it lasts until the next `Match` line or the end of the file. Every directive written after it became part of the block and now applies only to members of `carriers`. Move the block to the end or close it with `Match all`.
- Does `ForceCommand internal-sftp` by itself make the account key-only?No. It controls what runs after authentication, not how the partner authenticates. Key-only login for the group is a separate line in the same Match block, such as `AuthenticationMethods publickey`.
- The partner should own their chroot directory so they can upload into it.
- ForceCommand internal-sftp also blocks port and agent forwarding.
- The chroot needs a copy of a shell and its libraries for SFTP to work.
- A Match block ends at the next blank line or unindented line.
- Mapping Subsystem sftp to internal-sftp stops the partner from getting a shell.