Home Command line utilities How To Setup Chrooted SFTP In Linux
Setup Chrooted SFTP In Linux

This guide explains how to setup Chrooted SFTP to allow the users to connect through SFTP, but not allow them to connect through SSH. In other words, we are going to force the users to a specific directory and set their shell to /bin/nologin or some other shell that denies access to a ssh login. Are we clear? Good. Now, let us go ahead and setup Chrooted SFTP in Linux. This steps given below should work on any Linux distributions that supports openSSH.

Setup Chrooted SFTP In Linux

Starting from version 4.9, openSSH has a feature known as internal-sftp subsystem which allows only SFTP access, but not SSH access. So, the users can be able to access only the data from the server, but they can't access it using SSH.

Create Chrooted directory

First, Create a specific chrooted directory using command:

$ sudo mkdir /sftp

Make this directory fully owned by root user using command:

$ sudo chown root:root /sftp/

Under this directory, create separate directories for each user, like /sftp/user1/sftp/user2, and /sftp/user3 and so on.

For the purpose of this guide, I am going to create a directory called ostechnix under /sftp directory.

$ sudo mkdir /sftp/ostechnix

This is the directory where the users can save the data. Also, the users can't go beyond this directory. It's just like their $HOME directory.

Create sftp group and assign users to that group

Now, we need to create the users to be able to access SFTP chrooted directory.

Create a group called sftponly as shown in the following command:

$ sudo groupadd sftponly

Then, create new SFTP users or assign existing users to the "sftponly" group as shown below.

Let me create a new user, for example senthil, and assign him to the group called "sftponly". And then, setup his home directory as /sftp/ostechnix and the default shell as /sbin/nologin.

We can do this using the following online command:

$ sudo useradd -g sftponly -d /ostechnix -s /sbin/nologin senthil

Set password for the newly-created user using command:

$ sudo passwd senthil

To modify the existing user, use "usermod" instead of "useradd" command like below:

$ sudo usermod -g sftponly -d /ostechnix -s /sbin/nologin senthil

Assign proper permissions to the Chrooted directory

You need to assign proper permissions to the SFTP users to access their HOME directory like below.

$ sudo chown senthil:sftponly /sftp/ostechnix
$ sudo chmod 700 /sftp/ostechnix/

The other SFTP users can't access this directory.

Similarly, assign appropriate permissions to all other SFTP users as well.

Configure Chrooted SFTP

Edit /etc/ssh/sshd_config file:

$ sudo vi /etc/ssh/sshd_config

Find and comment out the following lines (i.e. add # in-front of it to comment out).

#Subsystem       sftp    /usr/libexec/openssh/sftp-server

In Ubuntu 18.04 LTS, find and comment the following line:

#Subsystem sftp /usr/lib/openssh/sftp-server

Next, add the following lines at the end of the file:

Subsystem       sftp    internal-sftp
Match group sftponly
     ChrootDirectory /sftp/
     X11Forwarding no
     AllowTcpForwarding no
     ForceCommand internal-sftp

Save and exit the file. Restart ssh service to update the changes.

$ sudo systemctl restart sshd

Now, try to SSH to this system from any other systems on the network using the sftp user (i.e senthil in our case).

$ ssh [email protected]

You will get the following error message.

[email protected]'s password: 
This service allows sftp connections only.
Connection to 192.168.43.2 closed.

Here, 192.168.43.2 is my remote system's IP address where we just configured SFTP.

You can only access the remote system using sftp as shown below.

$ sftp [email protected]
[email protected]'s password: 
Connected to 192.168.43.2.
sftp>

See? The user "senthil" can able to connect via sftp, but not ssh.

To know the current working directory, use 'pwd' command:

sftp> pwd
Remote working directory: /ostechnix
sftp>

Basic commands to get started with SFTP.

We can connect using an alternate port using -p flag.

$ sftp -P <port_number> [email protected]_host

To transfer remote file to the local system, do:

sftp> get /path/remote_file

We can transfer local file to the remote system using command:

sftp> put /path/local_file

To transfer remote folder to the local system recursively:

sftp> get -R /path/remote_folder

To display the list of files on local machine:

sftp> lls

To display the list of files on remote machine:

sftp> ls

For more details about sftp usage, refer man pages.

$ man sftp

Suggested read:


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

3 comments

Rob April 3, 2019 - 12:59 am

Fantastic. Simple and straight to the point. Thank you.

Reply
drakkan October 20, 2019 - 11:28 am

You can also try SFTPGo

https://github.com/drakkan/sftpgo

it has chroot support builtin, virtual quota, atomic uploads, bandwidth throttling and many other features.

It can execute configurable custom commands and/or send HTTP notifications on upload, download, delete or rename.

It is written in Go, so no runtime dependencies, and it works on Windows too

Reply
sk October 20, 2019 - 9:39 pm

Thanks for the update. I will try it for sure.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More