// Tutorial //

How To Enable SFTP Without Shell Access on Ubuntu 16.04

Published on May 31, 2017
Default avatar

By Mateusz Papiernik

Software Engineer, CTO @Makimo

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

Introduction

SFTP stands for SSH File Transfer Protocol. As its name suggests, it’s a secure way of transferring files to a server using an encrypted SSH connection. Despite the name, it’s a completely different protocol than FTP (File Transfer Protocol), though it’s widely supported by modern FTP clients.

SFTP is available by default with no additional configuration on all servers that have SSH access enabled. It’s secure and easy to use, but comes with a disadvantage: 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 some cases, you might want only certain users to be allowed file transfers and no SSH access. In this tutorial, we’ll set up the SSH daemon to limit SFTP access to one directory with no SSH access allowed on per user basis.

Prerequisites

To follow this tutorial, you will need:

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 we will 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, we 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 above it in the filesystem tree must be owned by root and not writable by anyone else. Consequently, it’s not possible to simply 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 16.04) do.

There are a number of ways to work around this ownership issue. In this tutorial, we’ll create and use /var/sftp/uploads as the target upload directory. /var/sftp will be owned by root and will be unwritable 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 sammyfiles.

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

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

Step 3 — Restricting Access to One Directory

In this step, we’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 append 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

Then save and close the file.

Here’s what each of those directives do:

  • Match User tells the SSH server to apply the following commands only to the user specified. Here, we specify sammyfiles.
  • 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. You can learn more about chroot in this chroot tutorial.
  • AllowAgentForwarding no, AllowTcpForwarding no. and X11Forwarding no disables port forwarding, tunneling and X11 forwarding for this user.

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 instead 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 instead need access to a computer with the user’s keypair.

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.

Logging in to the server as sammyfiles using normal shell access should no longer be possible. Let’s try it:

  1. ssh sammyfiles@localhost

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

Error message
This service allows sftp connections only. Connection to localhost closed.

This means that sammyfiles can no longer can access the server shell using SSH.

Next, let’s verify if the user can successfully access SFTP for file transfer.

  1. sftp sammyfiles@localhost

Instead of an error message, this command will show a successful login message with an interactive prompt.

SFTP prompt
Connected to localhost. 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.

SFTP file list output
uploads

To verify that the user is indeed restricted to this directory and cannot access any directory above it, you can try changing the directory to the one above it.

  1. cd ..

This command will not give an error, but listing the directory contents as before will show 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 he 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 more complex configuration schemes, including limiting access to groups or multiple users at once or limited access to certain IP addresses. You can find examples of additional configuration options and explanation 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

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.


Default avatar

staff technical writer

hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 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!

I was getting this error as well:

Error: Network error: Software caused connection abort Error: Could not connect to server

One entry in the sshd_config file that was omitted by the author:

#Subsystem sftp /usr/lib/openssh/sftp-server ‘comment this out’ Subsystem sftp internal-sftp ‘add this line’

Once added commented out the top line and added the second line, I was able to connect without any problems.

For people that have problems with the “Write failed: Broken pipe” or “Connection reset” errors, giving the root directory of the droplet 755 rights helped me solve the problems:

chmod 755 /

How would a non-sudo user get SFTP access to /var/www/html directory?

Can now only view .cache, .nano, .ssh directories.

Works perfectly for me, thanks a lot! Ubuntu 16.04.

Perfect document…

I followed this exactly (as far as I can tell) but I am getting:

packet_write_wait: Connection to xxx.xxx.xxx.xxx port 22: Broken pipe Connection closed

I know it’s hard to tell but where can I have missed?

Can’t get this to work. My Droplet was configured with a SSH-Key and even though I have:

Match User xyzuser
  ForceCommand internal-sftp
  PasswordAuthentication yes
  ChrootDirectory /var/sftp/uploads
  PermitTunnel no
  AllowAgentForwarding no
  AllowTcpForwarding no
  X11Forwarding no
```

I get the error:

```
Error:        	Disconnected: No supported authentication methods available (server sent: publickey)
Error:        	Could not connect to server
```

Hello, Great article and it helped me resolve my problem. However, I have one more little problem that I cannot seem to figure out and it is to do with WinSCP and chroot.

My issue is that when I use WinSCP to connect to the server I have set up it will work correctly except that the user and owner do not show up correctly. It shows as the UID and GID rather than the actual owner and group It will show the User as a value of 0 and Group as 1005. Its pulling the values from the /etc/group file.

When I access it via the shell it shows correct permissions for user/group -rwxrwsr-x+ 1 root nycdata 132 Mar 5 13:58 testfile.txt

So what I did to try correct this is that I copied the /etc/group and /etc/passwd files to the chrooted folder. Within the chrooted folder I created a folder called /etc. Also I created a folder called /bin and I dropped this into /etc of the chrooted folder.

However I still cannot see the correct permissions when I use WinSCP. Though I have not tried FileZilla. The issue is that I will be having users connecting via WinScp or Filezilla and I would like them to be able to see their files easily.

Not sure where I am going wrong but any advise would be great.

Thank you so much, Patrick

From FileZilla:

fzSftp started, protocol_version=8
...
Error:	Network error: Software caused connection abort
Error:	Could not connect to server

From SSH command-line

Connection reset by 138.197.96.36 port 22

Hi - not sure if anyone knows but I’ve set this up correctly and now failing to get access to work from WordPress Dashboard for updates. I’ve added the username & password credentials to my wpconfig file following this other tutorial: https://www.digitalocean.com/community/tutorials/how-to-configure-secure-updates-and-installations-in-wordpress-on-ubuntu

Note: I’m not using a SSH Key as yet so omitted this part.

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!

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

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

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
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