In this guide, we are going to set up an SFTP-only server on Debian 11/12. We will also configure a chroot jail so SFTP users are restricted to a specific directory and cannot get a normal SSH shell.
SFTP (SSH File Transfer Protocol) runs over SSH, which provides encryption in transit. This makes it a safer alternative to legacy FTP, which transmits credentials and data in clear text unless wrapped with TLS.
Related Content
- How to install and set up sftp server in Ubuntu 22.04
- Download Files from SFTP server Using a python script
- List, Upload and Download files from an SFTP Server using golang
- How to set up an SFTP server on OpenSUSE Leap 15.3 Server
- How to install and set up sftp server in Ubuntu 20.04
- How to set up an SFTP server on CentOS 8 /RHEL 8 Server
Prerequisites
To follow along this guide ensure you have the following:
- A Debian 11 or Debian 12 server
- Root access to the server or a user with sudo access
- Internet access from the server
Ensuring that the server is up to date
Before proceeding, ensure your system is up to date. Use this command to refresh the system packages and update them.
| |
Ensuring that the SSH service is installed
Install OpenSSH server (this provides both SSH and SFTP):
| |
Now that it is installed, enable and start the service:
| |
Confirm its status
| |
Create SFTP users and directories (chroot layout)
We will store SFTP users under /srv/sftp using this layout:
/srv/sftp/<username>: chroot directory (must be owned by root and not writable)/srv/sftp/<username>/upload: writable directory owned by the user (where file uploads go)
Create the base directory:
| |
Create an umbrella group for SFTP-only users:
| |
Create an SFTP-only user called citizix (no shell login):
| |
The above options do the following:
-g sftpusers: Set the primary group tosftpusers-d /upload: Set the home directory (inside the chroot we will create)-s /usr/sbin/nologin: Disable shell access (SFTP-only)- Finally, username
citizix
Now create the chroot directory and the writable upload directory with correct ownership and permissions:
| |
Then add password to the created user using this command:
| |
Configure SSH for SFTP-only users (chroot)
Now we’ll configure OpenSSH so users in the sftpusers group:
- can only use SFTP (no shell)
- are chrooted to
/srv/sftp/%u - land in
/uploadon login
Edit the SSH server config:
| |
Ensure the SFTP subsystem uses internal-sftp (this is often the default, but it’s safe to set explicitly):
| |
Add this content at the bottom of the file:
| |
Then restart SSH to reload the config:
| |
Verify that SSH is running as expected:
| |
Verifying that the set up is working as expected
After successfully creating the user and adding SFTP configurations, let’s test the setup:
| |
Try a few commands:
| |
If everything is configured correctly, the user will be restricted to the chroot and should start in /upload.
Adding shared directories inside the chroot (optional)
If you want users to access additional directories, they must still be inside the chroot (/srv/sftp/<username>/...). Also remember: the chroot directory itself must be root-owned and not writable.
Example: create a shared directory under the chroot and make a subdirectory writable:
Create the directory
| |
That is it. Users should now have access.
Optional hardening tips
- Disable direct root login over SSH by setting
PermitRootLogin noin/etc/ssh/sshd_config. - If you only need SFTP for these accounts, keep shell access disabled (
/usr/sbin/nologin) as shown above. - Consider using key-based authentication for admin users and restricting SSH access with
AllowUsers/AllowGroups.
Conclusion
We managed to set up an SFTP-only server on Debian 11/12 in this guide, with chroot isolation and a dedicated upload directory for SFTP users.