chroot jail for sftp users on Centos 6
I need a user (or users) to be able to sftp files to/from the server, but I want to chroot them to their own directory (so they cannot see any other part of the server)
In this example, the user name is MySFTPUser
I want all his files to go into:
/scpchroot/MySFTPUser/home/MySFTPUser
I want him to think his home directory is
/home/MySFTPUser
How to make it happen
As root:
Create a group for all users who are going to be jailed
groupadd sftpgroup
Configure the ssh service by editting the /etc/ssh/sshd_config file. Find the line:
Subsystem sftp /usr/libexec/openssh/sftp-server
and replace it with
Subsystem sftp internal-sftp
Add the following at the end of the file:
Match group sftpgroup ChrootDirectory /sftproot/%u X11Forwarding no AllowTcpForwarding no ForceCommand internal-sftp
Restart the sshd service
service sshd restart
Create the new user and set the password
useradd -d /home/MySFTPUser -M -s /bin/false -c "A SFTP User" -g sftpgroup MySFTPUser passwd MySFTPUser
Create the directory structure and permissions
mkdir -p /scpchroot/MySFTPUser/home/MySFTPUser chmod 755 /scpchroot/MySFTPUser chown MySFTPUser.sftpgroup /scpchroot/MySFTPUser/home/MySFTPUser
For any additional users, repeat from the “Create the new user” step and replace “MySFTPUser” with the user name you want to use.