DbAppWeb.com

Menu
  • Home
  • Linux
  • HP-UX
  • macOS
  • Windows
  • Web Servers
  • App Servers
  • Storage

How to Setup Chroot SFTP in Linux (Allow Only SFTP, not SSH)

May 18, 2020 DbAppWeb Admin

Chroot SFTP in Linux

Sometimes we need to transfer or access some files on a server through SFTP. In these cases we need to restrict the users so that they can access the files in their home directories only, other directories or files of the server should not be accessed by these users. It can be achieved through chroot SFTP, to setup chroot SFTP in Linux follows the steps given below:

Create a New Group for SFTP Users

Create a new group as sftpusers. Users who belong to this group will be automatically restricted to the SFTP chroot environment on this system.

# groupadd sftpusers

Create SFTP Users (or Modify Existing User)

Let us say you want to create a user sftp_user1 who should be allowed only to perform SFTP in a chroot environment and should not be allowed to perform SSH.

# useradd -g sftpusers -d /incoming -s /sbin/nologin sftp_user1
# passwd sftp_user1

The following command creates sftp_user1, assigns this user to sftpusers group, make /incoming as the home directory, and set /sbin/nologin as shell (which will not allow the user to ssh and get shell access).

Setup sftp-server Subsystem in sshd_config file

Modify the /etc/ssh/sshd_config file using vi editor and comment out the following line:

# override default of no subsystems
#Subsystem      sftp    /usr/libexec/openssh/sftp-server

Next, add the following line to the /etc/ssh/sshd_config file

# override default of no subsystems
#Subsystem      sftp    /usr/libexec/openssh/sftp-server
Subsystem       sftp    internal-sftp

Specify Chroot Directory for a Group

You want to put only certain users (i.e users who belong to sftpusers group) in the chroot jail environment. Add the following lines at the end of /etc/ssh/sshd_config

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       ForceCommand cvs server

Change above to

# Example of overriding settings on a per-user basis
Match Group sftpusers
  # Force the connection to use SFTP and chroot to the required directory.
  ChrootDirectory /home/sftp/%u
  ForceCommand internal-sftp
  # Disable tunneling, authentication agent, TCP and X11 forwarding.
  AllowAgentForwarding no
  AllowTcpForwarding no
  X11Forwarding no

In the above:

  • Match Group sftpusers – This indicates that the following lines will be matched only for users who belong to group sftpusers
  • ChrootDirectory /home/sftp/%u – This is the path that will be used for chroot after the user is authenticated. %u indicates the user. So, for sftp_user1, this will be /home/sftp/sftp_user1.
  • ForceCommand internal-sftp – This forces the execution of the internal-sftp and ignores any command that is mentioned in the ~/.ssh/rc file.

Setup Appropriate Permission for SFTP users

For chroot to work properly, you need to make sure appropriate permissions are set up properly on the directory you just created above.

Set the ownership to the user, and group to the sftpusers group as shown below.

# chown sftp_user1:sftpusers /home/sftp/sftp_user1/incoming

Restart sshd and Test Chroot SFTP

Restart the sshd service using the below command

# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
#

Test chroot sftp environment. As you see below, when sftp_user1 does sftp, and does “cd /”, they’ll only see the incoming directory.

# sftp sftp_user1@dbappweb.com
sftp_user1@dbappweb.com's password:
Connected to dbappweb.com.
sftp> pwd
Remote working directory: /incoming
sftp> cd /
sftp> ls
incoming
sftp> cd /etc
Couldn't canonicalize: No such file or directory
sftp>

As shown above, sftp_user1 is able to access only his home directory, other directories and files are not accessible to it.

Related Posts

  • SFTP remote readdir Permission denied error

Last Updated: May 18, 2020

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
Prev Article
Next Article
Tags:Linux RedHat Linux SFTP SSH Sys Admin System Security

No Responses

  1. Pingback: SFTP remote readdir Permission denied error - DbAppWeb.com

Leave a Reply Cancel Reply

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

  • Popular
  • Recent

Categories

  • Android (2)
  • Blogger (16)
  • Domain and Hosting (1)
  • Hardware Issues (7)
  • HP-UX (55)
  • HPE Data Protector (9)
  • IBM Lotus Notes (2)
  • IBM WebSphere Application Server (16)
  • Internet Tips & Tricks (15)
  • iOS (8)
  • JBoss/WildFly Application Server (2)
  • Linux (76)
  • macOS (15)
  • Microsoft Windows (31)
  • News and Updates (11)
  • Oracle Database (5)
  • SSL/TLS (1)
  • Storage Servers (23)
  • Tools/Softwares (1)
  • VMware ESXi (17)
  • Web Servers (14)
  • WordPress (5)

Archives

DbAppWeb.com

Solution of Database, Application and Web Server Problems

About DbAppWeb.com

One Stop Solution for Database Server, Application Server and Web Server Problems.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

DbAppWeb on Social Media

Copyright © 2026 DbAppWeb.com
Terms and Conditions   Theme by MyThemeShop.com