DEV Community

Nate
Nate

Posted on Originally published at shadeterm.com

Build a Linux SFTP Drop Box with OpenSSH and Chroot

SFTP is already part of OpenSSH on a typical Ubuntu or Debian server. You don’t need to install a separate FTP daemon: install OpenSSH Server, make sure the SSH service is reachable, and test a transfer with the sftp client.

The important design choice is what kind of access the account should have. A regular Linux account can use SFTP wherever its filesystem permissions allow. For an account that should only transfer files, OpenSSH can force SFTP and optionally confine the user to a directory with a chroot.

Start with a basic SFTP connection

Run these commands on the Ubuntu or Debian machine that will receive files:

sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh
sudo systemctl status ssh --no-pager
Enter fullscreen mode Exit fullscreen mode

Ubuntu and Debian commonly name the service ssh; other distributions may call it sshd. SSH and SFTP normally use TCP port 22, so check the server firewall and any cloud firewall or security group if the service is running but clients cannot connect. If you’re setting up SSH access on Ubuntu for the first time, this Ubuntu OpenSSH setup guide covers the server and connection checks.

Create a dedicated account for a simple test, or use an existing account with appropriate permissions:

sudo adduser sftpuser
Enter fullscreen mode Exit fullscreen mode

From a separate client, create a small file and connect. Replace SERVER_IP with the server’s address:

printf 'SFTP upload check\n' > sftp-smoke-test.txt
sftp sftpuser@SERVER_IP
Enter fullscreen mode Exit fullscreen mode

At the sftp> prompt, test both directions:

put sftp-smoke-test.txt
ls -l sftp-smoke-test.txt
get sftp-smoke-test.txt sftp-downloaded.txt
bye
Enter fullscreen mode Exit fullscreen mode

The local file must exist on the client before put can upload it. Reaching the prompt confirms the SFTP subsystem started; a successful put and get also test file access.

For a non-default SSH port, the SFTP client uses uppercase -P. Use -i to select a private key:

sftp -P 2222 -i ~/.ssh/id_ed25519 sftpuser@SERVER_IP
Enter fullscreen mode Exit fullscreen mode

SFTP uses SSH authentication. Don’t enable password authentication globally just to make a test account work; use the authentication policy already configured on the server.

Restrict an account to SFTP and an upload directory

A chroot is useful for accounts that should not browse the host filesystem or get an interactive shell. It adds configuration and permission requirements, so it’s unnecessary for trusted users who already have normal SSH access.

First, create a group for restricted users and add the account. Every member of this group will receive the restrictions in the configuration below, so don’t add administrator accounts:

sudo groupadd --force sftpusers
sudo usermod -aG sftpusers sftpuser
id sftpuser
Enter fullscreen mode Exit fullscreen mode

Set up a root-owned chroot and a separate writable directory inside it:

sudo install -d -o root -g root -m 755 /sftp
sudo install -d -o root -g root -m 755 /sftp/sftpuser
sudo install -d -o sftpuser -g sftpusers -m 750 /sftp/sftpuser/uploads
namei -l /sftp/sftpuser/uploads
Enter fullscreen mode Exit fullscreen mode

The chroot directory and its parent must be owned by root and must not be writable by other users. The uploads directory is where the account gets write access. Making the chroot itself writable, or using chmod 777, is not a fix for permission problems.

Add this block near the end of /etc/ssh/sshd_config, after the global configuration:

Match Group sftpusers
    ChrootDirectory /sftp/%u
    ForceCommand internal-sftp -d /uploads
    DisableForwarding yes
    PermitTTY no
Enter fullscreen mode Exit fullscreen mode

%u expands to the authenticated username. ForceCommand internal-sftp prevents an ordinary shell session, while -d /uploads starts the SFTP session in the writable directory. DisableForwarding and PermitTTY no further limit access for matched accounts.

Keep any existing Subsystem sftp ... directive. Don’t add a duplicate: it’s a global setting, not part of the Match block. Match rules also affect subsequent configuration lines, so place the block carefully and avoid putting global directives after it.

Validate before applying changes

Check the configuration syntax and inspect the effective settings for the test user:

sudo /usr/sbin/sshd -t
sudo /usr/sbin/sshd -T -C user=sftpuser,host=localhost,addr=127.0.0.1 \
  | grep -Ei '^(chrootdirectory|forcecommand|disableforwarding|permittty) '
namei -l /sftp/sftpuser/uploads
Enter fullscreen mode Exit fullscreen mode

A successful sshd -t prints nothing. The effective settings should show the intended chroot and internal-sftp command. These checks help catch configuration errors, but a real client connection is still needed to verify authentication and permissions.

Keep your current administrator session open while changing remote access settings. If the configuration validates, reload the service:

sudo systemctl reload ssh
Enter fullscreen mode Exit fullscreen mode

Then connect from a separate client and test an upload again. The restricted user should see the jail rather than the host filesystem, and should be able to write inside /uploads. A graphical client can use the same host, port, username, and authentication method; it doesn’t bypass server-side permissions. If you need to choose a client, this SFTP client comparison outlines options for different platforms and workflows.

Troubleshoot the failure, not the security policy

If login or transfer fails, start with read-only checks rather than disabling security controls:

  • Connection refused: Check systemctl status ssh, the actual listening port, and firewall rules.
  • Permission denied (publickey): Verify the username, offered key, and the server’s authorized-key configuration.
  • Subsystem request failed: Check that there is one valid Subsystem sftp directive.
  • Bad ownership or modes for chroot directory: Inspect every path component with namei -l; the chroot path must satisfy OpenSSH’s ownership requirements.
  • Login works but upload fails: Check write permission on /uploads inside the jail, not on the chroot root.
  • Unexpected shell access: Check id sftpuser and the effective forcecommand from sshd -T -C.

For recent server-side details, inspect the SSH logs:

sudo journalctl -u ssh -u sshd --since '15 minutes ago' --no-pager | tail -n 80
Enter fullscreen mode Exit fullscreen mode

A standard OpenSSH installation may only need a normal account and a successful transfer test. Add a chroot when you have a specific isolation requirement, and verify the directory ownership, effective SSH rules, and a real upload before granting the account access.

I originally published a more detailed version of this guide on the ShadeTerm blog.

I'm also building ShadeTerm — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)