About Me

My photo
Kalyan Kumar Pasupuleti B-Tech(Information Technology). • RedHat Certified Engineer(RHCE) • Directory Services and Authentication Certificate of Expertise(LDAP) • Red Hat SELinux Policy Administration Certificate of Expertise(SELinux) • Network Services Security Certificate of Expertise (Network Services) • RedHat Certified Virtualization Administrator(RHCVA) • Red Hat Certified Security Specialist (RHCSS) Working as Cloud DevOps engineer

Thursday, April 4, 2013

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

This tutorial describes how to give users chrooted SSH and/or chrooted SFTP access on linux environment.
Using this setup, the users cannot see your whole system. Your users will be jailed in a specific directory which they will not be able to break out off.

If you want to setup an account on your system that will be used only to transfer files (and not to ssh to the system), you should setup SFTP Chroot Jail.

In a typical sftp scenario when chroot sftp is not setup, if you use sftp, you can see whole file system based on the permissions assigned to you.

If you want to give sftp access on your system to outside vendors to transfer files, you should not use standard sftp. Instead, you should setup Chroot SFTP Jail as explained below.

Non-Chroot SFTP Environment

In the following example (a typical sftp environment), user1 can sftp to the system, and view /etc folder and download the files from there:

# sftp user1@station1.example.com
user1@station1.example.com's password:
sftp> pwd
Remote working directory: /home/user1

sftp> ls
file1  abc.txt data 

sftp> cd /etc
sftp> ls -l passwd
-rw-r--r--    0 0        0            3750 Dec 29 23:09 passwd

sftp> get passwd
Fetching /etc/passwd to passwd
/etc/passwd     100% 2600     2.9KB/s   00:00

Chroot SFTP Environment

In the following example, user1 can sftp to the system, and view only the directory that you've designated for user1 to perform sftp (i.e /incoming).

When user1 tries to perform ‘cd /etc’, it will give an error message. Since SFTP is setup in an chroot environment, user1 cannot view any other files in the system.

# sftp user1@station1.example.com
user1@station1.example.com's password:
sftp> pwd
Remote working directory: /home/user1

sftp> ls
sftp> cd /etc
Couldn't canonicalise: No such file or directory

So I believe you all understood what is Chroot SFTP environment is, let us see how to configure this.

Creating a New Group

Create a group called sftpusers. Only users who belong to this group will be automatically restricted to the SFTP chroot environment on this system.

# groupadd sftpusers

Create a new User or Modify an Existing User

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

Now create a new user and give password as shown below:

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

Verify that the user got created properly or not
# grep user1 /etc/passwd

If you want to modify an existing user user2 and make him an sftp user only and put him in the chroot sftp jail, do the following:

# usermod -g sftpusers -d /incoming -s /sbin/nologin user2

Configure sftp-server

Modify the the /etc/ssh/sshd_config file and comment out the below line:

# vim /etc/ssh/sshd_config
#Subsystem       sftp    /usr/libexec/openssh/sftp-server

Next, add the following line to the /etc/ssh/sshd_config file
Subsystem       sftp    internal-sftp
Save and exit the file

Specify Chroot Directory for a Group

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

# tail /etc/ssh/sshd_config
Match Group sftpusers
        ChrootDirectory /sftp/%u
        ForceCommand internal-sftp

Match Group sftpusers – This indicates that the following lines will be matched only for users who belong to group sftpusers

ChrootDirectory /sftp/%u – This is the path that will be used for chroot after the user is authenticated. %u indicates the user. So, for user1, this will be /sftp/user1.

ForceCommand internal-sftp – This forces the execution of the internal-sftp and ignores any command that are mentioned in the ~/.ssh/rc file.

NOTE: The Match keyword is not supported in older versions (<5) on SSH so an upgrade of openssh will be necessary.

Create sftp Home Directory

Since we’ve specified /sftp as ChrootDirectory above, create this directory (which is equivalent of your  /home directory)

# mkdir /sftp
# mkdir /sftp/user1

So, /sftp/user1 is equivalent to / for the user1. When user1 sftp to the system, and performs “cd /”, they’ll be seeing only the content of the directories under “/sftp/user1” (and not the real / of the system). This is how the chroot works

So, under this directory /sftp/user1, create any sub directory that you like user to see. For example, create a incoming directory where users can sftp their files.

# mkdir /sftp/user1/incoming

Setup Appropriate Permissions

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

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

# chown user1:sftpusers /sftp/user1/incoming

Now check the permissions of the directories as shown below:

# ls -ld /sftp/user1/incoming
drwxr-xr-x 2 user1 sftpusers 4096 Apr 04 18:31 /sftp/user1/incoming

# ls -ld /sftp/user1
drwxr-xr-x 3 root root 4096 Apr 04 18:31 /sftp/user1

# ls -ld /sftp
drwxr-xr-x 3 root root 4096 Apr 04 18:31 /sftp

Now restart sshd and check/test your Chroot SFTP configuration

# /etc/init.d/sshd restart

Now you can observe that when user1 does sftp, and does “cd /”, he can only see incoming directory.

# sftp user1@station1.example.com
user1@station1.example.com's password:

sftp> pwd
Remote working directory: /incoming

sftp> cd /
sftp> ls

When user1 transfers any files to the /incoming directory from the sftp, they’ll be really located under /sftp/user1/incoming directory on the system.

Good Luck..... :)

No comments:

Post a Comment