How to Set up OpenSSH SFTP only with Chroot on Ubuntu 26.04
Restricting a user to SFTP with a chroot means they can only reach one folder on an Ubuntu 26.04 server, and nothing else. You do this by creating a dedicated group, adding a Match Group block to the OpenSSH server config, and restarting the ssh service. Every server-side command below runs on the Ubuntu machine and needs administrator rights through sudo.
SFTP (SSH File Transfer Protocol) moves files over the same encrypted connection that SSH uses. A chroot (short for “change root”) makes one folder look like the whole file system to the user. Once it is set up, the user cannot browse anywhere else on the server.
Once the server is set up, you can connect from Windows 11 using the built-in sftp command.
Create a group named sftpusers, add a Match Group sftpusers block with ChrootDirectory /srv/sftp/%u and ForceCommand internal-sftp to /etc/ssh/sshd_config.d/50-sftp-chroot.conf, then run sudo sshd -t and sudo systemctl restart ssh on Ubuntu 26.04. The user can then log in only through SFTP, confined to their chroot folder.
Before you start
You need an Ubuntu 26.04 server with OpenSSH Server installed and a Windows 11 PC on the same network. This article uses 192.0.2.10 as the server address and USERNAME as the restricted account. Replace both with your own values.
Keep your current SSH session open until the new login works in a second window. The steps below also back up the config first.
Create the group, the user and the folders
Create the SFTP-only group and user
A group lets you apply the restriction to more than one account later. The user gets /usr/sbin/nologin as its shell (a setting that blocks normal terminal logins).
- Open a terminal on the Ubuntu server, or connect to it over SSH with an existing admin account.
- Create the group (requires administrator rights):Bash / Shell
sudo groupadd sftpusers
- Create the user with no shell and no local home folder (requires administrator rights):TheBash / Shell
sudo useradd -g sftpusers -s /usr/sbin/nologin -M -d /files USERNAME
-d /filesoption is a path inside the chroot, so the user lands in a folder they can write to. - Set a password (requires administrator rights):Type the password twice. Ubuntu confirms withBash / Shell
sudo passwd USERNAME
password updated successfully.
Build the chroot folder with the right ownership
OpenSSH refuses a chroot unless the chroot folder and every folder above it are owned by root. None of them can be writable by anyone else. The user gets a writable subfolder inside it.
- Create the folders (requires administrator rights):Bash / Shell
sudo mkdir -p /srv/sftp/USERNAME/files
- Give root ownership of the chroot path (requires administrator rights):Bash / Shell
sudo chown root:root /srv/sftp /srv/sftp/USERNAME
- Set the permissions so only root can write there (requires administrator rights):Bash / Shell
sudo chmod 755 /srv/sftp /srv/sftp/USERNAME
- Give the user ownership of the writable subfolder (requires administrator rights):Bash / Shell
sudo chown USERNAME:sftpusers /srv/sftp/USERNAME/files
Configure OpenSSH for SFTP only
Back up the config and add a drop-in file
Ubuntu reads extra settings from the /etc/ssh/sshd_config.d/ folder. A separate file keeps your change isolated, and deleting it undoes everything.
- Back up the main config (requires administrator rights):Bash / Shell
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
- Open a new file in the nano editor (requires administrator rights):Bash / Shell
sudo nano /etc/ssh/sshd_config.d/50-sftp-chroot.conf
- Paste these lines:HereCode
Match Group sftpusers
ChrootDirectory /srv/sftp/%u
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no%uexpands to the user's name, so each user gets their own chroot.ForceCommand internal-sftpruns the built-in SFTP server instead of a shell. The last two lines block port forwarding and X11 forwarding. - Press Ctrl+O, then Enter to save. Press Ctrl+X to exit.
Check the syntax and restart the service
- Test the configuration (requires administrator rights):No output means the syntax is valid. If you see an error, fix the file before you continue.Bash / Shell
sudo sshd -t
- Restart the SSH service (requires administrator rights):Bash / Shell
sudo systemctl restart ssh
Connect from 🪟 Windows 11
Windows 11 includes an OpenSSH client, so you need no extra software for a test.
Test the SFTP login
- Right-click the Start button and select Terminal.
- Connect to the server:Code
sftp USERNAME@192.0.2.10
- Type
yesif Windows asks you to trust the server's host key (its fingerprint) on first connection. - Enter the password. The prompt
sftp>appears. - Type
pwdto check your location. The server reports/, which is the chroot, not the real root of the server. - Type
cd filesto enter the writable folder. - Type
put C:\Users\USERNAME\Documents\test.txtto upload a file. Typelsto confirm it is there. - Type
exitto disconnect.
Confirm the account cannot open a shell
This check proves the restriction works.
- In Terminal, run:Code
ssh USERNAME@192.0.2.10
- Enter the password. The server answers
This service allows sftp connections only.and closes the connection.
Use a graphical client instead
WinSCP and FileZilla also work. In either one, choose SFTP as the protocol and enter 192.0.2.10 as the host. Use port 22, then sign in with USERNAME and its password.
Common problems
- "Broken pipe" or the connection closes right after login: the chroot ownership is wrong. Check that
/srv/sftpand/srv/sftp/USERNAMEare owned by root and not writable by the group or others. Review the log withsudo journalctl -u ssh. - "Permission denied" when uploading: you are at the top level of the chroot, which is read-only for the user. Change into the
filesfolder first. - Login fails with a correct password: confirm the user belongs to the group with
id USERNAME.
Undo the change
These steps remove the SFTP-only restriction.
- Delete the drop-in file (requires administrator rights):Bash / Shell
sudo rm /etc/ssh/sshd_config.d/50-sftp-chroot.conf
- Restart the service (requires administrator rights):Bash / Shell
sudo systemctl restart ssh
The user and folders stay on the server until you remove them, but the account is no longer restricted to SFTP.
What to do next
Password logins are the weak point of this setup. Switch the SFTP account to SSH key pair authentication, and keep it in the sftpusers group so the chroot still applies. To add more SFTP-only users, repeat the user and folder steps. The existing Match Group block already covers them.
Did this guide fix your problem?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
No comments yet — be the first to share your thoughts!