SFTP stands for SSH File Transfer Protocol, and is a secure way to transfer files between machines using an encrypted SSH connection. Although similar in name, this is a different protocol than FTP (File Transfer Protocol), but SFTP is widely supported by modern FTP clients.
SFTP is available by default with no additional configuration on all servers with SSH access enabled. Though it’s secure and fairly straightforward to use, one disadvantage of SFTP is that in a standard configuration, the SSH server grants file transfer access and terminal shell access to all users with an account on the system. In many cases, it is more secure to apply granular control over user permissions. For example, you may want to allow certain users to only perform file transfers, but prevent them from gaining terminal access to the server over SSH.
In this tutorial, you’ll set up the SSH daemon to limit SFTP access to one directory with no SSH access allowed on a per-user basis.
To follow this tutorial, you will need access to an Ubuntu 20.04 server. This server should have a non-root user with
sudo privileges, as well as a firewall enabled. For help with setting this up, follow our Initial Server Setup Guide for Ubuntu 20.04.
First, create a new user who will be granted only file transfer access to the server. Here, we’re using the username sammyfiles, but you can use any username you like:
- sudo adduser sammyfiles
You’ll be prompted to create a password for the account, followed by some information about the user. The user information is optional, so you can press
ENTER to leave those fields blank.
You have now created a new user that will be granted access to the restricted directory. In the next step, you’ll create the directory for file transfers and set up the necessary permissions.
In order to restrict SFTP access to one directory, you first have to make sure the directory complies with the SSH server’s permissions requirements, which are very particular.
Specifically, the directory itself and all directories before it in the filesystem tree must be owned by root and not writable by anyone else. Consequently, it’s not possible to give restricted access to a user’s home directory because home directories are owned by the user, not root.
Note: Some versions of OpenSSH do not have such strict requirements for the directory structure and ownership, but most modern Linux distributions (including Ubuntu 20.04) do.
There are a number of ways to work around this ownership issue. In this tutorial, you’ll create and use
/var/sftp/uploads as the target upload directory.
/var/sftp will be owned by root and will not be writable by other users; the subdirectory
/var/sftp/uploads will be owned by sammyfiles, so that user will be able to upload files to it.
First, create the directories:
- sudo mkdir -p /var/sftp/uploads
Set the owner of
/var/sftp to root:
- sudo chown root:root /var/sftp
Give root write permissions to the same directory, and give other users only read and execute rights:
- sudo chmod 755 /var/sftp
Change the ownership on the
uploads directory to the user you just created. The following command again uses the example user and group sammyfiles, so be sure to change this if you gave the user you created a different name:
- sudo chown sammyfiles:sammyfiles /var/sftp/uploads
Now that the directory structure is in place, you can configure the SSH server itself.
In this step, you’ll modify the SSH server configuration to disallow terminal access for sammyfiles but allow file transfer access.
Open the SSH server configuration file using
nano or your favorite text editor:
- sudo nano /etc/ssh/sshd_config
Scroll to the very bottom of the file and add the following configuration snippet:
Match User sammyfiles ForceCommand internal-sftp PasswordAuthentication yes ChrootDirectory /var/sftp PermitTunnel no AllowAgentForwarding no AllowTcpForwarding no X11Forwarding no
Here’s what each directive does:
Match Usertells the SSH server to apply the following commands only to the specified user. Here, we specify sammyfiles. Again, make sure to update this with your own user’s name, if different.
ForceCommand internal-sftpforces the SSH server to run the SFTP server upon login, disallowing shell access.
PasswordAuthentication yesallows password authentication for this user.
ChrootDirectory /var/sftp/ensures that the user will not be allowed access to anything beyond the
AllowTcpForwarding no, and
X11Forwarding nodisables port forwarding, tunneling, and X11 forwarding, respectively. The purpose of adding these directives is to further limit this user’s access to the server.
This set of commands, starting with
Match User, can be copied and repeated for different users too. Make sure to modify the username in the
Match User line accordingly.
Note: You can omit the
PasswordAuthentication yes line and set up SSH key access for increased security. Follow the Copying your Public SSH Key section of the SSH Essentials: Working with SSH Servers, Clients, and Keys tutorial to do so. Make sure to do this before you disable shell access for the user.
In the next step, we’ll test the configuration by SSHing locally with password access, but if you set up SSH keys, you’ll need access to a computer with the user’s key pair.
After adding these lines, save and close the file. If you’re using nano, you can do this by pressing
CTRL + X, and then
Y, and then
To apply the configuration changes, restart the service:
- sudo systemctl restart sshd
You have now configured the SSH server to restrict access to file transfer only for sammyfiles. The last step is testing the configuration to make sure it works as intended.
Let’s ensure that our new sammyfiles user can only transfer files. As mentioned previously, SFTP is used to transfer files between machines. You can verify this works by testing a transfer between your local machine and server.
First, try logging into your server as the user you created in Step 1. Because of the settings you added to the SSH configuration file, this won’t be possible:
- ssh sammyfiles@your_server_ip
You’ll receive the following message before being returned to your original prompt:
- OutputThis service allows sftp connections only.
- Connection to your_server_ip closed.
This means that sammyfiles can no longer access the server shell using SSH.
Next, verify if the user can successfully access SFTP for file transfer:
- sftp sammyfiles@your_server_ip
Instead of an error message, this command will generate a successful login message with an interactive prompt:
- OutputConnected to your_server_ip
You can list the directory contents using
ls in the prompt:
This will show the
uploads directory that was created in the previous step and return you to the
To verify that the user is indeed restricted to this directory and cannot access any directory before it, you can try changing the directory to the previous one:
- cd ..
This command will not give an error but will list the directory contents as before with no change, proving that the user was not able to switch to the parent directory.
You have now verified that the restricted configuration works as intended. The newly created sammyfiles user can access the server only using the SFTP protocol for file transfer and has no ability to access the full shell.
You’ve restricted a user to SFTP-only access to a single directory on a server without full shell access. While this tutorial uses only one directory and one user for brevity, you can extend this example to multiple users and multiple directories.
The SSH server allows even more complex configuration schemes, including limiting access to groups or multiple users at once, or even limited access to certain IP addresses. You can find examples of additional configuration options and explanations of possible directives in the OpenSSH Cookbook. If you run into any issues with SSH, you can debug and fix them with this troubleshooting SSH series.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Click here to Sign up and get $200 of credit to try our products over 60 days!