How to Set Up an SFTP-Only User on Ubuntu 24.04 with Chroot

Aybars Altınyay
Aybars Altınyay
Platform Engineer & Technical Writer
IntermediateUpdated Oct 27, 202618 min read~30 minutes total
SFTPOpenSSHUbuntu 24.04ChrootSecurityFile TransferAccess Control
Last reviewed:
Tutorial
How to Set Up an SFTP-Only User on Ubuntu 24.04 with Chroot
rafftechnologies.com/learn
On this page

Prerequisites

  • A Raff Ubuntu 24.04 Linux VM, SSH access with a non-root sudo user, a second admin SSH session for safe testing, and the current SSH port allowed by the firewall

Don’t have a server yet?

An SFTP-only account on Ubuntu 24.04 lets a user transfer files over SSH without giving that account an interactive shell or unrestricted access to the server filesystem. The safest baseline is to use OpenSSH's built-in internal-sftp, place the user inside a root-owned chroot, give them a writable subdirectory inside that jail, and disable every SSH forwarding feature that the account does not need.

This tutorial uses a group-based OpenSSH configuration so you can add more restricted users later without duplicating per-user Match blocks. It also fixes several common mistakes in older SFTP guides: assuming a writable chroot root is allowed, editing only the main sshd_config, leaving a Match block open inside sshd_config.d, restarting the wrong daemon blindly, and checking syntax without verifying the effective configuration that actually applies to the restricted user.

Ubuntu's current OpenSSH documentation recommends modular files in /etc/ssh/sshd_config.d/, running sshd -t before a restart, and restarting ssh.service after normal server configuration changes. OpenSSH also requires every component of ChrootDirectory to be root-owned and not writable by other users or groups. internal-sftp is useful here because it runs inside sshd and does not require copying a shell, libraries, or device nodes into the jail.

Raff Technologies is the Ubuntu VM platform used by the original tutorial. The original setup was tested on Ubuntu 24.04 LTS on a Raff Linux VM with 2 vCPU / 4 GB RAM; the OpenSSH configuration and Ubuntu service guidance were re-verified on September 4, 2026.

Prerequisites:

  • A Raff Linux VM running Ubuntu 24.04
  • SSH access with a non-root sudo user
  • A second SSH session available while you change sshd settings
  • Port 22/tcp or your existing SSH port allowed by the firewall
  • A test username that is not your current administrator account

The examples use the restricted account sftpclient and group sftponly.

Step 1 — Verify OpenSSH and keep your current admin session open

Check that the OpenSSH server package is installed:

Bash
dpkg -s openssh-server | grep -E '^(Status|Version):'

If it is not installed:

Bash
sudo apt update sudo apt install -y openssh-server

Check the service:

Bash
sudo systemctl is-active ssh.service

Check the server binary version:

Bash
/usr/sbin/sshd -V 2>&1 | head -1

Before changing anything, keep your current working SSH session open and, if possible, open a second administrator session. Do not test new SFTP rules by modifying the account you are currently using to administer the VM.

Ubuntu's current OpenSSH server documentation warns that a bad SSH configuration can lock you out and recommends validating the configuration before restarting the service.

Verify: openssh-server should be installed, ssh.service should be active, and you should still have a working administrator session that is not the SFTP test account.

Step 2 — Back up the SSH configuration before editing it

Create a timestamped backup of the main configuration and the current snippet directory:

Bash
sudo cp -a /etc/ssh/sshd_config \ "/etc/ssh/sshd_config.backup.$(date +%Y%m%d-%H%M%S)" sudo cp -a /etc/ssh/sshd_config.d \ "/etc/ssh/sshd_config.d.backup.$(date +%Y%m%d-%H%M%S)"

Ubuntu includes this line near the top of its default server configuration:

Text
Include /etc/ssh/sshd_config.d/*.conf

That means drop-in files are the preferred place for a small custom policy such as an SFTP-only group. Ubuntu also documents that OpenSSH generally uses the first value it obtains for most directives, which makes snippet ordering and Match scope worth checking rather than assuming.

Verify:

Bash
ls -ld /etc/ssh/sshd_config.backup.* /etc/ssh/sshd_config.d.backup.* | tail

A backup of both the main file and snippet directory should exist before you continue.

Step 3 — Create the SFTP-only group and user

Create the group:

Bash
sudo groupadd --force sftponly

Create the restricted test user if it does not already exist:

Bash
sudo useradd -m -s /bin/bash -G sftponly sftpclient

Set a password for the initial test:

Bash
sudo passwd sftpclient

This tutorial does not rely on /usr/sbin/nologin to enforce SFTP-only behavior. The OpenSSH Match Group block will force internal-sftp, disable forwarding, and disable TTY allocation. This avoids mixing shell-policy behavior with SFTP subsystem testing and makes the actual SSH restriction explicit and testable.

If the account already exists, add it to the group without removing its other memberships:

Bash
sudo usermod -aG sftponly sftpclient

Verify:

Bash
id sftpclient getent passwd sftpclient

The output should show membership in sftponly. Do not continue if you accidentally added your current administrator account to this group.

Step 4 — Create a root-owned chroot with a writable upload directory

OpenSSH checks ChrootDirectory ownership unconditionally. Every component of the chroot path must be root-owned and must not be writable by another user or group.

Use the user's home directory as the jail root:

Bash
sudo chown root:root /home/sftpclient sudo chmod 755 /home/sftpclient

Create a writable directory inside the jail:

Bash
sudo mkdir -p /home/sftpclient/uploads sudo chown sftpclient:sftpclient /home/sftpclient/uploads sudo chmod 750 /home/sftpclient/uploads

The resulting model is:

Text
/home/sftpclient root:root 755 <- chroot root /home/sftpclient/uploads sftpclient:* 750 <- writable area

Do not make /home/sftpclient user-writable and do not use chmod 777. OpenSSH's Ubuntu 24.04 man page states that all path components used for ChrootDirectory must be root-owned and not writable by any other user or group.

Verify:

Bash
namei -l /home/sftpclient stat -c '%U:%G %a %n' /home/sftpclient /home/sftpclient/uploads

The chroot root should be root:root and not group/other writable; the uploads directory should be writable by sftpclient.

Step 5 — Create an isolated OpenSSH Match Group policy

Create a dedicated snippet:

Bash
sudo tee /etc/ssh/sshd_config.d/60-sftp-only.conf > /dev/null <<'EOF' Match Group sftponly ChrootDirectory %h ForceCommand internal-sftp -d /uploads DisableForwarding yes PermitTTY no PermitUserRC no Match all EOF

The important directives are:

  • ChrootDirectory %h — makes the user's real home directory the jail root;
  • ForceCommand internal-sftp -d /uploads — forces the in-process SFTP server and starts the user in the writable /uploads directory inside the jail;
  • DisableForwarding yes — disables TCP, agent, X11, and StreamLocal forwarding in one directive;
  • PermitTTY no — prevents TTY allocation;
  • PermitUserRC no — prevents execution of user SSH rc files;
  • Match all — ends the restricted Match scope before parsing continues.

That final Match all is especially important in a drop-in file. Ubuntu loads /etc/ssh/sshd_config.d/*.conf from the main configuration, and a Match context continues until another Match directive or end of the full configuration stream. Ending the snippet with Match all prevents later global settings from accidentally remaining inside the sftponly Match context.

You do not need to replace Ubuntu's global Subsystem sftp ... line for this design. ForceCommand internal-sftp directly forces the in-process SFTP server for matching users.

Verify:

Bash
sudo cat /etc/ssh/sshd_config.d/60-sftp-only.conf

The file should contain exactly one Match Group sftponly block followed by Match all.

Step 6 — Validate both syntax and the effective Match configuration

First run OpenSSH's syntax test:

Bash
sudo sshd -t

No output means the syntax and host-key checks passed.

Then inspect the effective configuration for sftpclient. sshd -T prints the resulting configuration, while -C applies Match rules for a simulated connection:

Bash
sudo sshd -T \ -C user=sftpclient,host=localhost,addr=127.0.0.1,laddr=127.0.0.1,lport=22 \ | grep -E '^(chrootdirectory|forcecommand|disableforwarding|permittty|permituserrc) '

Expected values include:

Text
chrootdirectory %h forcecommand internal-sftp -d /uploads disableforwarding yes permittty no permituserrc no

This check is stronger than syntax validation alone because it proves that the Match Group policy actually applies to the account you intend to restrict.

Verify: sshd -t should produce no error and sshd -T -C ... should show the SFTP-only settings above for sftpclient.

Step 7 — Restart OpenSSH safely and verify administrator access first

Ubuntu's current OpenSSH server documentation recommends applying normal sshd configuration changes with:

Bash
sudo systemctl restart ssh.service

Do that only after sshd -t succeeds:

Bash
sudo systemctl restart ssh.service

Immediately verify the service:

Bash
sudo systemctl is-active ssh.service

Before closing your original SSH window, open a new administrator SSH session and make sure it works.

Ubuntu 24.04 can use socket activation in some OpenSSH configurations, which matters particularly when changing listener settings such as Port or ListenAddress. This tutorial does not change those listener directives. For the Match policy used here, follow Ubuntu's documented ssh.service restart path and validate the actual result with a fresh connection.

Verify: ssh.service should be active and a new administrator SSH connection should succeed before you test sftpclient.

Step 8 — Test SFTP login, landing directory, upload, and download

From another machine or another local terminal, connect with SFTP:

Bash
sftp sftpclient@SERVER_IP

After authentication, confirm the working directory:

Text
sftp> pwd

Because the Match policy uses internal-sftp -d /uploads, the user should start in:

Text
Remote working directory: /uploads

Create a local test file before connecting, or use an existing harmless file. From the SFTP prompt:

Text
sftp> put test-upload.txt sftp> ls -la sftp> get test-upload.txt test-download.txt

Confirm that the user cannot leave the jail:

Text
sftp> cd / sftp> ls

The / visible to this user is the chroot root, not the real server filesystem root. Paths such as the server's real /etc are outside the jail.

Verify: The user should land in /uploads, be able to upload/download there, and remain confined to the chroot filesystem.

Step 9 — Verify shell commands, TTYs, and forwarding are blocked

SFTP success is only half the test. Confirm that the same account cannot obtain a shell command channel:

Bash
ssh sftpclient@SERVER_IP 'id'

Because ForceCommand internal-sftp replaces the requested command, this should not execute id as a shell command.

Try an interactive SSH session:

Bash
ssh sftpclient@SERVER_IP

The account should not receive a normal shell prompt.

The DisableForwarding yes rule also blocks the account from being used for SSH forwarding. For example, a local forwarding request should fail rather than create a tunnel:

Bash
ssh -N -L 15432:127.0.0.1:5432 sftpclient@SERVER_IP

Do not leave this command running if your client behaves unexpectedly; stop it with Ctrl+C.

OpenSSH documents that ForceCommand alone does not disable other forwarding channels, which is why this tutorial also sets DisableForwarding yes.

Verify: sftpclient should transfer files through SFTP but should not receive a shell, execute arbitrary SSH commands, allocate a TTY, or create SSH forwarding channels.

Step 10 — Restrict network access when the SFTP client has a stable source IP

SFTP uses SSH transport, so the network port is the server's SSH port — normally TCP 22 unless you deliberately changed it.

If this SFTP account is used only from a known office, VPN, CI runner, or partner IP, firewall restriction adds a useful independent control.

For example, with UFW and a stable client address 198.51.100.20:

Bash
sudo ufw allow from 198.51.100.20 to any port 22 proto tcp

Do not remove your broader administrator SSH rule until you have confirmed how all legitimate administrators connect. A firewall change can lock you out independently of the SFTP configuration.

If your VM also uses a cloud-side security group, apply the same source-IP principle there when appropriate.

For a complete firewall workflow, use Set Up UFW Firewall on Ubuntu 24.04.

Verify: Review the firewall with sudo ufw status numbered and confirm it still permits every required administrator path as well as the intended SFTP client source.

Step 11 — Monitor SFTP authentication and errors with journalctl

Ubuntu's current server documentation uses the ssh.service journal for OpenSSH troubleshooting. Follow logs while you test:

Bash
sudo journalctl -fu ssh.service

Or inspect recent entries:

Bash
sudo journalctl -u ssh.service -n 100 --no-pager

Useful problems to look for include:

  • bad ownership or modes for chroot directory — a path component is not root-owned or is writable by another user/group;
  • authentication failures — password/key or account-policy problem;
  • Connection closed immediately after authentication — often a chroot, directory, or Match-policy problem;
  • missing /uploads — the forced start directory does not exist inside the jail.

Do not depend on hard-coded /var/log/auth.log availability for every Ubuntu logging setup; the systemd journal is the most portable first check on current Ubuntu Server.

Verify: A successful SFTP connection should appear in the SSH journal without a chroot ownership error or repeated authentication failure.

Step 12 — Add another SFTP-only user without editing sshd_config again

Create another user and add it to the same group:

Bash
sudo useradd -m -s /bin/bash -G sftponly client2 sudo passwd client2

Make the new home directory a valid chroot root:

Bash
sudo chown root:root /home/client2 sudo chmod 755 /home/client2

Create the writable directory:

Bash
sudo mkdir -p /home/client2/uploads sudo chown client2:client2 /home/client2/uploads sudo chmod 750 /home/client2/uploads

No OpenSSH configuration edit is necessary because the group Match rule already exists. No service restart is required merely because a new user joined the group.

Check that the effective Match configuration applies to the second user as well:

Bash
sudo sshd -T \ -C user=client2,host=localhost,addr=127.0.0.1,laddr=127.0.0.1,lport=22 \ | grep -E '^(chrootdirectory|forcecommand|disableforwarding) '

Verify: client2 should inherit the same chroot, forced SFTP command, and forwarding restrictions without another sshd_config change.

Step 13 — Run the final end-to-end SFTP security check

Run the final checklist before using the account for real file transfers.

On the server:

Bash
sudo sshd -t sudo systemctl is-active ssh.service stat -c '%U:%G %a %n' /home/sftpclient /home/sftpclient/uploads sudo sshd -T \ -C user=sftpclient,host=localhost,addr=127.0.0.1,laddr=127.0.0.1,lport=22 \ | grep -E '^(chrootdirectory|forcecommand|disableforwarding|permittty|permituserrc) '

From the client:

Bash
sftp sftpclient@SERVER_IP

Inside SFTP:

Text
pwd put test-upload.txt ls -la

In another client terminal:

Bash
ssh sftpclient@SERVER_IP 'uname -a'

The SFTP operation should work, while the arbitrary SSH command should not run as a shell command.

Verify: The final state is correct only when file transfer works, the user is confined to the jail, the chroot root is not user-writable, arbitrary SSH commands do not execute, forwarding is disabled, and normal administrator SSH access still works.

Step 14 — Roll back or remove the SFTP-only setup safely

To remove only the Match policy, first move the snippet out of the active .conf path:

Bash
sudo mv /etc/ssh/sshd_config.d/60-sftp-only.conf \ /etc/ssh/60-sftp-only.conf.disabled

Validate before restarting:

Bash
sudo sshd -t

Then apply the change:

Bash
sudo systemctl restart ssh.service

If the restricted account is no longer needed, remove it only after confirming that uploaded files have been archived or transferred elsewhere:

Bash
sudo userdel sftpclient

Do not use userdel -r until you have deliberately decided to delete /home/sftpclient and its uploaded files.

If this was a disposable lab and the data is safe to delete:

Bash
sudo rm -rf /home/sftpclient

Remove the group only when no remaining account depends on it:

Bash
getent group sftponly sudo groupdel sftponly

Verify: sudo sshd -t should still pass, administrator SSH should work after the restart, and only the users/data you intentionally removed should be gone.

Troubleshooting

fatal: bad ownership or modes for chroot directory

Inspect every component of the jail path:

Bash
namei -l /home/sftpclient

/, /home, and /home/sftpclient must satisfy OpenSSH's chroot ownership rules. The jail root should be root-owned and not writable by other users/groups. Only directories inside the jail, such as /uploads, should be user-writable.

SFTP authenticates and then disconnects immediately

Run:

Bash
sudo journalctl -u ssh.service -n 100 --no-pager

Then confirm the forced start directory exists:

Bash
ls -ld /home/sftpclient/uploads

Also re-check the effective policy:

Bash
sudo sshd -T \ -C user=sftpclient,host=localhost,addr=127.0.0.1,laddr=127.0.0.1,lport=22 \ | grep -E '^(chrootdirectory|forcecommand|disableforwarding) '

The user can connect by SFTP but cannot upload to /

That is expected. The chroot root must not be writable by the restricted user. Upload into /uploads, which is intentionally owned by sftpclient.

The SFTP user unexpectedly gets a shell

Check group membership:

Bash
id sftpclient

Then check the effective OpenSSH policy with sshd -T -C. If the account is not in sftponly or the Match rule is not being applied, fix that before allowing the account to remain active.

sshd -t fails after editing the snippet

Do not restart OpenSSH. Read the exact syntax error, fix the file, and rerun:

Bash
sudo sshd -t

Only restart ssh.service when the syntax test succeeds.

SFTP works but SSH port forwarding also works

The Match rule is either not applying or DisableForwarding yes is missing. Check the effective configuration with sshd -T -C and confirm:

Text
disableforwarding yes

Conclusion

You now have an SFTP-only OpenSSH account on Ubuntu 24.04 with a root-owned chroot, a writable upload directory, forced internal-sftp, forwarding disabled, no TTY allocation, explicit Match-scope termination, effective-configuration testing, service logs, end-to-end validation, and a rollback path.

The two details that prevent most SFTP chroot failures are straightforward: keep the chroot root owned by root and non-writable by the restricted user, and verify the effective OpenSSH Match configuration instead of assuming a syntactically valid file is applying the rules you intended.

For key-based administrator access, see Configure SSH Keys on Ubuntu. For broader host protection, continue with Secure Ubuntu 24.04 Server.

Deploy a Raff Ubuntu VM for restricted SFTP-only file transfers with a root-owned chroot, safe directory permissions, and tested SSH recovery.

Was this article helpful?
Aybars Altınyay
Aybars Altınyay
Platform Engineer & Technical Writer

Aybars is a Platform Engineer & Technical Writer at Raff Technologies. He works on Linux VM templates, self-hosted application workflows, and security-focused deployment validation, turning tested infrastructure procedures into clear, practical tutorials for developers and teams.

Put it into practice

Spin up a Raff server, follow the steps above on real infrastructure, and you are live. Linux or Windows, backed by a 14-day money-back guarantee.

Frequently asked questions

How do I create an SFTP-only user on Ubuntu 24.04?
Create a restricted group, add the user, make the user's home directory a root-owned chroot, create a writable subdirectory inside it, then apply `ChrootDirectory`, `ForceCommand internal-sftp`, and forwarding restrictions in an OpenSSH Match Group block.
Why must the SFTP chroot directory be owned by root?
OpenSSH checks every component of `ChrootDirectory` at session startup and requires it to be root-owned and not writable by other users or groups. Put user-writable files in a subdirectory such as `/uploads` inside the jail.
What is internal-sftp in OpenSSH?
`internal-sftp` is OpenSSH's in-process SFTP server. It is convenient for chroot setups because it does not require copying an external SFTP binary, shared libraries, shells, or device files into the jail.
How do I verify that a Match Group rule actually applies?
Use `sshd -T -C` with the target username and simulated connection values. This prints the effective OpenSSH configuration after Match rules are applied, which is more useful than syntax validation alone.
Why add Match all after an SFTP Match Group block?
A Match context continues until another Match directive or the end of the complete configuration stream. Ending a drop-in with `Match all` prevents later global configuration from accidentally remaining inside the restricted group's Match context.
Can an SFTP-only account still use SSH port forwarding?
It can unless forwarding is explicitly disabled. `ForceCommand internal-sftp` restricts the executed command, but OpenSSH documents that forwarding is separate. This tutorial uses `DisableForwarding yes` inside the Match block.
Should I use /usr/sbin/nologin for an SFTP-only user?
It is not required for this configuration. The tutorial makes the OpenSSH policy itself enforce SFTP-only access with `ForceCommand internal-sftp`, `DisableForwarding yes`, and `PermitTTY no`, then verifies that effective policy with `sshd -T -C`.
How do I troubleshoot an SFTP chroot connection that closes immediately?
Check `journalctl -u ssh.service`, verify every chroot path component with `namei -l`, confirm the writable directory exists, run `sshd -t`, and inspect the effective Match settings with `sshd -T -C`.