|
sFTP Server Chroot ConfigurationThis sFTP tutorial covers the configuration required to chroot a user to a home directory for sFTP sessions and deny the user a shell account.
|
|
Related YoLinux Tutorials:
Free Information Technology Magazines and Document Downloads |
This configuration will allow a system user to access their home directory using sFTP and to upload and download files with their account. The user will be denied access to the rest of the system as they will be chrooted to the user home directory. Thus users will not be able to snoop around the system to /etc or application directories. User login to a shell account will also be denied. The ability to chroot an sshd session of sftp has been available since OpenSSH 4.9. This is available with Red Hat Enterprise Linux 6 and Fedora 11 (and later) with OpenSSH 5.1. Ubuntu 11.04 uses OpenSSH 5.8.
RHEL 6 packages:
This configuration requires:
1) Define a group of which members will be chrooted:This is a standard Linux group assignment. The group name is user definable.Define a group: groupadd sftpusers Groups are defined in the file /etc/group
2) Add users to the group and deny users shell access:A non-working shell can be assigned to a user to prevent shell access. Linux includes two shells for this purpose:
The shell can be assigned to a user upon user account creation: useradd -s /bin/false -G sftpusers userid
The user group and shell assignment can be edited in the file /etc/passwd:
3) Create user home directories:The typical user home directory is /home/useridThe use of chroot requires a new root which is not "/". In this configuration we will use /home/sftpusers. All user home directories will have their true physical paths added to the rooted path at /home/sftpusers. Thus the true physical paths will be /home/sftpusers/home/userid but will appear to the user to be at /home/userid The user "root" must own the rooted directory: chown root.root /home/sftpusers The user "root" should own the rooted home directory: chown root.root /home/sftpusers/home The user will own their home path: chown userid.sftpusers -R /home/sftpusers/home/userid Set appropriate permissions: chmod 755 /home/sftpusers/home/userid/ Tip: Set SELinux rules on home directory: setsebool -P ssh_chroot_rw_homedirs on 4) SSH daemon configuration to chroot a user group:Edit the sshd configuration file: /etc/ssh/sshd_config
[Potential Pitfall]: You may get the following error: [user1]$ sftp user1@sftp.megacorp.com Connecting to 192.121.121.1... user1@sftp.megacorp.com's password: Write failed: Broken pipe Couldn't read packet: Connection reset by peerThis is typically due to a miss-configuration: Note that sshd will reject sftp connections to accounts that are set to chroot into any directory that has ownership/permissions that sshd doesn't consider secure. [Potential Pitfall]: You may get the following error: sftp> put example.sql Uploading example.sql to /home/user1/example.sql Couldn't get handle: Permission deniedThis is typically due to a directory permissions problem: /home/sftpusers - owned by root. This will be chrooted. /home/sftpusers/home - owned by root. /home/sftpusers/home/user1 - owned by user After sshd has chrooted to the ChrootDirectory, it will chdir to the home directory as normal.
Example sshd configuration file: /etc/ssh/sshd_config
Man pages:
|
|
Copyright © 2012 by Greg Ippolito