Log inSign up

Give file access over SFTP while keeping users in their own folder

Create a group such as sftponly, give each user a home like /srv/sftp/alice owned by root with a writable upload folder inside, and add a Match Group sftponly block to /etc/ssh/sshd_config with ChrootDirectory /srv/sftp/%u and ForceCommand internal-sftp. After sudo sshd -t and a restart, those users can transfer files but cannot open a shell or see the rest of the disk.

Topic: Server basics · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

When do SFTP-only accounts make sense?

Use them when a person or a program needs to drop or fetch files but should not run commands: a designer uploading site assets, a partner delivering nightly reports, a camera or backup tool that speaks SFTP. The OpenSSH server you already use for administration does the work, so there is no extra daemon and no unencrypted protocol like plain FTP.

A chroot changes what the user sees as the root of the file system. Inside it, / is their own folder, and paths like /etc or other customers do not exist for them. Combined with internal-sftp, the account has no shell and no access to programs on the server. If you need classic FTP clients instead, see vsftpd with FTPS; for your own admin transfers plain SFTP needs none of this setup.

What are the ownership rules for a chroot?

OpenSSH is strict here, and almost every failed setup breaks this rule: the chroot directory and every directory above it must be owned by root and not writable by any other user or group. If /srv/sftp/alice belongs to alice, the login is refused with a bad ownership message in the log.

Because the user cannot write to the top of their own chroot, you create a subfolder inside it that they do own. The layout used below:

/srv/sftp              root:root  755
/srv/sftp/alice        root:root  755   (chroot, read only for alice)
/srv/sftp/alice/upload alice:alice 750  (where alice writes)

Read more about modes and owners in file permissions.

How do I create the group and the first user?

Create the group once, then add a user whose home is the chroot path and whose shell is nologin:

sudo groupadd sftponly
sudo mkdir -p /srv/sftp
sudo useradd -d /srv/sftp/alice -s /usr/sbin/nologin -G sftponly -M alice
sudo passwd alice

The -M flag skips creating the home, because you build it by hand with the right owners:

sudo mkdir -p /srv/sftp/alice/upload
sudo chown root:root /srv/sftp /srv/sftp/alice
sudo chmod 755 /srv/sftp /srv/sftp/alice
sudo chown alice:alice /srv/sftp/alice/upload
sudo chmod 750 /srv/sftp/alice/upload

The path /usr/sbin/nologin exists on all four distributions. Skip passwd if the user will log in with a key only.

How do I configure sshd?

Open /etc/ssh/sshd_config with sudoedit and add this block at the very end of the file. A Match section applies to every line after it, so it must come last:

Match Group sftponly
    ChrootDirectory /srv/sftp/%u
    ForceCommand internal-sftp -d /upload
    AllowTcpForwarding no
    AllowAgentForwarding no
    X11Forwarding no
    PermitTTY no

%u expands to the login name, so one block serves every member of the group. internal-sftp runs the SFTP server inside the sshd process, which means no helper programs are needed inside the chroot. The -d /upload option opens sessions directly in the writable folder.

Test the syntax before restarting, then restart the service. It is called ssh on Ubuntu and Debian and sshd on Rocky and Alma:

sudo sshd -t
sudo systemctl restart ssh     # Ubuntu, Debian
sudo systemctl restart sshd    # Rocky, AlmaLinux

Keep your current admin session open until a new login works.

Should these users log in with a password or a key?

Keys are safer, and many automated clients support them. If you turned off passwords server wide, as recommended in SSH keys and hardening, either use keys for SFTP users too or allow passwords only for this group by adding PasswordAuthentication yes inside the Match block.

For a key, place it in a .ssh folder inside the chroot. sshd reads it before entering the jail:

sudo mkdir /srv/sftp/alice/.ssh
sudo nano /srv/sftp/alice/.ssh/authorized_keys
sudo chown -R alice:alice /srv/sftp/alice/.ssh
sudo chmod 700 /srv/sftp/alice/.ssh
sudo chmod 600 /srv/sftp/alice/.ssh/authorized_keys

Paste the public key the person sends you, one line, into authorized_keys. With password access enabled, add fail2ban to slow down guessing.

How do I test that the jail works?

From another computer, connect with any SFTP client:

sftp alice@198.51.100.24
sftp> pwd
sftp> ls /
sftp> put report.csv
sftp> cd /etc

pwd should print /upload, ls / should show only upload (and .ssh if present), the upload should succeed and cd /etc should fail. Then confirm that a shell is refused:

ssh alice@198.51.100.24

The connection should close with a message that only SFTP is allowed. Graphical clients like FileZilla and WinSCP work the same way; choose the SFTP protocol on port 22.

How do I share a web folder with an SFTP user?

A chroot cannot follow symbolic links that point outside it, so a link to /var/www/site will appear broken. Use a bind mount instead, which shows the real directory inside the jail:

sudo mkdir /srv/sftp/alice/site
echo "/var/www/site /srv/sftp/alice/site none bind 0 0" | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
sudo mount /srv/sftp/alice/site

Then give alice write access through a shared group rather than ownership, for example by adding her to a www-data or project group and setting group write on the web folder. On Rocky and Alma the web content keeps its SELinux label through the bind mount.

How do I troubleshoot failed logins?

Read the SSH log right after a failed attempt:

sudo journalctl -u ssh -n 30      # Ubuntu, Debian
sudo journalctl -u sshd -n 30     # Rocky, AlmaLinux

bad ownership or modes for chroot directory means a folder on the path is not owned by root or is group writable; fix it with chown root:root and chmod 755. A session that connects but cannot write usually means the user tried to write at the top of the chroot instead of in upload. If permissions look right on Rocky or Alma and uploads still fail, run sudo ausearch -m AVC -ts recent | audit2why as described in the SELinux guide.

To remove a user later, run sudo userdel alice and delete the folder once you have saved anything you need.

Questions about SFTP-only users in a chroot

Can an SFTP-only user see other users files?

No. Each user is locked into their own chroot folder, and other folders under /srv/sftp are outside the visible tree.

Does this change SFTP for my admin account?

No. The Match block applies only to members of the sftponly group. Your sudo user keeps a normal shell and full SFTP access.

Can I limit how much an SFTP user stores?

Yes, with disk quotas on the file system, or by placing the chroot on a separate size limited file system. Watch usage with the tools in the disk usage guide.

Which port do SFTP clients use?

The same port as SSH, normally 22. If you moved SSH to another port, tell users the new number; on Rocky and Alma also label it with semanage.

Can I log what users upload?

Yes. Add -l INFO to the ForceCommand line, for example ForceCommand internal-sftp -d /upload -l INFO, and file operations appear in the SSH journal.

Is SFTP the same as FTPS?

No. SFTP runs inside SSH on one port. FTPS is classic FTP wrapped in TLS and needs a separate server such as vsftpd plus passive ports.