File transfers with SCP and SFTP

FTP is the de-facto standard for transferring files over the Internet. Although file transfers via HTTP and RSYNC are becoming more popular, these are typically for downloading files to your local system, whereas FTP is often used to transfer files to and from a remote system, or even remotely move and manipulate files.However, FTP is extremely insecure in that it is a completely plain-text protocol; everything from password credentials to file transfer data is sent in the clear.

SSH provides a means to transfer files securely using the SCP (Secure Copy Protocol) and SFTP programs. SCP is simply an SSH-aware “cp”. You must know the name of the file to copy prior to the transfer, and it sends files to or from a remote system over SSH.

SFTP, however, is an interactive FTP-like protocol that also uses SSH as a transport medium. This means that the entire FTP conversation is encrypted and you can use SSH keys with SFTP to allow for password-less FTP logins. Most FTP clients today support SFTP natively which means there is very little prep-work.

The fact that it runs over SSH remains a problem with SCP and SFTP, however, because a user can snoop outside of their home directory and could download sensitive files in the /etc directory, for instance. Most FTP servers for Linux have a chroot option that restricts the user to their home directory, and subdirectories, only. With SFTP, this is possible if you use the scponly “shell” which can chroot users to their home directory for SCP, SFTP, and RSYNC over SSH sessions.

You can download scponly from (be sure to grab the latest version, 4.3, to avoid potential security problems with older versions). Using scponly is extremely straightforward; instead of providing a shell like /bin/sh in your passwd file for a user, simply change the shell to use scponly.

To chroot users to their home directories, you need to do a little prep-work by running the supplied script to create the appropriate chroot environment for the user. Once this is done, the user is effectively jailed in their home directory.