Want a friend or a family member to download files from your server, or to upload something for you, without access to the rest of the server? Create an account that can only use SFTP and sees only its own folder. It cannot open a terminal and cannot leave the folder. The steps were tested on Ubuntu 26.04 LTS; run the commands as root.
1. The Folders
The example gives the user friend a folder with two parts:
movies: your own folder/srv/data/movies, shown read-only;upload: a folder wherefriendcan save files.
SSH requires that the top folder of such an account belongs to root and that nobody else can write to it. Create the folders like this:
mkdir -p /srv/sftp/friend/upload /srv/sftp/friend/movies chown root:root /srv/sftp /srv/sftp/friend chmod 755 /srv/sftp /srv/sftp/friend
2. Create the Account
A group for all SFTP-only accounts, then the user, without a terminal:
groupadd sftponly useradd -g sftponly -s /usr/sbin/nologin -M -d /upload friend passwd friend chown friend:sftponly /srv/sftp/friend/upload
-d /upload makes the user start in the upload folder. Choose a strong password with passwd.
3. Lock the Group Into Its Folder
Create the file /etc/ssh/sshd_config.d/60-sftp-only.conf (for example with nano):
Match Group sftponly
ChrootDirectory /srv/sftp/%u
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
%u is the user name, so every account of the group gets its own folder /srv/sftp/name. Check the file and load it; the open SSH sessions stay connected:
sshd -t systemctl reload ssh
sshd -t prints nothing when everything is fine. You can also check what applies to one user: sshd -T -C user=friend,host=x,addr=203.0.113.5 | grep -E "chroot|forcecommand" must show /srv/sftp/%u and internal-sftp; for root the same check shows none, so your own login does not change.
The account logs in with its password. If you have switched off password logins for SSH (Log In to Your Linux Server With an SSH Key), add PasswordAuthentication yes to the Match block above, or give the user an SSH key instead.
4. Show Your Folder Read-Only
To show /srv/data/movies inside the account without copying it, mount it into the folder read-only. Add this line to /etc/fstab, so that it also works after a restart:
/srv/data/movies /srv/sftp/friend/movies none bind,ro 0 0
Then mount it now:
systemctl daemon-reload mount /srv/sftp/friend/movies
findmnt /srv/sftp/friend/movies shows ro in the options. New files in /srv/data/movies appear in the account at once.
5. How It Looks for the User
Your friend connects with FileZilla, WinSCP or another SFTP program: protocol SFTP, the address of your server, port 22, the user name and the password (Download files from server via FTP – FileZilla). The account then behaves like this:
- The account sees only
moviesandupload. Folders of the server such as/etcdo not exist for it, and going up from/stays in/. - Downloading from
moviesworks; uploading intomoviesfails (Failure), because it is read-only. - Uploading into
uploadworks. You find the files in/srv/sftp/friend/upload. - An SSH login with the same account only answers
This service allows sftp connections only.
6. Remove the Access
To end the access, remove the account and its mount:
umount /srv/sftp/friend/movies userdel friend
Then delete the line from /etc/fstab. Delete /srv/sftp/friend only after you have moved the uploaded files you want to keep; check first with findmnt /srv/sftp/friend/movies that nothing is mounted there any more, so that you do not delete your own data through the mount.