Tutorial

How To Enable SFTP Without Shell Access on Ubuntu 20.04

How To Enable SFTP Without Shell Access on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

Introduction

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.

Prerequisites

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.

Step 1 — Creating a New User

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:

  1. 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.

Step 2 — Creating a Directory for File Transfers

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:

  1. sudo mkdir -p /var/sftp/uploads

Set the owner of /var/sftp to root:

  1. sudo chown root:root /var/sftp

Give root write permissions to the same directory, and give other users only read and execute rights:

  1. 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:

  1. sudo chown sammyfiles:sammyfiles /var/sftp/uploads

Now that the directory structure is in place, you can configure the SSH server itself.

Step 3 — Restricting Access to One Directory

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:

  1. sudo nano /etc/ssh/sshd_config

Scroll to the very bottom of the file and add the following configuration snippet:

/etc/ssh/sshd_config

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 User tells 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-sftp forces the SSH server to run the SFTP server upon login, disallowing shell access.
  • PasswordAuthentication yes allows password authentication for this user.
  • ChrootDirectory /var/sftp/ ensures that the user will not be allowed access to anything beyond the /var/sftp directory.
  • AllowAgentForwarding no, AllowTcpForwarding no, and X11Forwarding no disables 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 ENTER.

To apply the configuration changes, restart the service:

  1. 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.

Step 4 — Verifying the Configuration

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:

  1. ssh sammyfiles@your_server_ip

You’ll receive the following message before being returned to your original prompt:

  1. Output
    This service allows sftp connections only.
  2. 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:

  1. sftp sammyfiles@your_server_ip

Instead of an error message, this command will generate a successful login message with an interactive prompt:

  1. Output
    Connected to your_server_ip
  2. sftp>

You can list the directory contents using ls in the prompt:

  1. ls

This will show the uploads directory that was created in the previous step and return you to the sftp> prompt:

Output
uploads

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:

  1. 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.

Conclusion

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.

Learn more about us


About the authors
Default avatar

Technical Writer

Educator and writer committed to empowering our community by providing access to the knowledge and tools for making creative ideas into a reality


Default avatar

Manager, Developer Education

Technical Writer @ DigitalOcean


Default avatar

Software Engineer, CTO @Makimo

Creating bespoke software ◦ CTO & co-founder at Makimo. I’m a software enginner & a geek. I like making impossible things possible. And I need tea.


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These instructions are inaccurate. You don’t want to change the owner of /var/sftp/uploads. The owner should be root:root for /var/sftp/uploads. Hopefully you didn’t waste too much time correcting the problem.

When I follow this tutorial and connect to the server, it logs me into /home instead of /var/sftp/. How can I force it to default into /var/sftp?

Hi! There are several users. I want that at authorization of the user in sftp went to the folder. At me now sees all users, and has access only to the. You don’t need to see all users. How to do? (Google translate)

Doesn’t explain why would I need to disable AllowAgentForwarding, AllowTcpForwarding, and X11Forwarding.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel