An SFTP chroot locks each user inside their own folder. In sshd_config, a Match Group sftp block applies ChrootDirectory and ForceCommand internal-sftp, and the chroot root directory must be owned by root and not writable by anyone else. The user can only write to a subdirectory they own and never gets a shell on the server.

The Match Group sftp block in sshd_config for an SFTP chroot

A Match Group sftp block applies its directives only to members of the sftp group and belongs at the end of /etc/ssh/sshd_config: everything after a Match line stays inside it until the next Match or the end of the file. Replace your existing Subsystem sftp line instead of adding a second one.

# /etc/ssh/sshd_config
Subsystem sftp internal-sftp

Match Group sftp
    ChrootDirectory /srv/sftp/%u
    ForceCommand internal-sftp
    AllowTcpForwarding no
    X11Forwarding no
    PermitTunnel no
  • ChrootDirectory /srv/sftp/%u: after authentication, sshd chroots to that path. %u expands to the username and %h to the home directory.
  • ForceCommand internal-sftp: ignores whatever command the client requests and starts the SFTP server built into sshd. Because it is in-process, you don't copy binaries or libraries into the chroot.
  • AllowTcpForwarding, X11Forwarding and PermitTunnel set to no: stop the account from being used as a tunnel, which ForceCommand alone does not prevent.

Every directive is documented in the sshd_config manual page. internal-sftp takes the same arguments as sftp-server, such as -l INFO to log file operations; see the sftp-server command-line options. Depending on your syslog setup, logging from inside the chroot may require a log socket reachable within that path.

SFTP chroot root directory permissions: owned by root, not writable by others

OpenSSH enforces a strict rule: the ChrootDirectory and every component of its path must be owned by root and must not be writable by group or others. That is why the user cannot write to the root of their chroot and needs a subdirectory of their own.

# Group and a user with no shell
groupadd sftp
useradd -m -G sftp -s /usr/sbin/nologin ana

# Chroot root: root:root and 755
mkdir -p /srv/sftp/ana/uploads
chown root:root /srv/sftp /srv/sftp/ana
chmod 755 /srv/sftp /srv/sftp/ana

# Folder the user can write to
chown ana:sftp /srv/sftp/ana/uploads
chmod 750 /srv/sftp/ana/uploads

# Public key: sshd reads it before the chroot
mkdir -p /home/ana/.ssh
cat ana.pub >> /home/ana/.ssh/authorized_keys
chown -R ana: /home/ana/.ssh
chmod 700 /home/ana/.ssh
chmod 600 /home/ana/.ssh/authorized_keys

The authorized_keys file lives in the real home, /home/ana, outside the chroot: sshd checks it during authentication, before the session is confined. Expected permissions are the usual ones, 700 for ~/.ssh and 600 for authorized_keys. Once logged in, Ana sees /srv/sftp/ana as / and can only upload to /uploads. On RHEL and derivatives you can also write the shell as /sbin/nologin.

How to test the SFTP chroot before and after restarting sshd

sshd -t validates the configuration without applying it; run it before every restart and keep your admin session open until a fresh login succeeds.

sshd -t
systemctl restart ssh    # Debian/Ubuntu; on RHEL and derivatives: sshd

sftp ana@server
sftp> pwd
Remote working directory: /
sftp> cd uploads
sftp> put report.pdf

ssh ana@server
This service allows sftp connections only.

If pwd returns / and you can't climb above it, the chroot works. Since OpenSSH 9.0, scp uses the SFTP protocol by default, so it works for these users too. rsync needs to run a remote binary and fails. Mosh is out as well: it starts mosh-server over an initial SSH connection, which ForceCommand blocks.

If you manage the server from your phone, the termi1 SSH terminal covers the whole loop. Your root session survives the restart over Mosh and tmux even if you switch from wifi to mobile data; its own keyboard gives you Esc and Ctrl for editing sshd_config in vim; and the built-in SFTP lets you open a second connection as ana and confirm only uploads shows up. With the AI in Review mode, the chown lands in the prompt without Enter being pressed, and on connections tagged as production, Production Guard holds back the systemctl restart. You can generate the test key on the phone itself: the private key never leaves the device, and only the public key goes into authorized_keys.

SFTP chroot user compared with a regular SSH account

A user with ForceCommand internal-sftp loses the shell, port forwarding and any tool that needs to run remote commands. Here is what changes:

What is measuredRegular SSH accountSFTP chroot user
Interactive shellYes, the one in /etc/passwdNo; ssh replies that only SFTP is allowed
Visible file treeWhatever permissions allowOnly /srv/sftp/username, shown as /
scp with OpenSSH 9.0 or laterWorksWorks, because scp uses SFTP by default
rsyncWorksFails: it needs to run rsync remotely
MoshWorks if mosh-server is installedFails: mosh-server cannot start
Port forwardingAllowed by defaultDisabled with AllowTcpForwarding no

4 mistakes when setting up an SFTP chroot in OpenSSH

A permissions failure leaves a single trace in the server log, bad ownership or modes for chroot directory, while the client only sees the connection close right after authentication.

  1. Giving the chroot root to the user. Running chown ana /srv/sftp/ana breaks the ownership rule and sshd drops the session. Writes belong in uploads.
  2. Leaving group write on /srv or /srv/sftp. The check walks every component of the path, not just the last one. namei -l /srv/sftp/ana shows owner and mode for each.
  3. Putting the Match block mid-file. Directives below Match Group sftp apply only to that group. Keep it at the end.
  4. Duplicating the Subsystem sftp line. sshd rejects a subsystem defined twice; edit the existing line and run sshd -t again.

Frequently asked questions

Why can't an SFTP chroot user write to their root folder?

An SFTP chroot user can't write to the root folder because OpenSSH requires the ChrootDirectory and its whole path to be owned by root, with no write access for group or others. The usual fix is a subdirectory inside it, such as uploads, owned by the user.

Does scp work with a chrooted SFTP user?

scp works with a chrooted SFTP user when both ends run OpenSSH 9.0 or later, because since April 2022 scp uses the SFTP protocol by default. rsync does not work, since it must run a binary on the server and ForceCommand internal-sftp prevents that.

Do I need to copy binaries into the chroot for SFTP?

Copying binaries into the chroot is unnecessary with ForceCommand internal-sftp, because the SFTP server is built into sshd itself. Only an external sftp-server would require replicating the binary and its libraries inside the jailed path, which is more fragile to maintain across updates.

Can a chrooted SFTP user log in with an SSH key?

Key authentication works for chrooted SFTP users. sshd reads authorized_keys during authentication, before applying the chroot, so the file can stay in the real home directory outside the jail, with ~/.ssh set to 700 and authorized_keys set to 600.