Strovent SystemsSign in
Reference

SFTP chroot: bad ownership or modes for chroot directory

sshd closes a chroot SFTP login when any directory in the ChrootDirectory path is not root-owned or is group- or world-writable. Fix the owners and modes.

The SFTP client logs in, then stops at once with Connection closed. A plain ssh login to the same account may say Connection to host.example.com closed by remote host. The real reason is only in the server log, in one of these forms:

fatal: bad ownership or modes for chroot directory "/srv/sftp/user1"
fatal: bad ownership or modes for chroot directory component "/srv/sftp/"

The password or key was accepted. The session failed in the next step, when sshd checked the folder named by ChrootDirectory.

The rule sshd checks

Before it changes the root to the ChrootDirectory path, sshd checks every directory in that path, from / down to the last one. Each one must be:

  • owned by root (user ID 0), and
  • not writable by group or others (no w in the group or other bits).

The second log line names the parent directory that failed (component). The first names the chroot directory itself. This check always runs. StrictModes no does not turn it off.

A related message, chroot path "..." is not a directory, means one part of the path is a file or link to a file.

Why it happens

  • ChrootDirectory %h with a normal home folder. %h is the user's home, and a home folder is usually owned by the user. That fails the root-owned rule.
  • A shared upload folder made writable for a group (mode 775 or 770) or for everyone (777).
  • A parent folder such as /srv or /data that someone made group-writable.

The fix

Make the chroot directory and every parent root-owned with no group or other write bit, for example mode 755. Give the user a writable folder inside it:

sudo chown root:root /srv/sftp /srv/sftp/user1
sudo chmod 755 /srv/sftp /srv/sftp/user1
sudo mkdir /srv/sftp/user1/upload
sudo chown user1:user1 /srv/sftp/user1/upload

Folders inside the chroot are not checked, so upload can belong to the user. On Linux, namei -l /srv/sftp/user1 lists the owner and mode of every part of the path in one go.

"I can log in but cannot upload"

This is the other half of the same rule. The chroot directory belongs to root, so the user cannot create files at its top level and gets Permission denied. Upload into the user-owned folder instead.

To start SFTP sessions in that folder, add -d to the SFTP server command in sshd_config, for example ForceCommand internal-sftp -d /upload. The path is read inside the chroot.

Keys still have their own checks

The chroot rule is separate from key login. sshd reads authorized_keys before the chroot, from the home folder in the account entry. If key login fails with Permission denied (publickey), check that file and its folders as for any account; see Authorized keys and permissions. For the chroot-specific causes, see SFTP chroot user gets "Permission denied (publickey)" with the right key.

Sources

Spotted a mistake? Email [email protected].