Home How ToHow to Set Up an SFTP Server with Chroot Jail on Linux

How to Set Up an SFTP Server with Chroot Jail on Linux

by Knowledge Base Linux
Set up SFTP server with chroot jail for isolated file uploads

Clients and teammates often need to upload files to your server — but handing out full SSH shell accounts for that is a security risk. When you set up an SFTP server with a chroot jail, users get encrypted file transfers and nothing else: no shell, no snooping around the filesystem, and no access outside their own directory. In this guide you will set up an SFTP server on Linux using the OpenSSH built-in SFTP subsystem, lock users into a chroot, and test the result.

Why Set Up an SFTP Server with Chroot Jails?

  • Encryption: SFTP runs over SSH, unlike legacy FTP which sends passwords in clear text
  • No extra software: the internal-sftp subsystem ships with OpenSSH, already installed on nearly every server
  • Isolation: a chroot jail makes the user’s home directory appear as the root of their world
  • No shell access: matched users can transfer files but cannot execute commands

If SSH itself is new to you, first review how to SSH into a Linux server and how to generate SSH keys — your SFTP users can authenticate with those same keys.

Prerequisites – How to Set Up an SFTP

  • A Linux server (Ubuntu/Debian or RHEL family) with OpenSSH server installed and sudo access
  • A second terminal or console access, so a bad sshd edit cannot lock you out
  • Port 22 open in your firewall

Step 1: Create the Group and Directory Structure – How to Set Up an SFTP

All jailed users will belong to one group, which makes the sshd configuration simple:

sudo groupadd sftpusers
sudo mkdir -p /data/sftp

The chroot requirement that makes or breaks this setup: every component of the chroot path must be owned by root and not writable by anyone else. If sshd detects group-writable chroot directories it will refuse the login. Our layout is:

  • /data/sftp/<user> — the chroot, owned by root:root, mode 755
  • /data/sftp/<user>/uploads — the writable folder, owned by the user

Step 2: Create a Jailed User

sudo useradd -g sftpusers -d /data/sftp/client1 -s /usr/sbin/nologin client1
sudo mkdir -p /data/sftp/client1/uploads
sudo chown root:root /data/sftp/client1
sudo chmod 755 /data/sftp/client1
sudo chown client1:sftpusers /data/sftp/client1/uploads
sudo passwd client1

The nologin shell plus the sshd rules below guarantee this account can only ever transfer files.

Set up SFTP server encrypted file transfer into a jailed folder

Step 3: Configure sshd for the SFTP Server

Edit /etc/ssh/sshd_config. First, make sure the subsystem line uses the internal SFTP server:

Subsystem sftp internal-sftp

Then add a Match block at the very end of the file (Match blocks apply until the end of the file, so nothing global may follow them):

Match Group sftpusers
    ChrootDirectory /data/sftp/%u
    ForceCommand internal-sftp
    PermitTunnel no
    AllowAgentForwarding no
    AllowTcpForwarding no
    X11Forwarding no

%u expands to the username, giving every member of the group their own jail automatically. Validate the configuration and restart SSH:

sudo sshd -t
sudo systemctl restart sshd

Step 4: Test the SFTP Server

From another machine, connect as the jailed user:

sftp client1@your-server
sftp> pwd
sftp> ls
sftp> cd uploads
sftp> put report.pdf
sftp> exit

The remote working directory should show as / — the user is inside the jail and cannot navigate above it. Trying to log in with regular SSH should fail immediately:

ssh client1@your-server
# This service allows sftp connections only.

Detailed behavior of every directive is documented in the official sshd_config manual.

Troubleshooting

“Connection closed” right after login: almost always chroot permissions. Run sudo sshd -T -C user=client1 | grep chroot to confirm the effective directory, then verify every path component is root-owned and not group-writable, and check /var/log/auth.log (or /var/log/secure) for the exact fatal message.

User can still get a shell: the Match block is not at the end of the file, or the user is not in the sftpusers group — confirm with id client1.

Uploads fail with permission denied: users cannot write to the jail root by design. Direct them to the uploads folder, which is the one directory owned by them.

Optional: Key-Based Authentication for SFTP Users

Passwords work, but keys are better, especially for automated uploads from scripts or CI jobs. Add the user’s public key to /data/sftp/client1/.ssh/authorized_keys — note the path: because the account’s home is the chroot, the .ssh directory lives inside the jail and must be owned by the user with mode 700, while the authorized_keys file itself is mode 600. Once a key works, you can disable password logins for the group by adding PasswordAuthentication no inside the same Match block. Scripts then connect non-interactively with sftp -i ~/.ssh/id_ed25519 -b batchfile client1@your-server, and password guessing against these accounts becomes impossible because there is no password path at all.

Conclusion

You now know how to set up an SFTP server that gives each user a private, encrypted, jailed drop-box on your existing SSH infrastructure. Add users by repeating the user-creation step, prefer key authentication for automation, and audit transfers in the auth log. It is the safest simple answer to “how do I let people send me files?” — without ever giving away a shell.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.